1

I would like to know if there's a way to automatically trigger a script every time a specific sheet, inside a worksheet, is selected.

Looking around I found the onSelectionChange function but I have not been able to use it for my scope. Script draft below.

function onSelectionChange(e) 
{ // Run update script when sheet is selected 
 var sheet = e.sheet; 
 var ssname = SpreadsheetApp.getActiveSheet().getName(); 
 if(ssname === "Projects") {
    UpdateProjects(); 
    }
} 

Any suggestion would be much appreciated. Thanks!

Rubén
  • 34,714
  • 9
  • 70
  • 166
Andre8426
  • 25
  • 4

1 Answers1

1

I believe your goal as follows.

  • You want to run a script when the specific sheets are activated.
  • You want to achieve this using Google Apps Script.

I thought that in this case, your goal can be achieve by modifying the sample script of this thread.

Usage

  1. Please copy and paste the following sample script to the container-bound script of Google Spreadsheet, and save the script.

  2. Please reopen the Google Spreadsheet.

    • By this, onOpen is run and the current sheet is put to PropertiesService.
    • Unfortunately, in the current stage, it seems that the event object of onSelectionChange has no information about the change of tab. So in order to detect the change of tab, I used the PropertiesService.
  3. Then, please select other sheet on Google Spreadsheet.

    • By this, onSelectionChange is run by the onSelectionChange event trigger, and when the activated sheet is included in specificSheetNames, the script of e.source.toast("Run script"); is run.

Sample script:

function onOpen(e) {
  const prop = PropertiesService.getScriptProperties();
  const sheetName = e.range.getSheet().getSheetName();
  prop.setProperty("previousSheet", sheetName);
}

function onSelectionChange(e) {
  const specificSheetNames = ["Projects"]; // Please set the sheet names you want to run the script.

  const prop = PropertiesService.getScriptProperties();
  const previousSheet = prop.getProperty("previousSheet");
  const range = e.range;
  const a1Notation = range.getA1Notation();
  const sheetName = range.getSheet().getSheetName();
  if (!specificSheetNames.includes(sheetName)) return;
  
  // When the specifc sheet names are activated, this script is run.
  e.source.toast("run script"); // This is a sample script.

  prop.setProperty("previousSheet", sheetName);
}
  • In this sample script, when the sheet is changed to "Projects", the script in the if statement is run.

Note:

  • From your script, unfortunately, I cannot understand about UpdateProjects() in your script. In the current stage, all methods cannot be run with onSelectionChange because of the authorization. Because this trigger is the simple trigger. So please be careful this. So as a simple test for checking above script, at first, please use e.source.toast("run script");.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Tanaike, thanks a lot for your reply. I tried the same code you shared but nothing happens. I am wondering if there's a type-o or something else... :/ – Andre8426 Feb 16 '21 at 06:47
  • @Andre8426 Thank you for replying. I apologize for the inconvenience. Unfortunately, when I open the sheet to "Projects" by testing this sample script, the sample script of `e.source.toast("run script")` is run. So I cannot replicate your situation. I apologize for this. For example, before you use this script, please confirm whether the same function names are NOT existing in your Google Apps Script project. – Tanaike Feb 16 '21 at 08:26
  • @Andre8426 And, please reopened the Google Spreadsheet. By this, the initial sheet name is saved. And then, please open the sheet of "Projects". By this, you can confirm that the sample script is run. If you tested this flow and the sample script is not run, can you provide your current Google Spreadsheet including the current script? By this, I would like to confirm your situation. – Tanaike Feb 16 '21 at 08:26
  • @Andre8426 By the way, I cannot understand about `I am wondering if there's a type-o or something else`. I deeply apologize for my poor English skill. Can I ask you about the detail of it? – Tanaike Feb 16 '21 at 08:27
  • Tanaike, thanks again. Sorry, I guess the script was not working for a fault on my side. I should implement all scripts present in this worksheet to prevent lag or long loading times. Answering your question, the script that should trigger is the one shown in this p: https://stackoverflow.com/questions/66194924/cannot-read-property-indexof-of-undefined/66196623?noredirect=1#comment117035752_66196623 – Andre8426 Feb 16 '21 at 09:20
  • @Andre8426 Thank you for replying. I have to apologize for my poor English skill. Unfortunately, I cannot understand about `I guess the script was not working for a fault on my side` and `I should implement all scripts present in this worksheet to prevent lag or long loading times. Answering your question, the script that should trigger is the one shown in this`. Can I ask you about your current issue? From more information, I would like to try to understand it. By the way, when you tested my script using a simple sample script as I said in my answer, what result did you obtain? – Tanaike Feb 16 '21 at 12:37
  • No need to apologize at all. We are all learning (: Busy days recently, didn't have time to make new attempts on this project. Answering your question: The script you gave me worked only one time (I saw the toast on the bottom right corner of the screen). Now it doesn't work anymore, even if I reload the page. The only thing that comes to my mind is that I also have other onOpen and onEdit functions: do you think this could be the problem? should I populate them all together in a single function? Is there any other solution, if this is the problem? Thanks again Tanaike. – Andre8426 Feb 18 '21 at 15:02
  • 1
    @Andre8426 Thank you for replying. I apologize for the inconvenience. About `The only thing that comes to my mind is that I also have other onOpen and onEdit functions: do you think this could be the problem? should I populate them all together in a single function?`, when you created new Spreadsheet and put my sample script and you use the script, what result will you obtain? From this result, how about thinking about your issue? Because your this question is for this situation. – Tanaike Feb 18 '21 at 23:38