0
votes

I have a script that I want to run onOpen when only certain sheets are open, but not others in the spreadsheet to which the script is bound. The sheets that are needed all have names "Agent report - 'NameOfAgent'". Each agent report also has certain cells inside that can be used for the IF condition statement. Logically, I want to have this:

IF name of active sheet contains "Agent report" -> run script ELSE do nothing

Alternatively, IF in active sheet cell A2=="Role" -> run script ELSE do nothing

Thank you for any help.

1
Won't the active sheet onOpen be always the first sheet? Also show what you've tried so farTheMaster
It's not necessarily always the same sheet though. Use a intermediate function and wrap your logic around that.Cooper
Questions that already were answered do not be changed in a way that it invalidates the current answers, instead you should post a new question. Considering this, I will revert the last edit made by the OP.Rubén

1 Answers

2
votes

onOpen will be executed when the owner or the editor open the spreadsheet no matter of what sheet was referred in the url parameters but you could include an if statement on i to control what is done when the condition is met.

/**
 * This will be executed always that the owner or an editor opens the spreadsheet
 *
 */
function onOpen(e){
  if(SpreadsheetApp.getActiveSheet().getName() === 'Agent report'){
    // This will be executen when the condition is true
    doSomething();
  } else {
    // This will be executen when the condition is false
    return; // This is actually not necessary as there isn't any statement after it.
  }
}

/**
 * Function to be called when the condition is met
 *
 */
function doSomething(){
  // do something
}

Related