Repository navigation
fix(XlsxFormat): preserve macro sheets - #306
Conversation
CDefinedName::toXML only wrote name, localSheetId and hidden, so a round trip dropped function, xlm, vbProcedure, shortcutKey, comment and the other attributes. An Auto_Open name pointing at an Excel 4.0 macro lost function="1" xlm="1" and Excel no longer ran it as a macro. The reader also looked for "shortcutKey " with a trailing space and never matched. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: dsyzov <dmytro.syzov@nextcloud.com>
Macro sheets (xlMacrosheet and xlIntlMacrosheet) have no model in the editor, so opening an xlsm and saving it dropped them with everything they own: XLM formulas, form buttons and the defined names that point into them. localSheetId and activeTab were left pointing past the end of the shortened sheet list. On open, x2t now takes each macro sheet out of the workbook together with its related parts and the names it owns, makes the sheet XML self-contained (shared strings inlined, style indices removed), and writes it as an opaque PreservedSheets record (type 100) in Editor.bin. The remaining localSheetId and bookViews indices are shifted to match. On save the record is read back and each sheet is reinserted at its original position with its parts and names, the indices remapped and the parts renamed so they cannot collide with the editor's own. The x2tTester sheets_macro case converts a fixture with a macro sheet between two worksheets through xlst and back to xlsm and compares the result with a snapshot. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: dsyzov <dmytro.syzov@nextcloud.com>
A defined name scoped to a normal sheet but pointing into a macro sheet travels with the macro sheet through the editor. Its localSheetId was kept as the original sheet position, which is wrong once sheets are deleted or reordered in the editor: Excel then reports the file as damaged and drops the name, or the name lands on another sheet. A name's scope is now checked against every macro sheet before its formula, so a name scoped to one macro sheet is not taken by another one it refers to. For a name scoped to a normal sheet, the sheet's name is stored in Editor.bin (new DefinedNameScope item) and looked up again on save, since the editor renumbers its sheets. If that sheet no longer exists, the name is dropped, as Excel does when a sheet is deleted; renaming the sheet in the editor has the same effect. "[1]Macro1!A1" refers to a sheet in another workbook, so such a name now stays in the editor; "[0]Macro1!A1" still means this workbook. The sheets_macro x2tTester case gets two more inputs: one with names of each kind, and one that has already been saved through xlst once, to check that part names do not grow with every save. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: dsyzov <dmytro.syzov@nextcloud.com>
…g them
The PreservedSheets record comes back from the editor on save, and the
client can also build Editor.bin itself ("Download as"). The part names
in the record were used to build output file paths as they were.
RestorePreservedSheets now writes a preserved sheet only if it looks
like one CollectPreservedSheets made: a macro sheet relationship type,
part names that are relative and normalized (no empty, "." or ".."
segments, no backslash or colon), and part names and content types that
can go into the package XML unescaped. Any other sheet is dropped.
Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: dsyzov <dmytro.syzov@nextcloud.com>
shortcutKey holds the key itself ("k"), while the XLSB name record
keeps its character code. toBin passed the attribute to std::stoi,
which throws for a letter and aborted the xlsx -> xlsb conversion.
fromBin wrote the code as a decimal number ("107", or "0" for a name
without a key). Neither showed while the reader missed the attribute
and toXML did not write it; both do since "write all defined name
attributes back to the workbook".
toBin now stores the character code of an ASCII key (0 otherwise), and
fromBin sets shortcutKey only for a non-zero code, as that character.
Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: dsyzov <dmytro.syzov@nextcloud.com>
main took workbook record id 100 for SourceFileType (#364), the same id PreservedSheets used. Both enum entries compile, since duplicate values are legal, but a workbook opened from ODS would then send its source file type back through the preserved sheets reader. PreservedSheets is now 101, with a note on which id the next Euro-Office record takes. sdkjs needs the same number. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: dsyzov <dmytro.syzov@nextcloud.com>
…o case The other configs moved to build/package/bin/ when CI started packing core; the sheets_macro config still pointed at build/package/ and would not find x2t. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: dsyzov <dmytro.syzov@nextcloud.com>
41c395c to
a3d98c1
Compare
A shared-string cell in a preserved macro sheet becomes an inline string, and the item was copied from sharedStrings.xml as it was. When the two parts use different prefixes for the spreadsheet namespace, the copied elements end up wrong in the sheet: - sharedStrings.xml with an "x:" prefix (as the Open XML SDK writes it) and a sheet that doesn't declare "x:": the sheet is not well formed and LibreOffice drops the rest of it; - unprefixed shared strings in a sheet that uses "x:": the text lands in no namespace and is lost without an error. Each item now keeps the prefix of its <si>, and its elements take the prefix of the cell they are copied into. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: dsyzov <dmytro.syzov@nextcloud.com>
The editor doesn't know the preserved macro sheets, so a user can give an editor sheet the name of one. On save the macro sheet became "Name (2)", but the names that travel with it, Auto_Open for example, still said "Name!" and so pointed at the user's sheet: the macro stopped working without an error. The suffix could also take the name past Excel's 31 characters. The new name is now shortened to 31 characters and never takes another preserved sheet's name, and the references to the renamed sheet in the names that travel with the preserved sheets are rewritten to it. The references are found the same way RefersToSheet finds them; both now share one matcher. VBA code, cell formulas and controls that name the sheet can't be followed and still point at the user's sheet. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: dsyzov <dmytro.syzov@nextcloud.com>
core has no dialog sheet support, so the editor never got the dialog sheets of an xlsm, and they were lost on save. They also broke the index arithmetic of the preserved sheets, which assumes that every sheet that isn't preserved reaches the editor: with Data, Dialog1, Macro1, Last, the saved file had Data, Last, Macro1, and a name scoped to Last ended up scoped to Macro1. Dialog sheets are macro content like the macro sheets (Excel doesn't keep them in a macro-free workbook either), so they now travel the same way and are written back to xl/dialogsheets/. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: dsyzov <dmytro.syzov@nextcloud.com>
PrepareWorkbook gives the editor an empty sheet when a workbook has no worksheet, and replaces the sheet list for it. It ran before CollectPreservedSheets, so a workbook with only macro sheets lost them: no record was written, and the round trip produced a package with nothing but the theme. The macro sheets are now collected first; the editor gets the empty sheet, and the macro sheets come back next to it on save. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: dsyzov <dmytro.syzov@nextcloud.com>
An XLSB name record has every field, and fromBin set all of them. Since toXML writes every defined name attribute, xlsb -> xlsx wrote comment="" description="" help="" function="0" vbProcedure="0" and so on for every name. fromBin now keeps only the values that differ from the XML defaults, which is what toBin assumes in the other direction. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: dsyzov <dmytro.syzov@nextcloud.com>
The names that travel with a preserved sheet are out of the editor, so the user can define a name with the same name and scope there. Both were written on save, and Excel rejects a workbook with a duplicate name. The user's name now keeps it, as an editor sheet keeps a clashing sheet name, and the travelling one is dropped. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: dsyzov <dmytro.syzov@nextcloud.com>
CPreservedSheetFile::write found the package root by matching the expected "<folder>/<file name>" at the end of the output path. When it didn't match, a fallback wrote the sheet part alone, without its .rels and related parts, so its r:id references pointed nowhere. The root is now the output file's folder, as many levels up as the sheet's folder has, and the file name is taken from the output path, so there is no fallback. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: dsyzov <dmytro.syzov@nextcloud.com>
CContentTypes keeps its Default and Override entries in unordered maps and wrote them in iteration order, which depends on the standard library. The same document therefore gave a different [Content_Types].xml with each toolchain: the sheets_macro snapshots, made with an Ubuntu build, failed in CI's Rocky 8 builds with the same entries in another order, and editorbin_source_file_type already compares the file as a sorted list for the same reason (macOS and Linux). Write now sorts the entries by key (extension, part name). The order has no meaning for readers. Packages written through CContentTypes change their entry order once; the sheets_macro snapshots are updated, and the other x2tTester cases don't use this writer. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: dsyzov <dmytro.syzov@nextcloud.com>
chrip
left a comment
There was a problem hiding this comment.
Approve. Looks good to merge.
This carries Excel 4.0 macro sheets and Excel 5.0 dialog sheets through Editor.bin, together with their parts and the defined names that point into them, and writes them back when the file is saved as xlsm. I read the core changes and the three test files, and the new sheets_macro conversion test passes on both Rocky 8 builds. I did not build it locally or open the result in Excel. The editor side is Euro-Office/sdkjs#178. This PR can go in first: until that one is merged, sdkjs skips the unknown record 101 and the macro sheets are dropped on save, as they are today.
suggestion: Add test files for the cases the later commits fix.
All three inputs in Test/Applications/x2tTester/input_sheets_macro/ have one macro sheet between two worksheets and unprefixed shared strings. A dialog sheet (5da368b), a workbook with only macro sheets (4b21a26) and an x:-prefixed sharedStrings.xml (e36402e) go through the same xlsm → xlst → xlsm path, so each one needs only another input file and snapshot.
Assisted-by: ClaudeCode:claude-opus-5-5
Macro sheets (xlMacrosheet and xlIntlMacrosheet) have no model in the
editor, so opening an xlsm and saving it dropped them with everything
they own: XLM formulas, form buttons and the defined names that point
into them. localSheetId and activeTab were left pointing past the end
of the shortened sheet list.
On open, x2t now takes each macro sheet out of the workbook together
with its related parts and the names it owns, makes the sheet XML
self-contained (shared strings inlined, style indices removed), and
writes it as an opaque PreservedSheets record (type 101) in Editor.bin.
The remaining localSheetId and bookViews indices are shifted to match.
On save the record is read back and each sheet is reinserted at its
original position with its parts and names, the indices remapped and
the parts renamed so they cannot collide with the editor's own.
Assisted-by: ClaudeCode:claude-opus-5-5