def testSingletonServiceManager(self): """ Test that returned service manager is a singleton object. """ serviceManager1 = Context.getServiceManager() serviceManager2 = Context.getServiceManager() self.assertEqual(id(serviceManager1), id(serviceManager2))
def deleteSheetByName(cls, name): """ Delete sheet from the document by given name. Return true on success, false otherwise. """ try: Context.getDocument().Sheets.removeByName(name) return True except NoSuchElementException: return False
def newOverview(cls, name, position): """ Insert new Overview sheet with given name to the given position. Returns created sheet UNO object. Sheet with given name must not exist, otherwise RuntimeException is raised. """ Context.getDocument().Sheets.copyByName(OVERVIEW_TEMPLATE_NAME, name, position) return cls.getByPosition(position)
def newDetails(cls, overviewName, name): """ Insert new Details sheet with given name right of the given overview sheet. Sheet with given name must not exist, otherwise RuntimeException is raised. """ position = cls.getPosition(overviewName) + 1 if position == None: return None Context.getDocument().Sheets.copyByName(DETAILS_TEMPLATE_NAME, name, position) return cls.getByPosition(position)
def getActiveSheetName(): """ Return currently active sheet name. """ document = Context.getDocument() activeSheet = document.getCurrentController().getActiveSheet() return activeSheet.getName()
def reopenCurrentFile(): """ Reopens current file. All variables made before this will be invalid. Make sure to initialize them too. """ frame = Context.getFrame() frame.loadComponentFromURL(getUrl(), "_self", 0, ())
def getUrl(): """ Returns current opened file URL. This can be used to reopen the same file. URL format for a file is: "file:///path_to_project/movelister/templates/movelister_template.ods" """ return Context.getFrame().getController().getModel().getURL()
def getCurrentSelection(): """ This function just gets the current active Selection and returns it. """ model = Context.getDocument() selection = model.getCurrentSelection() return selection
def testNewSheetRightOf(self): rightOfName = MASTER_LIST_SHEET_NAME name = 'test sheet' sheet = Sheet.newSheetRightOf(rightOfName, name) self.assertEqual(sheet.getName(), name) names = Context.getDocument().Sheets.getElementNames() index = names.index(rightOfName) self.assertEqual(names.index(name), index + 1)
def testNewSheetLeftOf(self): leftOfName = MODIFIER_LIST_SHEET_NAME name = 'test sheet' sheet = Sheet.newSheetLeftOf(leftOfName, name) self.assertEqual(sheet.getName(), name) names = Context.getDocument().Sheets.getElementNames() index = names.index(leftOfName) self.assertEqual(names.index(name), index - 1)
def __init__(self, UnoSheet, column, viewName): self.sheet = UnoSheet self.sheetName = self.sheet.Name self.column = column self.viewName = viewName self.namedRanges = Context.getDocument().NamedRanges self.sheetData = cursor.getColumn(self.sheet, self.column) self.cellAddress = self.sheet.getCellByPosition(1, 1).getCellAddress()
def testGetActiveSheetName(self): """ This test checks if active Sheet name is successfully acquired by this Helper-function. Note: this test requires that the template sheet opens with Master List active. """ document = Context.getDocument() activeSheet = document.getCurrentController().getActiveSheet() name = helper.getActiveSheetName() self.assertEqual(name, 'Inputs')
def setUpClass(cls): super().setUpClass() # Get LibreOffice executable from env variable. libreOffice = os.environ.get('MV_LB_BIN', 'soffice') port = os.environ.get('MV_LB_PORT', 8080) # Open LibreOffice process differently if platform is Windows. system = platform.system() if system == 'Windows': cls.process = Popen(libreOffice + 'templates\movelister_test.ods \ --norestore --accept=socket,host=127.0.0.1,port={0};urp'. format(port)) else: cls.process = Popen([ libreOffice, 'templates/movelister_test.ods', '--headless', '--norestore', '--accept=socket,host=127.0.0.1,port={0};urp'.format(port) ]) # Reset and setup context. Context.reset() Context.setup(host='127.0.0.1', port=port)
def createMessage(type, titleText, messageText): model = Context.getDocument() window = model.CurrentController.Frame.ContainerWindow if type == 'OK': box = window.getToolkit().createMessageBox( window, MESSAGEBOX, MessageBoxButtons.BUTTONS_OK, titleText, messageText) if type == 'YES_NO': box = window.getToolkit().createMessageBox( window, MESSAGEBOX, MessageBoxButtons.BUTTONS_YES_NO, titleText, messageText) result = box.execute() if result == MessageBoxResults.OK: return 'OK' if result == MessageBoxResults.YES: return 'YES' if result == MessageBoxResults.NO: return 'NO'
def getNameRanges(): return Context.getDocument().NamedRanges.getElementNames()
def getSheetNames(cls): """ Return list of sheet names in current document. """ return Context.getDocument().Sheets.getElementNames()
def getMasterSheet(cls): return Context.getDocument().Sheets.getByName(MASTER_LIST_SHEET_NAME)
def getCellStyleFamily(): document = Context.getDocument() return document.getStyleFamilies().getByName('CellStyles')
def addCellStyle(name, color): if not isinstance(color, Color): raise TypeError('given color is not instance of Color') cellStyle = Context.createInstance('com.sun.star.style.CellStyle') getCellStyleFamily().insertByName(name, cellStyle) cellStyle.setPropertyValue('CellBackColor', color.value)
def getInputSheet(cls): return Context.getDocument().Sheets.getByName(INPUT_LIST_SHEET_NAME)
def getModifierSheet(cls): return Context.getDocument().Sheets.getByName(MODIFIER_LIST_SHEET_NAME)
def getResultSheet(cls): return Context.getDocument().Sheets.getByName(RESULT_LIST_SHEET_NAME)
def newSheet(cls, name, position): """ Creates new sheet with given name to given position and returns it. """ Context.getDocument().Sheets.insertNewByName(name, position) return cls.getByName(name)
def getAboutSheet(cls): return Context.getDocument().Sheets.getByName(ABOUT_SHEET_NAME)
from movelister.sheet import helper # noqa from movelister.sheet.about import About # noqa from movelister.sheet.details import Details # noqa from movelister.sheet.master import Master # noqa from movelister.sheet.modifiers import Modifiers # noqa from movelister.sheet.overview import Overview # noqa from movelister.sheet.results import Results # noqa from movelister.sheet.inputs import Inputs from movelister.sheet.sheet import Sheet, MASTER_LIST_SHEET_NAME # noqa from movelister.sheet.sheet import MODIFIER_LIST_SHEET_NAME, ABOUT_SHEET_NAME, RESULT_LIST_SHEET_NAME, INPUT_LIST_SHEET_NAME # noqa from movelister.ui import message_box # noqa from movelister.utils import format, validation, version # noqa # Setup context automatically when macro is run from the LibreOffice. if __name__ != '__main__': Context.setup() def updateDetails(*args, **kwargs): """ A macro function that will update one Details-sheet while preserving previous user data. Movelister can have multiple Details-views. To determine which one is updated, the code checks which Overview button was pressed to trigger the macro. """ try: if not Sheet.checkTemplatesExists(): message_box.showWarningWithOk( 'This file doesn\'t seem to have all necessary templates. Can\'t generate.' ) return
def getByName(cls, name): """ Get sheet by given name, if not found raise NoSuchElementException. """ return Context.getDocument().Sheets.getByName(name)
def getByPosition(cls, position): """ Return sheet by given position. """ return Context.getDocument().Sheets.getByIndex(position)
def setActiveSheet(unoSheet): """ Set currently active sheet to given UNO object presenting sheet. """ document = Context.getDocument() document.getCurrentController().setActiveSheet(unoSheet)