xlsx: sheet_index is not necessarily monotonically increasing
When trying to convert some xlsx files to csv with orcus-xlsx, i got the same assertion on several files.
orcus-xlsx -f csv -o csv file.xlsx
orcus-xlsx: factory.cpp:219: virtual orcus::spreadsheet::iface::import_sheet* orcus::spreadsheet::import_factory::append_sheet(orcus::spreadsheet::sheet_t, const char*, size_t): Assertion `sheet_index == static_cast<sheet_t>(mp_impl->m_doc.sheet_size())' failed.
Aborted (core dumped)
I added some debugging code which shows that the sheet_index can be any arbitrary number, it needs not be monotonically increasing from 0. I verified the sheet_names are in the order they get displayed on LibreOffice.
After removing the assertion, i have successfully converted my xlsx files.
sheet_index: 3 m_sheets.size: 0 m_doc.sheet_size(): 0 sheet_name: Sheet1
sheet_index: 1 m_sheets.size: 1 m_doc.sheet_size(): 1 sheet_name: Sheet2
sheet_index: 0 m_sheets.size: 2 m_doc.sheet_size(): 2 sheet_name: Sheet3
sheet_index: 2 m_sheets.size: 3 m_doc.sheet_size(): 3 sheet_name: Sheet4
sheet_index: 8 m_sheets.size: 4 m_doc.sheet_size(): 4 sheet_name: Sheet5
sheet_index: 9 m_sheets.size: 5 m_doc.sheet_size(): 5 sheet_name: Sheet6
The xlsx files triggering the assertion can not be shared as they contain confidential information. I got the issue with multiple xlsx files received from independent suppliers. Those were probably made with MS Excel. I have not been able to reproduce with workbooks made by LibreOffice.
Debugging patch:
Index: liborcus-0.13.4/src/spreadsheet/factory.cpp
===================================================================
--- liborcus-0.13.4.orig/src/spreadsheet/factory.cpp
+++ liborcus-0.13.4/src/spreadsheet/factory.cpp
@@ -216,7 +216,9 @@ iface::import_pivot_cache_records* impor
iface::import_sheet* import_factory::append_sheet(
sheet_t sheet_index, const char* sheet_name, size_t sheet_name_length)
{
- assert(sheet_index == static_cast<sheet_t>(mp_impl->m_doc.sheet_size()));
+ std::cout << "sheet_index: " << sheet_index << " m_sheets.size: " << mp_impl->m_sheets.size() << " m_doc.sheet_size(): " << mp_impl->m_doc.sheet_size() << " sheet_name: " << sheet_name << std::endl;
+
+// assert(sheet_index == static_cast<sheet_t>(mp_impl->m_doc.sheet_size()));
sheet* sh = mp_impl->m_doc.append_sheet(
pstring(sheet_name, sheet_name_length), mp_impl->m_default_row_size, mp_impl->m_default_col_size);