ycphs / openxlsx

openxlsx - a fast way to read and write complex xslx files
https://ycphs.github.io/openxlsx/
Other
226 stars 75 forks source link

Corrupt file when dropdown list exists in loaded workbook #303

Closed christianbraathen closed 2 years ago

christianbraathen commented 2 years ago
JanMarvin commented 2 years ago

Hi @christianbraathen , could you please provide an example file. You can attach it to the github issue (it was already requested in the issue template you choose to ignore). Thanks

christianbraathen commented 2 years ago

Hi! Sorry for the late reply here. I discovered the solution, so I'll just give it to you outright. If you still want me to modify by filling out the template and providing an example file, just let me know.

However, the solution is actually ridiculously simple: Adding the sheet name to the cell references that are made within the same sheet as the formula. In ordinary Excel, writing in Sheet 1, it would normally be sufficient to write—for example—=A1 in cell B1. However, 'Sheet 1'!$A$1 is needed in B1 to avoid corrupted files in Excel for Mac.

Please note I have tried both with/without absolute references—they can be skipped. So both 'Sheet 1'!$A$1 and 'Sheet 1'!A1 produces a non-corrupt file.

So I would recommend that you modify writeFormula (and writeData if input is of class formula) to check if the reference is missing the sheet name, and to automatically prepend it if is. Alternatively, just write in the documentation that it is recommended to use sheet names in references to avoid corrupted files for Mac users.

Let me know if you need more from me. And thumbs up for an awesome package!

JanMarvin commented 2 years ago

Great that you found a solution! I merely requested an R example, some code to understand what you did or want to achieve. Something minimal like

wb <- createWorkbook()
addWorksheet()
writeData()
... # something that causes the corruption
saveWorkbook ()

Right now I simply got that you have an issue an found a solution. But no worries, if you've solved the issue you can simply close the issue.

christianbraathen commented 2 years ago

I realize now that the error was actually something quite different. It is simply that all formulas must follow the syntax of using commas as argument separators. So there is no need to use the sheet references unless the data occurs in other sheets. Demonstrated within the sheet Bar:

So @ycphs , there's a need to update the documentation of writeFormula, specifying that the arguments must be comma-separated.

christianbraathen commented 2 years ago

PR here: #322