From 2c61f2b9f2178a3cee0a59dee4a18f542a6f6b13 2020-06-29 13:38:04 From: Brett Smith Date: 2020-06-29 13:38:04 Subject: [PATCH] reports: Add BaseODS.set_open_sheet() method. --- diff --git a/conservancy_beancount/reports/core.py b/conservancy_beancount/reports/core.py index a7a80af867f5083dff946a3ace98c2f39bc43477..0209fcfb94258d0cba3dcf8165db1dfab7030dd1 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 a34bfde13b6a099d8149b0adb310b081af6f52fc..a771c08d7cee5f5b3a59de771b07a41c989daa12 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')