【发布时间】:2021-05-11 14:59:00
【问题描述】:
我希望将此公开信/Google 套件工作流程 (https://jeffreyeverhart.com/2020/05/18/open-letter-maker-with-google-forms-docs-and-apps-script/) 与需要某种形式批准的额外批准步骤结合起来(例如,Google 表格中的复选框,或某种形式的编辑/更改Google Sheet),然后才将信息传递给最终的 Google Doc。我已经能够使用 Jeffrey Everhart 的代码(见下文)成功设置 Google 表单 -> Google 表格 -> Google Doc 公开信工作流程,该工作流程获取 Google 表单信息并将其添加到表单提交时的 Google Doc 中。但是,尽管昨天一整天都在拔头发,但我无法使脚本在编辑/更改触发器上工作。那将是理想的,因为我可以简单地在 google 工作表中添加一个复选框“批准”列,指示表单提交是否应该继续下一步添加到 Google Doc。有什么建议吗?
function appendSignatureRow(e) {
//Since there could be a bunch of people submitting, we lock the script with each execution
//with a 30 second timeout so nothing gets overwritten
const lock = LockService.getScriptLock();
lock.waitLock(30000);
//Here we read the variables from the form submission event
const date = new Date(e.values[0]).toLocaleDateString();
//of you can use toLocaleString method if you want the time in the doc
const name = e.values[1];
const affiliation = e.values[2];
const country = e.values[3];
//Next format those values as an array that corresponds to the table row layout
//in your Google Doc
const tableCells = [name, affiliation, country, date]
//Next we open the letter and get its body
const letter = DocumentApp.openById('INSERT ID HERE')
const body = letter.getBody();
//Next we get the first table in the doc and append an empty table row
const table = body.getTables()[0]
const tableRow = table.appendTableRow()
//Here we loop through our table cells from above and add
// a table cell to the table row for each piece of data
tableCells.forEach(function(cell, index) {
let appendedCell = tableRow.appendTableCell(cell)
})
//here we save and close our letter and then release a lock
letter.saveAndClose();
lock.releaseLock();
}
Here's the Google Sheet format I'm using, 列 F 是我希望在编辑触发器上添加批准复选标记的列,然后触发脚本在“const letter = DocumentApp.openById('INSERT ID HERE')”行中获取。
我是 Google 应用脚本的新手,非常感谢您的帮助!
【问题讨论】:
-
如果您希望通过脚本编辑电子表格以触发 onEdit,那么您将等待很长时间,因为 onEdit 触发器仅在用户编辑时触发。
-
但是,如果您说您希望将数据发布到电子表格,但将写入文档的操作延迟到用户选中复选框之后,那么这将起作用,但您可能需要使用可安装的触发器,因为创建和保存新文档需要权限。
-
啊,是的,我希望实现的是后者。我想延迟写入文档的操作,直到用户/管理员选中电子表格中的复选框。但是我已经尝试了可安装的“编辑时”触发器,但没有任何运气——当我在 Apps 脚本网站上使用“选择事件类型”->“编辑时”选项时,绝对没有任何反应。
-
onEdit 触发器的事件对象中没有 evenType。寻找自己:developers.google.com/apps-script/guides/triggers/events
标签: google-apps-script google-sheets automation google-forms