<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def doWorksheetLogic(base, action, analysis):
 """ This function checks if the actions contains worksheet actions. There is a selection list in each action section. This select has the following options and consequence. 1) "To the current worksheet" (selected by default) 2) "To another worksheet" 3) "Create another worksheet" 4) "No worksheet" - If option 1) is selected, the Analyst selection list will not be displayed. Since the action doesn't require to add the new analysis to another worksheet, the function will try to add the analysis to the same worksheet as the base analysis. If the base analysis is not assigned in a worksheet, no worksheet will be assigned to the new analysis. - If option 2) is selected, the Analyst selection list will be displayed. - If option 2) is selected and an analyst has also been selected, then the system will search for the latest worksheet in status "open" for the selected analyst and will add the analysis in that worksheet (the system also searches for the worksheet template if defined). If the system doesn't find any match, another worksheet assigned to the selected analyst and with the analysis must be automatically created. - If option 2) is selected but no analyst selected, then the system will search for the latest worksheet in the status "open" regardless of the analyst assigned and will add the analysis in that worksheet. If there isn't any open worksheet available, then go to option 3) - If option 3) is selected, a new worksheet with the defined analyst will be created. If no analyst is defined for the original analysis, the system will create a new worksheet and assign the same analyst as the original analysis to which the rule applies. If the original analysis doesn't have assigned any analyst, the system will assign the same analyst that was assigned to the latest worksheet available in the system. If there isn't any worksheet created yet, use the first active user with role "analyst" available. - if option 4) the Analyst selection list will not be displayed. The analysis (duplicate, repeat, whatever) will be created, but not assigned to any worksheet, so it will stay "on queue", assigned to the same Analysis Request as the original analysis for which the rule has been triggered. """

    otherWS = action.get('otherWS', False)
    worksheet_catalog = getToolByName(base, CATALOG_WORKSHEET_LISTING)
    if otherWS in ['to_another', 'create_another']:
        # Adds the new analysis inside same worksheet as the previous analysis.
        # Checking if the actions defines an analyst
        new_analyst = action.get('analyst', '')
        # Checking if the action defines a worksheet template
        worksheettemplate = action.get('worksheettemplate', '')
        # Creating the query
        contentFilter = {
            'review_state': 'open',
            'sort_on': 'created',
            'sort_order': 'reverse'}
        # If a new analyst is defined, add the analysis to the first
        # analyst's worksheet
        if new_analyst:
            # Getting the last worksheet created for the analyst
            contentFilter['Analyst'] = new_analyst
        if worksheettemplate:
            # Adding the worksheettemplate filter
            contentFilter['getWorksheetTemplateUID'] = worksheettemplate
        # Run the filter
        wss = worksheet_catalog(contentFilter)
        # 'repeat' actions takes advantatge of the 'retract' workflow action.
        # the retract process assigns the new analysis to the same worksheet
        # as the base analysis, so we need to desassign it now.
        ws = analysis.getWorksheet()
        if ws:
            ws.removeAnalysis(analysis)
        # If worksheet found and option 2
        if len(wss) > 0 and otherWS == 'to_another':
            # Add the new analysis to the worksheet
            wss[0].getObject().addAnalysis(analysis)
        # No worksheet found, but option 2 or 3 selected:
        elif new_analyst:
            # Create a new worksheet and add the analysis to it
            ws = _createWorksheet(base, worksheettemplate, new_analyst)
            ws.addAnalysis(analysis)
        elif not new_analyst:
            # Getting the original analysis to which the rule applies
            previous_analysis = analysis.getReflexAnalysisOf()
            # Getting the worksheet of the analysis
            prev_ws = previous_analysis.getWorksheet()
            # Getting the analyst from the worksheet
            prev_analyst = prev_ws.getAnalyst() if prev_ws else ''
            # If the previous analysis belongs to a worksheet:
            if prev_analyst:
                ws = _createWorksheet(base, worksheettemplate, prev_analyst)
                ws.addAnalysis(analysis)
            # If the original analysis doesn't have assigned any analyst
            else:
                # assign the same analyst that was assigned to the latest
                # worksheet available
                prev_analyst = wss[0].getObject().getAnalyst() if wss else ''
                if prev_analyst:
                    ws = _createWorksheet(base, worksheettemplate, prev_analyst)
                    ws.addAnalysis(analysis)

    elif otherWS == 'current':
        # Getting the base's worksheet
        ws = base.getWorksheet()
        if ws:
            # If the old analysis belongs to a worksheet, add the new
            # one to it
            ws.addAnalysis(analysis)
    # If option 1 selected and no ws found, no worksheet will be assigned to
    # the analysis.
    # If option 4 selected, no worksheet will be assigned to the analysis
    elif otherWS == 'no_ws':
        ws = analysis.getWorksheet()
        if ws:
            ws.removeAnalysis(analysis)