Example #1
0
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

        # Get Overview sheet name from active sheet or from provided kwargs.
        activeOverviewName = kwargs.get('activeSheet',
                                        helper.getActiveSheetName())
        # Get view name for the Details. This is presented in Overview sheet name inside parentheses.
        viewName = names.getViewName(activeOverviewName)
        detailsSheetName = names.getDetailsName(viewName)
        previousDetails = Details(viewName)
        if Sheet.hasByName(detailsSheetName):
            # Check if user wants to update existing Details-sheet.
            if not message_box.showSheetUpdateWarning():
                return
            previousDetails = Details.fromSheet(detailsSheetName)
        modifiersSheet = Modifiers(MODIFIER_LIST_SHEET_NAME)
        parentOverview = Overview.fromSheet(activeOverviewName)
        # Create new Details sheet by combining new and existing data.
        newDetails = UpdateDetails.update(modifiersSheet, parentOverview,
                                          previousDetails, viewName)
        # Delete previous Details-sheet and generate a new one.
        Sheet.deleteSheetByName(detailsSheetName)
        formatter = DetailsFormatter(newDetails, parentOverview)
        unoDetailsSheet = formatter.generate()
        # Make columns width optimal.
        length = cursor.getWidth(unoDetailsSheet)
        format.setOptimalWidthToRange(unoDetailsSheet, 0, length)
        # Generate data validation.
        validation.setDataValidationToDetailsSheet(unoDetailsSheet, viewName)
        # Generate named ranges.
        about = About(ABOUT_SHEET_NAME)
        if about.isGenerateNamedRangesOn():
            NamedRanges(unoDetailsSheet, 0, viewName).generate()
        # Generate cell styles used for details sheet.
        UpdateStyles.update()
        # Create conditional format ranges to the new details sheet which uses
        # styles created above.
        masterSheet = Master(MASTER_LIST_SHEET_NAME)
        resultsSheet = Results(RESULT_LIST_SHEET_NAME)
        inputSheet = Inputs(INPUT_LIST_SHEET_NAME)
        conditionalFormat.createDetailsConditionalFormats(
            unoDetailsSheet, masterSheet, resultsSheet, inputSheet, viewName)
        # Set new sheet as currently active sheet.
        helper.setActiveSheet(unoDetailsSheet)
    except errors.MovelisterError as e:
        helper.setActiveSheet(e.activeSheet)
        message_box.showWarningWithOk(str(e))
Example #2
0
def updateOverview(*args, **kwargs):
    """
    A macro function to update or create a new Overview sheet. Updated sheet
    will include earlier user data in the old Overview if any.
    """
    try:
        if not Sheet.checkTemplatesExists():
            message_box.showWarningWithOk(
                'This file doesn\'t seem to have all necessary templates. Can\'t generate.'
            )
            return

        # Get name of the Overview which user wants to generate.
        masterSheet = Master(MASTER_LIST_SHEET_NAME)
        activeSheetName = kwargs.get('activeSheet', '')
        if activeSheetName == '':
            viewName = masterSheet.getOverviewName()
            overviewSheetName = names.getOverviewName(viewName)
        else:
            viewName = names.getViewName(activeSheetName)
            overviewSheetName = activeSheetName

        # Some error checking.
        if not viewName:
            message_box.showWarningWithOk(
                'You can\'t generate an Overview if no View-name is set in cell C1.'
            )
            return

        if viewName not in masterSheet.getViewNames():
            message_box.showWarningWithOk(
                'You can\'t generate a View that has no Actions. Make sure at least one '
                +
                'Action uses the View-name written in cell C1 before continuing.'
            )
            return

        oldOverview = Overview(viewName)
        # If document has existing Overview, then that is set as previous instead.
        if Sheet.hasByName(overviewSheetName):
            # Check if user wants to update existing Overview.
            if not message_box.showSheetUpdateWarning():
                return
            oldOverview = Overview.fromSheet(overviewSheetName)

        newOverview = UpdateOverview.update(oldOverview, viewName)

        # Place new Overview sheet on the same position as the previous one. If previous one
        # does not exist, then place right of the Master sheet instead.
        position = Sheet.getPosition(overviewSheetName)
        if not position:
            position = Sheet.getPosition(MASTER_LIST_SHEET_NAME) + 1
        # Delete old sheet if exists.
        Sheet.deleteSheetByName(overviewSheetName)
        # Generate a new one.
        formatter = OverviewFormatter(newOverview)
        unoOverviewSheet = formatter.generate(position)
        # Make columns width optimal.
        length = cursor.getWidth(unoOverviewSheet)
        format.setOptimalWidthToRange(unoOverviewSheet, 0, length)
        # Fix sheet colors.
        formatter.setOverviewModifierColors(overviewSheetName)
        # Set new sheet as currently active sheet.
        helper.setActiveSheet(unoOverviewSheet)
    except errors.MovelisterError as e:
        helper.setActiveSheet(e.activeSheet)
        message_box.showWarningWithOk(str(e))