0

Is it possible to have triggers that work for 2 spreadsheets (2, independent files) with one script? I am stuck at how to do this part, as it needs to be automated. So far in my research I have seen importrange triggers but nothing that can do a prompt on a specific worksheet.


My goal is to make a "list veto" function with the following requirements:

  1. Must use google spreadsheets a) One Sheet with "triggers" that is the "controller" or moderator b) One Sheet for the "preferred" elector c) One Sheet for the "secondary" elector

  2. The list will be dynamic, but for this example let us say it consists of a list of 15 locations.

  3. Both "electors" will have to veto continuously until there is only 8 locations left.

  4. The "controller" will trigger a system that asks the "preferred elector" first, then the "secondary," by a prompt that goes to their respective spreadsheet. Once the preferred makes a choice, the list of locations is deducted, then a prompt goes to the worksheet of the "secondary."

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • You may need to create a web apps with the 'controller', and do post request from the 2 spreadsheets to the 'controller' with `onEdit(e)` – idfurw Aug 09 '21 at 03:27

1 Answers1

0

Triggers are set by spreadsheet, not by sheet, but you could use JavaScript control statements like if..else and switch to control when should be executed parts of your script.

Example:

function respondToOnEdit(e){
  var sheetName = e.source.getActiveSheet().getName();
  if(sheetName === 'Sheet1'){
    // do something when Sheet1 is edited
  } else if(sheetName === 'Sheet2'){
    // do something when Sheet2 is edited  
  } else {
    // do something when any other sheet is edited
  }
}

You will have to use code to create an installable trigger for each spreadsheet.

Code snippet:

[id1, id2].forEach(id => {
var spreadsheet = SpreadsheetApp.openById(id);
ScriptApp.newTrigger('respondToOnEdit')
.forSpreadsheet(spreadsheet)
.create()
}

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • I made a mistake in my writeup: I need it to talk to other files, as in other spreadsheets, rather internal sheets to one spreadsheet. Does that make sense? Thank you for the quick reply, though! Looked at what you wrote and referenced it, would def work if it was for sheets only. – Graham Palermo Aug 09 '21 at 23:42
  • @GrahamPalermo Please edit your question to clarify it. – Rubén Aug 09 '21 at 23:43
  • It is already edited above, forgot to mention that, sorry! – Graham Palermo Aug 09 '21 at 23:45
  • Looks like I will need to do some lessons with triggers and graduate to custom ones first. This is way more advanced than what I was anticipating. Do you have any good links to video lessons for anything like this? – Graham Palermo Aug 10 '21 at 01:17