Skip to content

Commit 86daa51

Browse files
committed
Support saving file in case of a missing workbookView
Note that Google Sheets sometimes saves a file without workbookView even if a worksheet has a sheetView. Fixes #6
1 parent 6e39730 commit 86daa51

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

source/detail/serialization/xlsx_producer.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,16 +2365,24 @@ void xlsx_producer::write_worksheet(const relationship &rel)
23652365
write_start_element(xmlns, "sheetViews");
23662366
write_start_element(xmlns, "sheetView");
23672367

2368-
const auto wb_view = source_.view();
23692368
const auto view = ws.view();
23702369

23712370
if (!view.show_grid_lines())
23722371
{
23732372
write_attribute("showGridLines", write_bool(view.show_grid_lines()));
23742373
}
23752374

2376-
if ((wb_view.active_tab.is_set() && (ws.id() - 1) == wb_view.active_tab.get())
2377-
|| (!wb_view.active_tab.is_set() && ws.id() == 1))
2375+
if (source_.has_view())
2376+
{
2377+
const auto wb_view = source_.view();
2378+
2379+
if ((wb_view.active_tab.is_set() && (ws.id() - 1) == wb_view.active_tab.get())
2380+
|| (!wb_view.active_tab.is_set() && ws.id() == 1))
2381+
{
2382+
write_attribute("tabSelected", write_bool(true));
2383+
}
2384+
}
2385+
else if (ws.id() == 1)
23782386
{
23792387
write_attribute("tabSelected", write_bool(true));
23802388
}
7.43 KB
Binary file not shown.

tests/workbook/serialization_test_suite.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class serialization_test_suite : public test_suite
7474
register_test(test_formatting);
7575
register_test(test_active_sheet);
7676
register_test(test_locale_comma);
77+
register_test(test_Issue6_google_missing_workbookView);
7778
}
7879

7980
bool workbook_matches_file(xlnt::workbook &wb, const xlnt::path &file)
@@ -824,6 +825,13 @@ class serialization_test_suite : public test_suite
824825
xlnt_assert_equals(ws.cell("A1").value<double>(), 1.9999999999);
825826
xlnt_assert_equals(ws.cell("A2").value<double>(), 1.1);
826827
}
828+
829+
void test_Issue6_google_missing_workbookView()
830+
{
831+
xlnt::workbook wb;
832+
wb.load(path_helper::test_file("Issue6_google_missing_workbookView.xlsx"));
833+
xlnt_assert_throws_nothing(wb.save("temp.xlsx"));
834+
}
827835
};
828836

829837
static serialization_test_suite x;

0 commit comments

Comments
 (0)