编辑 SpreadsheetApp 时触发 Google DocumentApp 脚本
Trigger Google DocumentApp Script Upon Edit of SpreadsheetApp
使用 Google Apps 脚本,有没有办法让 Google Documents 文件在编辑 Google Sheets 文件时自动更新?
我有一个 Google DocumentApp 文件,其中包含一个从 Google SpreadsheetApp 文件获取数据的脚本。我希望创建一个脚本,以便在编辑 SpreadsheetApp 文件时自动更新 DocumentApp 文件。
这是我目前使用的代码:
function updateDocumentOnEditTrigger() {
var ss = SpreadsheetApp.openById(SheetID);
ScriptApp.newTrigger('UpdateDocument')
.forSpreadsheet(ss)
.onEdit()
.create();
}
运行 updateDocumentOnEditTrigger
函数似乎没有触发 UpdateDocument
函数,它在手动 运行.
时可以正常工作
答案:
为了 运行 编辑电子表格的 DocumentApp
脚本,必须使用附加到电子表格的 On edit
可安装触发器。
更多信息:
根据简单触发器文档,需要考虑一些限制。特别是:
They can modify the file they are bound to, but cannot access other files because that would require authorization.
因此,无法使用onEdit(e)
触发功能。但是,可以创建一个可安装的触发器,设置设置使其可以在编辑时触发。
代码:
在绑定到电子表格文件的脚本中使用以下函数:
function updateDocument() {
var doc = DocumentApp.openById('DOCUMENT_ID');
// here you can put your code that edits the document in the way you want.
}
您可以创建一个可安装的触发器,它 运行 在编辑电子表格时使用。在设置触发器之前,您需要至少 运行 代码一次 - 这是因为 DocumentApp
需要授权,您需要授予它!
###设置可安装触发器:
设置代码后,您可以通过完成以下步骤来创建可安装触发器:
在绑定电子表格脚本的 Apps 脚本编辑器视图中,遵循路径 Edit > Current project's triggers
。这将在新选项卡或 window.
中打开项目的触发器
在左下角,单击 + Add Trigger
按钮,调出 Add Trigger
模式。在这里,使用以下属性设置触发器:
- 选择要运行的函数:
updateDocument
- 选择应 运行 的部署:
Head
- Select 事件来源:
From spreadsheet
- Select 事件类型:
On edit
然后点击Save
。这将设置触发器,以便每次编辑电子表格时您的文档编辑功能都会 运行。
参考文献:
使用 Google Apps 脚本,有没有办法让 Google Documents 文件在编辑 Google Sheets 文件时自动更新?
我有一个 Google DocumentApp 文件,其中包含一个从 Google SpreadsheetApp 文件获取数据的脚本。我希望创建一个脚本,以便在编辑 SpreadsheetApp 文件时自动更新 DocumentApp 文件。
这是我目前使用的代码:
function updateDocumentOnEditTrigger() {
var ss = SpreadsheetApp.openById(SheetID);
ScriptApp.newTrigger('UpdateDocument')
.forSpreadsheet(ss)
.onEdit()
.create();
}
运行 updateDocumentOnEditTrigger
函数似乎没有触发 UpdateDocument
函数,它在手动 运行.
答案:
为了 运行 编辑电子表格的 DocumentApp
脚本,必须使用附加到电子表格的 On edit
可安装触发器。
更多信息:
根据简单触发器文档,需要考虑一些限制。特别是:
They can modify the file they are bound to, but cannot access other files because that would require authorization.
因此,无法使用onEdit(e)
触发功能。但是,可以创建一个可安装的触发器,设置设置使其可以在编辑时触发。
代码:
在绑定到电子表格文件的脚本中使用以下函数:
function updateDocument() {
var doc = DocumentApp.openById('DOCUMENT_ID');
// here you can put your code that edits the document in the way you want.
}
您可以创建一个可安装的触发器,它 运行 在编辑电子表格时使用。在设置触发器之前,您需要至少 运行 代码一次 - 这是因为 DocumentApp
需要授权,您需要授予它!
###设置可安装触发器: 设置代码后,您可以通过完成以下步骤来创建可安装触发器:
在绑定电子表格脚本的 Apps 脚本编辑器视图中,遵循路径 Edit > Current project's triggers
。这将在新选项卡或 window.
在左下角,单击 + Add Trigger
按钮,调出 Add Trigger
模式。在这里,使用以下属性设置触发器:
- 选择要运行的函数:
updateDocument
- 选择应 运行 的部署:
Head
- Select 事件来源:
From spreadsheet
- Select 事件类型:
On edit
然后点击Save
。这将设置触发器,以便每次编辑电子表格时您的文档编辑功能都会 运行。