From 2c61f2b9f2178a3cee0a59dee4a18f542a6f6b13 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Mon, 29 Jun 2020 09:38:04 -0400 Subject: [PATCH] reports: Add BaseODS.set_open_sheet() method. --- conservancy_beancount/reports/core.py | 15 +++++++++++++++ tests/test_reports_spreadsheet.py | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/conservancy_beancount/reports/core.py b/conservancy_beancount/reports/core.py index a7a80af..0209fcf 100644 --- a/conservancy_beancount/reports/core.py +++ b/conservancy_beancount/reports/core.py @@ -952,6 +952,21 @@ class BaseODS(BaseSpreadsheet[RT, ST], metaclass=abc.ABCMeta): self.set_config(config_map, 'VerticalSplitMode', 2, 'short') self.set_config(config_map, 'VerticalSplitPosition', 1, 'short') + def set_open_sheet(self, sheet: Union[str, odf.table.Table, None]=None) -> None: + """Set which sheet is open in the document + + When the user first opens the spreadsheet, their view will be on this + sheet. You can provide a sheet name string or sheet object. With no + argument, defaults to ``self.sheet``. + """ + if sheet is None: + sheet = self.sheet + if not isinstance(sheet, str): + sheet = sheet.getAttribute('name') + if not isinstance(sheet, str): + raise ValueError("sheet argument has no name for setting") + self.set_config(self.view, 'ActiveTable', sheet, 'string') + def use_sheet(self, name: str) -> odf.table.Table: """Switch the active sheet ``self.sheet`` to the one with the given name diff --git a/tests/test_reports_spreadsheet.py b/tests/test_reports_spreadsheet.py index a34bfde..a771c08 100644 --- a/tests/test_reports_spreadsheet.py +++ b/tests/test_reports_spreadsheet.py @@ -479,6 +479,24 @@ def test_ods_lock_first_cells(ods_writer, method_name, split_name, side_name): assert child.getAttribute('type') == ctype assert child.firstChild.data == value +@pytest.mark.parametrize('arg', [ + None, + 'Target Sheet', + odf.table.Table(name='Target Sheet'), +]) +def test_ods_set_open_sheet(ods_writer, arg): + ods_writer.use_sheet('Start Sheet' if arg else 'Target Sheet') + ods_writer.set_open_sheet(arg) + view_settings = get_child( + ods_writer.document.settings, + odf.config.ConfigItemSet, + name='ooo:view-settings', + ) + views = get_child(view_settings, odf.config.ConfigItemMapIndexed, name='Views') + view1 = get_child(views, odf.config.ConfigItemMapEntry, index=0) + actual = get_child(view1, odf.config.ConfigItem, name='ActiveTable') + assert actual.text == 'Target Sheet' + @pytest.mark.parametrize('style_name', XML_NAMES_LIST) def test_ods_writer_add_row(ods_writer, style_name): cell1 = ods_writer.string_cell('one')