0
votes

I would like to be able to run a script in another spreadsheet outside of the current spreadsheet I am in.

I tried the below script but the script is not in the current spreadsheet editor

 function RunScriptInAnotherSpreadsheet() {

 var target = SpreadsheetApp.openById("sheetID");

//runs the below script
 relocationtomaster();

 }

am I dreaming ?

I understand a trigger would do that in the target sheet - just wanted to do it manually

any help would be appreciated

2

2 Answers

2
votes

Publish the main script(with implementation of relocationtomaster) as library. Then add this library as dependency to the script you want this code run.

Function relocationtomaster will be available in context of dependant script.

Example main script(publish as Library)

function relocationtomaster(id){
  var ss = SpreadsheetApp.open(id);

  Logger.log(ss.getActiveSheet().getDataRange().getValues())

}

Example dependant script(include above script as library)

function run(){
  relocationtomaster('1MXsIX_SprLSimqNDUlDkEvWhtQp8Kz0By1IaA5JfkSA'); // Sheet id
}

You can pass spreadsheetid as parameter.

1
votes

AFAIK you have to publish them as a Library and call it from there, you can have a look here how to run google app script function from another project

Mind that that id could affect to the performance of the script.