diff options
Diffstat (limited to 'ishtar_common/libreoffice.py')
-rw-r--r-- | ishtar_common/libreoffice.py | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/ishtar_common/libreoffice.py b/ishtar_common/libreoffice.py index 6337675fe..482364a73 100644 --- a/ishtar_common/libreoffice.py +++ b/ishtar_common/libreoffice.py @@ -74,12 +74,22 @@ class UnoCalc(UnoClient): def create_calc(self): return self.create_document('scalc') + def open_calc(self, filename): + return self.get_document(filename) + def save_calc(self, calc, filename): url = "file://{}".format(filename) args = (PropertyValue(Name="FilterName", Value="Calc8"),) calc.storeToURL(url, args) - def get_sheet(self, calc, sheet_index, name=None): + def get_sheet_number(self, calc): + sheets = calc.getSheets() + return sheets.Count + + def get_sheet(self, calc, sheet_index): + return calc.getSheets().getByIndex(sheet_index) + + def get_or_create_sheet(self, calc, sheet_index, name=None): sheets = calc.getSheets() while sheets.Count < (sheet_index + 1): if name and sheets.Count == sheet_index: @@ -89,6 +99,30 @@ class UnoCalc(UnoClient): sheets.insertNewByName(sheet_name, sheets.Count) return calc.getSheets().getByIndex(sheet_index) + def sheet_get_last_row_number(self, sheet): + cursor = sheet.createCursor() + cursor.gotoEndOfUsedArea(False) + return cursor.getRangeAddress().EndRow + 1 + + def sheet_get_data(self, sheet, last_column=None): + if not last_column: + # assume the first line is header with no empty columns + last_column = 0 + has_data = True + while has_data: + has_data = sheet.getCellByPosition(last_column, 0).getString() + if not has_data: + break + last_column += 1 + last_row = self.sheet_get_last_row_number(sheet) + data = [] + for row in range(last_row): + data.append([]) + for col in range(last_column): + value = sheet.getCellByPosition(col, row).getString() + data[-1].append(value) + return data + def create_list(self, sheet, col, start_row, title, items): items = [title] + items row_idx = 0 @@ -147,11 +181,11 @@ class UnoCalc(UnoClient): if not lst: # no list return calc = self.create_calc() - lst_sheet = self.get_sheet(calc, 1, "List types") + lst_sheet = self.get_or_create_sheet(calc, 1, "List types") lst_col, lst_row = 0, 0 end_row = self.create_list(lst_sheet, lst_col, lst_row, title, lst) - main_sheet = self.get_sheet(calc, 0) + main_sheet = self.get_or_create_sheet(calc, 0) self.set_cell_validation_list(main_sheet, 0, 0, 200, lst_sheet, lst_col, [lst_row + 1, end_row]) self.save_calc(calc, "/tmp/test.ods") @@ -162,7 +196,7 @@ class UnoCalc(UnoClient): propval.Value = True filename = "/tmp/main.ods" calc = self.get_document(filename, (propval,)) - sheet = self.get_sheet(calc, 0) + sheet = self.get_or_create_sheet(calc, 0) validation = sheet.getCellByPosition(0, 0).Validation for k in dir(validation): if k.startswith("get"): |