-
Notifications
You must be signed in to change notification settings - Fork 15
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
Why aren't openxlsx2
formulas evaluated?
#863
Comments
There is another issue, that the formulas are not recalculated in a saved workbook. Is there any way to mark cells for a recompute or mark the whole document for recompute? Because, when I open a file, make some changes and save it as another file, cached values persist. I can manually recalculate whole document with Ctrl + Shift + F9 (at least in LibreOffice), but this is inconvenient, if these files are meant to be used by someone else. That person may not be aware of the outdated values. Is there a way to remove all cached values? |
You could try something similar to the snippet below. This removes all cached values for a single sheet (it wont handle pivot tables and probably many other things). But for your example, I'd say the case is a little different. In this case, it should be the responsibility of the owner of the output file to make sure that the values are all updated as expected. Unfortunately even in spreadsheet heavy work environments it is often not possible to make sure that all workbooks are updated to the latest. Someone linked something from another spreadsheet, but in the meantime this spreadsheet has already been updated? This linked spreadsheet is on a netshare (like fl <- system.file("extdata", "openxlsx2_example.xlsx", package = "openxlsx2")
wb <- openxlsx2::wb_load(file = fl)
## get cc from worksheet
cc <- wb$worksheets[[1]]$sheet_data$cc
## update cached value
cc[cc$f != "", "v"] <- ""
## add cc to worksheet
wb$worksheets[[1]]$sheet_data$cc <- cc
if (interactive()) wb$open() |
Thanks a lot! |
:) Unfortunately, often it is not feasible to search for better solutions, after all time is money. But I'm not advocating for spreadsheets. I didn't learn using statistical software just for toying around and I value databases. |
I have written about this issue already in various places of the
openxlsx
issue tracker and probably in other places and this issue is just to raise awareness and not something to solve or something I'm going to work on.The reason why formulas are not evaluated is, because there is nothing to evaluate them.
wb_to_df()
also shows values?" "Yes again."Why is this the case?
The XML file structure of a formula written by us looks like this:
Here we have a cell with a simple formula:
SUM(A1:B1)
. If A1 is1
and B1 is1
the formula will evaluate 2. In your spreadsheet software you can see the value2
. If you save the file to disk and load it again, the XML structure of the cell has changed. Now it looks like this:It contains a value field now and this value is
2
. Now if we read the value fromopenxlsx2
we also get a2
. But only because this value is now written into the cell. It is "cached" into the cell. And this might actually become an issue.If you replace the cell
A1
with a value of-1
(wb_add_data(dims = "A1", x = -1)
. Your formula in cell will still print2
if read bywb_to_df()
, and the formula is obviously not evaluated, because it should show a value of0
.But why is the value not updated?
That is because, computers programs - in this case your spreadsheet software - need a way to evaluate the formula in the cell with actual data in the spreadsheet. The software needs a way to calculate the formula and it needs a reason to evaluate this formula. The implementation of the formula evaluation is somewhat simple with basic formulas like
A1 + A2
andSUM(A1:A2)
, but it will get tricky if you ever try to implement something like nestedVLOOKUP()
/CHOOSE()
functions, functions where R and your spreadsheet software produce different results and last but not least, someone has to program all/many/most/at least a few of the functions and their quirks into a function we can evaluate in R. The first part is, to actually provide functions that reproduce what the complementary spreadsheet functions do. Since we cannot use a spreadsheet software, we would have to program them ourselves. And I do not want to do this and I do not want to be the one who does this and if you want me to be the one, lets talk about my salary ..., but good news!You could be the one!
It is not really hard to begin with. First, most likely you do not want EVERY function (this could take a while, because there are a lot), but instead focus on only a few. Second, begin with something easy and not with an overly complex 300 lines of code containing nested INDEX() functions. Instead let us have a look at the basics, like
ABS()
,SUM()
orAVERAGE()
and maybe begin with something like this. The idea is get the sheet, reference and operators from the formula and evaluate everything in some R formula just like base R functions.Draft code
Here I use spreadsheet formulas using
+
andSUM()
and my code matches them with Rs+
andsum()
. Similar I could useMIN()
/MAX()
and other functions that have identical names. ForAVERAGE()
I might need something likeaverage <- function(...) mean(...)
and other more complex functions might require actual custom written functions:The text was updated successfully, but these errors were encountered: