[英]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 .将主脚本(执行relocationtomaster )发布为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. Function relocationtomaster将在依赖脚本的上下文中可用。
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.您可以将电子表格 ID 作为参数传递。
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 AFAIK,您必须将它们发布为库并从那里调用它,您可以在这里查看如何从另一个项目运行 google app script function
Mind that that id could affect to the performance of the script.请注意,该 id 可能会影响脚本的性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.