-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Setting selected cells sometimes has no effect with XLSX writer #1887
Comments
Based on your description, I was able to at least partly duplicate your symptom. The spreadsheet that I write has its selected cells set correctly on each sheet. However, the spreadsheet object in the program now has the selected cells changed as you describe. I was able to avoid that by coding: $writer->setPreCalculateFormulas(false); This is probably an easier workaround than adding a lot of getCalculatedValue calls to your program. It is, of course, not a complete solution to your problem. |
* Fix for Issue #1887 - Lose Track of Selected Cells After Save Issue #1887 reports that selected cells are lost after saving Xlsx. Testing indicates that this applies to the object in memory, though not to the saved spreadsheet. Xlsx writer tries to save calculated values for cells which contain formulas. Calculation::_calculateFormulaValue issues a getStyle call merely to retrieve the quotePrefix property, which, if set, indicates that the cell does not contain a formula even though it looks like one. A side-effect of calls to getStyle is that selectedCell is updated. That is clearly accidental, and highly undesirable, in this case. Code is changed to save selectedCell before getStyle call and restore it afterwards. The problem was reported only for Xlsx save. To be on the safe side, test is made for output formats of Xlsx, Xls, Ods, Html (which basically includes Pdf), and Csv. For all of those, the object in memory is tested after the save. For Xlsx and Xls, the saved file is also tested. It does not make sense to test the saved file for Csv and Html. It does make sense to test it for Ods, but the necessary support is not yet present in either the Ods Reader or Ods Writer - a project for another day. * Move Logic Out of Calculation, Add Support for Ods ActiveSheet and SelectedCells Mark Baker thought logic belonged in Worksheet, not Calculation. I couldn't get it to work in Worksheet, but doing it in Cell works, and that has already been used to preserve ActiveSheet over call to getCalculatedValue, so this just extends that idea to SelectedCells. Original tests could not completely support Ods because of a lack of support for ActiveSheet and SelectedCells in Ods Reader and Writer. There's a lot missing in Ods support, but a journey of 1000 miles ... Those two particular concepts are now supported for Ods.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
Should be resolved by PR 1887, which was part of Release 18. |
Hello, I am having this problem too. I tried to use "setSelectedCells" or "setSelectedCell", but got unwanted behavior, as save function use another cell to be selected. Did anyone found a solution? |
What release of PhpSpreadsheet? Assuming that it's a current version, I would need to see code/spreadsheet which demonstrate this problem. |
@miguel2211 It is possible that PR #3841, which has been merged but is not yet part of an official release, fixes this problem. If you can test against master, give it a try. |
This is:
What is the expected behavior?
At the end of the creation of a multiworksheet Spreadsheet I use setSelectedCells to leave the way the client expected: with one specific cell selected. The expected behaviour is that opening the generated file with Excel the user finds the cell selected
What is the current behavior?
The current behaviour is that on the first worksheet the right cell is selectred while on subsequent (different) worksheets the selected cell is the bottom right cell (the last written). When the file is saved, the functions take care of the saving, call getStyle() that calls setSelectedCells() altering the selection of the worksheets.
I managed to track the changing of the selection to an unwanted value inserting some debug code in Worksheet.php and this is a stack trace of the time the value is altered:
The last few entries are mine, but you can see the steps arte started by seving the sheet.
save -> writeWorksheet -> writeSheetData -> writeCell -> writeCellFormula -> getCalculatedValue
What are the steps to reproduce?
Unfortunately I can't provide this for now, but for sure you can make a lot by following the trace. I think writeSheetData should save and recover the state of the selection. If I have time I may contribute to this.
Which versions of PhpSpreadsheet and PHP are affected?
This was tested on 1.15.0 - 2020-10-11
TEMPORARY FIX
The problem is caused by writeSheetData ending up calling getCalculatedValue so, if values are in cache this does not happen:
calling getCalculatedValue() after every formula writ,e fixed the wrong behaviour.
The text was updated successfully, but these errors were encountered: