本文介绍了复制模板电子表格脚本时,可在Google电子表格脚本中安装onEdit触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复制模板电子表格的脚本(A).该模板电子表格中包含脚本(B).模板电子表格和脚本已成功通过编程方式复制并与他人共享.模板脚本需要使用onEdit()触发器来修改第三个电子表格(模板电子表格副本中的编辑将同步到第三个电子表格).简单触发器没有权限访问第三个电子表格.我曾尝试使用可安装的触发器,但未能成功使可安装的onEdit触发器在复制的电子表格中工作.

I have a script(A) that copies a template spreadsheet. This template spreadsheet has a script(B) in it. The template spreadsheet and script are successfully programmatically copied and shared with others. The template script needs to use an onEdit() trigger to modify a third spreadsheet (Edits in the copy of the template spreadsheet are going to be synced to the third spreadsheet). A simple trigger does not have permission to access the third spreadsheet. I have tried using installable triggers, but have had no success in getting an installable onEdit trigger to work in the copied spreadsheet.

我想要实现的是在复制的电子表格中具有一个onEdit触发器,该触发器不需要单独的授权即可写入第三张电子表格.

What I want to achieve is to have an onEdit trigger in the copied spreadsheet that does not require separate authorization to write to a third spreadsheet.

所有电子表格均由一个用户拥有.

All spreadsheets are owned by a single user.

我可以使用脚本A中的以下代码在脚本B中创建可安装的触发器.

I can create an installable trigger in script B using the following code in script A.

createSpreadsheetEditTrigger(id_of_shared_spreadsheet)
...
...
function createSpreadsheetEditTrigger(idss) {
var ss = SpreadsheetApp.openById(idss);
ScriptApp.newTrigger('myOnEdit')
      .forSpreadsheet(ss)
      .onEdit()
      .create();
}

"myOnEdit"功能位于脚本B中,并且其中包含与第三张电子表格同步的代码.编辑复制的电子表格时不会触发此操作.

The 'myOnEdit' function is in script B and has in it the code that syncs to the third spreadsheet. This is not triggered when the copied spreadsheet is edited.

如果我通过从简单的onOpen()函数调用脚本B在脚本B中创建可安装触发器,则该触发器由于权限而失败.

If I create the installable trigger in script B by calling it from a simple onOpen() function it fails because of permissions.

遵循以下建议:执行失败:您没有调用getProjectTriggers的权限我可以创建可安装的触发器和一个菜单项来调用它,再加上一个msgbox,提示用户单击安装并授权触发器.

Following the advice here: Execution failed: You do not have permission to call getProjectTriggers I can create the installable trigger and a menu item to call it, plus a msgbox that prompts the user to click on install and authorize the trigger.

在复制有权编辑第三个电子表格的电子表格时,无需用户手动授权触发器,是否可以通过编程方式安装onEdit触发器?

Is it possible to programmatically install an onEdit trigger when copying spreadsheets that has permissions to edit a third spreadsheet without the user having to manually authorize the trigger?

非常感谢

Trevor Storr

Trevor Storr

推荐答案

我刚刚经历过同样的交易,我非常有信心没有授权就没有实现这一目标的简便"方法.

I just went through this same sort of deal, I am pretty confident there is not an "easy" way to make this happen without Authorization.

尽管您可以将"createSpreadsheetEditTrigger"脚本分配给一个按钮,该按钮仅安装触发器并同时授权所有按钮,单击3秒钟和5秒,然后它将按您的预期工作.

Although you can assign your "createSpreadsheetEditTrigger" script to a button that just installs the trigger and authorizes all at the same time, 3 clicks and 5 seconds, then it will work just as you intended.

这篇关于复制模板电子表格脚本时,可在Google电子表格脚本中安装onEdit触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 19:39