0

I need to have a centralized script which is executed by many different sheets. I did some research here and figured out how to do it by using the library method:

  test.Submit();

Where "test" is the library identifier, and Submit() is the function within the library.

I have the issue that Submit() is trying to get the active spreadsheet (the "client" sheet from where the function is called):

function Submit() {
  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet1'); 
  
  sheet.getRange(1,1).setValue("OK!"); }

It seems that getActiveSpreadsheet() does not work (ss is Undefined)

How can I let the main function Submit() know which spreadsheet needs to be edited?

Filippo
  • 320
  • 2
  • 5
  • 22

1 Answers1

0

I was able to make it work by passing the sheet id as parameter.

(initially this was not working because I was not updating the script version. Remember to update the script version when saving!)

"local" sheet script:

  var id = SpreadsheetApp.getActiveSpreadsheet().getId();
  test.Submit(id);

Centralized script:

function Submit(id) {
  
  var ss = SpreadsheetApp.openById(id);
  var sheet = ss.getSheetByName('Sheet1'); 
  
  sheet.getRange(1,1).setValue("OK!"); 
Filippo
  • 320
  • 2
  • 5
  • 22