简体   繁体   中英

Run Apps Script on a Sheet not in Current Workbook - Google Sheets / Apps Script

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM