diff options
author | Alain Romedenne <alain.romedenne@libreoffice.org> | 2023-11-23 17:34:04 +0100 |
---|---|---|
committer | Hossein <hossein@libreoffice.org> | 2024-02-22 09:10:33 +0100 |
commit | 9fdf0913b6b7ac38e5ea9eff19945e127be89339 (patch) | |
tree | 8f4e1f445865f6e2f79359fc4693d27665210a5a /odk | |
parent | b863b599c751b28dde2d15188a9ed7cbce4c158a (diff) |
SDK - 1st Steps example code enhancement
Python example extended in order to be equally callable from LibO client
Change-Id: Ib2d733809e960971b98c30cbd6368ebc755dd7ab
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/159767
Tested-by: Jenkins
Reviewed-by: Hossein <hossein@libreoffice.org>
Diffstat (limited to 'odk')
-rw-r--r-- | odk/Package_examples.mk | 1 | ||||
-rw-r--r-- | odk/examples/DevelopersGuide/FirstSteps/FirstLoadComponent/python/DualComponentLoader.py | 70 |
2 files changed, 71 insertions, 0 deletions
diff --git a/odk/Package_examples.mk b/odk/Package_examples.mk index eb6c3563c5f5..9d84dc6620de 100644 --- a/odk/Package_examples.mk +++ b/odk/Package_examples.mk @@ -184,6 +184,7 @@ $(eval $(call gb_Package_add_files_with_dir,odk_examples,$(SDKDIRNAME)/examples, DevelopersGuide/FirstSteps/FirstLoadComponent/java/Makefile \ DevelopersGuide/FirstSteps/FirstLoadComponent/basic/FirstLoadComponent.bas \ DevelopersGuide/FirstSteps/FirstLoadComponent/python/FirstLoadComponent.py \ + DevelopersGuide/FirstSteps/FirstLoadComponent/python/DualComponentLoader.py \ DevelopersGuide/FirstSteps/HelloTextTableShape/java/HelloTextTableShape.java \ DevelopersGuide/FirstSteps/HelloTextTableShape/java/Makefile \ DevelopersGuide/FirstSteps/HelloTextTableShape/basic/HelloTextTableShape.bas \ diff --git a/odk/examples/DevelopersGuide/FirstSteps/FirstLoadComponent/python/DualComponentLoader.py b/odk/examples/DevelopersGuide/FirstSteps/FirstLoadComponent/python/DualComponentLoader.py new file mode 100644 index 000000000000..b154ae306080 --- /dev/null +++ b/odk/examples/DevelopersGuide/FirstSteps/FirstLoadComponent/python/DualComponentLoader.py @@ -0,0 +1,70 @@ +# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# + +import uno +import officehelper +import sys +import traceback +from com.sun.star.sheet.CellFlags import FORMULA + +def main(ctx=None): + try: + if ctx is None: # Execution triggered via Office client + ctx = uno.getComponentContext() + if ctx is None: + print("ERROR: Python UNO runtime is absent.") + sys.exit(1) + srv_mgr = ctx.getServiceManager() + desktop = srv_mgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx) + spreadsheet_component = desktop.loadComponentFromURL("private:factory/scalc", "_blank", 0, tuple()) + spreadsheets = spreadsheet_component.getSheets() + spreadsheets.insertNewByName("MySheet", 0) + elem_type = spreadsheets.getElementType() + print(elem_type) + sheet = spreadsheets.getByName("MySheet") + cell = sheet.getCellByPosition(0, 0) + cell.setValue(21) + cell = sheet.getCellByPosition(0, 1) + cell.setValue(21) + cell = sheet.getCellByPosition(0, 2) + cell.setFormula("=sum(A1:A2)") + + cell.setPropertyValue("CellStyle", "Result") + + spreadsheet_controller = spreadsheet_component.getCurrentController() + spreadsheet_controller.setActiveSheet(sheet) + cell.setPropertyValue("VertJustify", "com.sun.star.table.CellVertJustify.TOP") + formula_cells = sheet.queryContentCells(FORMULA) + formulas = formula_cells.getCells() + formula_enum = formulas.createEnumeration() + + while formula_enum.hasMoreElements(): + formula_cell = formula_enum.nextElement() + print("Formula cell in column " + str(formula_cell.getCellAddress().Column) + + ", row " + str(formula_cell.getCellAddress().Row) + + " contains " + cell.getFormula()) + + except Exception as e: + print(e) + traceback.print_exc() + sys.exit(1) + +# Entry point for LO client via +# Tools - Macros - Run macro... menu +g_exportedScripts = (main,) + +# Entry point for Python console or Python IDEs +if __name__ == "__main__": + remote_context = officehelper.bootstrap() + if remote_context is None: + print("ERROR: Could not bootstrap default Office.") + sys.exit(1) + main(remote_context) + +# vim: set shiftwidth=4 softtabstop=4 expandtab: |