简体   繁体   English

在当前工作簿中没有的工作表上运行 Apps 脚本 - Google Sheets / Apps 脚本

[英]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.

相关问题 使用 For 循环运行 Apps 脚本 - Google 表格 / Apps 脚本 - Use A For Loop to Run Apps Script - Google Sheets / Apps Script 如何在隐藏工作表上运行Google Apps脚本 - How to run a Google Apps Script on a hidden sheet 使用 Google Apps 脚本在 Google 表格中创建新表格 - Create a new sheet in a Google Sheets with Google Apps Script 使用Google Apps脚本从Google表格打印选择表格 - Printing select sheets from google sheet with Google Apps Script 使用 Google 表格上的 Google Apps 脚本将多个电子表格导入 1 张表格 - Import Multiple Spreadsheets into 1 Sheet using Google Apps Script on Google Sheets 谷歌表格应用脚本在按钮点击时下载表格 as.xlsx - Google Sheets Apps Script to download sheet as .xlsx on button click 在Apps脚本中使用相同工作表的Google表格API - Using Google Sheets API from within Apps Script for same Sheet 使用Google Apps脚本在Google表格中突出显示当前用户的名称 - Highlight a name of current user in Google Sheets using Google Apps Script Apps Script (Google Sheet) 不允许我运行脚本 - Apps Script (Google Sheet) not allowing me to Run Script 运行包含 If 语句的应用程序脚本代码(如何自动触发?) - Google Apps Script / Google Sheets - Run an apps script code containing an If Statement (How To Trigger Automatically ?) - Google Apps Script / Google Sheets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM