Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing the reference of a shared formula breaks the workbook #1217

Closed
JanMarvin opened this issue Dec 16, 2024 · 2 comments
Closed

Replacing the reference of a shared formula breaks the workbook #1217

JanMarvin opened this issue Dec 16, 2024 · 2 comments
Labels
bug 🐛 Something isn't working

Comments

@JanMarvin
Copy link
Owner

A bug that I actually caught early on in development (using the loadExample.xlsx file iirc). But until recently, we were unable to fill and recreate shared formulas and apparently nobody else noticed it (or cared enough to report it).

What causes the bug: The reference of the shared formula is overwritten. Therefore the remaining shared formula cells have no reference. Excel will complain and fixes the workbook by removing the shared formula from the remaining cells.

The fix: A proper fix would be

  • Detect if a cell is the reference of shared formula
  • Get replace the formulas for all the shared formula cell with their un-shared variant
library(openxlsx2)

wb <- wb_workbook()$add_worksheet()$
  add_data(x = 1)$
  add_formula(dims = "B1:D1", x = "A1 + 1", shared = TRUE)

wb$add_data(x = 2, dims = "B1") # overwrite the reference of the shared formula

if (interactive()) wb$open()

Another rather interesting behavior I realized just now, if one replaces a shared formula cell with something else, the other shared formula cells could still be impacted. In the example below, a cell of the shared formula range is replaced with a value. this changes the output of the formulas. The Values will now be: 1, 2, 2*, 3. (* indicates the replaced value) instead of 1, 2, 3, 4 (as in the original example without any replacement).

Therefore it might be helpful to throw a warning, if the user replaces any shared formula cell.

library(openxlsx2)

wb <- wb_workbook()$add_worksheet()$
  add_data(x = 1)$
  add_formula(dims = "B1:D1", x = "A1 + 1", shared = TRUE)

wb$add_data(x = 2, dims = "C1") # a shared formula cell

if (interactive()) wb$open()
@JanMarvin JanMarvin added the bug 🐛 Something isn't working label Dec 16, 2024
@JanMarvin
Copy link
Owner Author

#1091 (comment)

@JanMarvin
Copy link
Owner Author

Closed in #1218

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant