Bug 164249 - XmlFilterBase::importFragment - XML parser failed in fragment, "invalid sheet index used
Summary: XmlFilterBase::importFragment - XML parser failed in fragment, "invalid sheet...
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Calc (show other bugs)
Version:
(earliest affected)
25.2.0.0 alpha0+
Hardware: All All
: medium normal
Assignee: Caolán McNamara
URL:
Whiteboard: target:25.8.0 target:25.2.2
Keywords:
Depends on:
Blocks: XLSX-Sheet
  Show dependency treegraph
 
Reported: 2024-12-09 09:46 UTC by Caolán McNamara
Modified: 2025-03-06 13:42 UTC (History)
5 users (show)

See Also:
Crash report or crash signature:


Attachments
much smaller example to reproduce the issue (11.94 KB, application/vnd.oasis.opendocument.spreadsheet)
2025-02-23 20:35 UTC, Caolán McNamara
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Caolán McNamara 2024-12-09 09:46:54 UTC
Description:
Some xlsx files when import and exported back to xlsx cannot be opened by LibreOffice

Steps to Reproduce:
1. wget https://bugs.documentfoundation.org/attachment.cgi?id=122409 -O ~/Downloads/tdf97598-1.xlsx
2. ./instdir/program/soffice --headless --convert-to xlsx ~/Downloads/tdf97598-1.xlsx
3. ./instdir/program/soffice --headless --convert-to pdf ./tdf97598-1.xlsx

Actual Results:
warn:sc:873055:873055:sc/source/filter/oox/formulabase.cxx:1383: OpCodeProviderImpl::initFuncOpCode - no opcode mapping for function ODF 'ORG.OPENOFFICE.EASTERSUNDAY' <-> OOXML '_xlfn.ORG.OPENOFFICE.EASTERSUNDAY'
warn:sc:873055:873055:sc/source/filter/oox/formulabase.cxx:1383: OpCodeProviderImpl::initFuncOpCode - no opcode mapping for function ODF 'ORG.OPENOFFICE.EASTERSUNDAY' <-> OOXML 'EASTERSUNDAY'
warn:legacy.osl:873055:873055:sc/source/filter/oox/formulabase.cxx:1244: OpCodeProviderImpl::OpCodeProviderImpl - opcodes not initialized
warn:oox:873055:873055:oox/source/core/xmlfilterbase.cxx:424: XmlFilterBase::importFragment - XML parser failed in fragment 'xl/workbook.xml' com.sun.star.uno.RuntimeException message: "invalid sheet index used at /home/caolan/LibreOffice/core/sc/source/filter/oox/workbookhelper.cxx:438"
Error: source file could not be loaded

Expected Results:
successful roundtrip


Reproducible: Always


User Profile Reset: No

Additional Info:
not known if this ever worked
Comment 1 Caolán McNamara 2024-12-10 08:37:08 UTC
I wonder if this has anything to do with bug #162177 given the mention of "no opcode mapping for function ODF 'ORG.OPENOFFICE.EASTERSUNDAY' <-> OOXML '_xlfn.ORG.OPENOFFICE.EASTERSUNDAY'"
Comment 2 Eike Rathke 2024-12-12 15:17:05 UTC
Sounds very likely.
If I find time over the weekend / next week I can take a look at this / that change.
Comment 3 Eike Rathke 2024-12-23 17:40:31 UTC
The EASTERSUNDAY name quirk is only an unrelated side condition, not the cause. Will fix separately as a follow-up for bug 162177.

The invalid sheet index looks more relevant, and there are also a lot of warnings during save already in several different areas, among them loads of

warn:legacy.osl:569730:569730:sc/source/filter/excel/xeformula.cxx:519: XclExpFmlaCompImpl::CreateFormula - unknown garbage behind formula
and
warn:legacy.osl:569730:569730:sc/source/filter/excel/xename.cxx:247: XclExpName::SetLocalTab - invalid sheet index
Comment 4 Eike Rathke 2024-12-23 21:53:19 UTC
Something's utterly broken with that document. The original has sheets

xl/worksheets/sheet1.xml
xl/worksheets/sheet2.xml
xl/worksheets/sheet3.xml
xl/worksheets/sheet4.xml
xl/worksheets/sheet5.xml

with after loading all sheets except "Purchase order" (sheet2) hidden, but there are actually 10 sheets; 3 are scenario data (sample1,sample2,sample3) of which bug 97598 was about to not apply that data to the Purchase order sheet.

xl/workbook.xml has

  <sheets>
    <sheet name="AutoOpen Stub Data" sheetId="1" state="veryHidden" r:id="rId1"/>
    <sheet name="Purchase order" sheetId="3" r:id="rId2"/>
    <sheet name="Macros" sheetId="4" state="veryHidden" r:id="rId3"/>
    <sheet name="ATW" sheetId="5" state="veryHidden" r:id="rId4"/>
    <sheet name="Lock" sheetId="6" state="veryHidden" r:id="rId5"/>
    <sheet name="Intl Data Table" sheetId="7" state="veryHidden" r:id="rId6"/>
    <sheet name="TemplateInformation" sheetId="8" state="veryHidden" r:id="rId7"/>
  </sheets>

so 7 sheets, of which at least 2 (ATW, Lock) apparently are Excel internal drawing/database/template related structures.

When saved to .xlsx again it results in

xl/worksheets/sheet1.xml
xl/worksheets/sheet2.xml
xl/worksheets/sheet6.xml
xl/worksheets/sheet7.xml
xl/worksheets/sheet8.xml
xl/worksheets/sheet9.xml
xl/worksheets/sheet10.xml

so 7 sheets, with the scenario data sheets omitted but the ATW and Lock sheets included.

When loading that again the "bad sheet index" complained about is 8, greater than the number of sheets (but maybe file numbering would have to be taken into account).

Fwiw, xl/workbook.xml then has

  <sheets>
    <sheet name="AutoOpen Stub Data" sheetId="1" state="hidden" r:id="rId3"/>
    <sheet name="Purchase order" sheetId="2" state="visible" r:id="rId4"/>
    <sheet name="Macros" sheetId="6" state="hidden" r:id="rId5"/>
    <sheet name="ATW" sheetId="7" state="hidden" r:id="rId6"/>
    <sheet name="Lock" sheetId="8" state="hidden" r:id="rId7"/>
    <sheet name="Intl Data Table" sheetId="9" state="hidden" r:id="rId8"/>
    <sheet name="TemplateInformation" sheetId="10" state="hidden" r:id="rId9"/>
  </sheets>


Unassigning myself from this.
Comment 5 Caolán McNamara 2025-02-23 20:35:52 UTC
Created attachment 199412 [details]
much smaller example to reproduce the issue

This is a far simpler example that reproduces the problem. Apparently there are sheets that are not exported, but we write a localsheetid based on the calc tab index for xlsx, so on the reimport we have localsheetid's that are out of sync with that reality. for xls we use an index adjusted for what we export. So presumbly we could do that for xlsx too
Comment 6 Commit Notification 2025-02-24 08:40:44 UTC
Caolán McNamara committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/bf4fe0c2ba1d0a44cc07c6afba6d56b8adf8af50

tdf#164249 adjust XML_localSheetId to take into account not exported sheets

It will be available in 25.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 7 Commit Notification 2025-03-06 13:42:41 UTC
Caolán McNamara committed a patch related to this issue.
It has been pushed to "libreoffice-25-2":

https://git.libreoffice.org/core/commit/c6df63bf9d4731f7d1f2d7cc95f247c72f0e3045

tdf#164249 adjust XML_localSheetId to take into account not exported sheets

It will be available in 25.2.2.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.