【问题标题】:Adding approval step for Google Form entries ->Google Sheet/Doc为 Google 表单条目添加批准步骤 -> Google Sheet/Doc
【发布时间】: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


【解决方案1】:

您如何使用 onFormSubmit 为您的 Google 表单中的每个条目添加复选框,并使用 onEdit 验证复选框并写入 Google 文档。

在这里,我为onFormSubmit 创建了一个函数,它将复选框附加到新条目并修改了appendSignatureRow() 的某些部分,并将其用作onEdit 触发器的函数,以在条目为时写入Google Docs检查。

示例:

表格:

电子表格数据:

可安装触发器设置:

代码:

function formSubmit(e){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; //get form response sheet
  var col = e.range.getLastColumn();
  var row = e.range.getLastRow();
  sheet.getRange(row, col+1).insertCheckboxes(); // add checkbox
}

function appendSignatureRow(e) {
  var sheetName = e.source.getSheetName(); //get sheet name
  var editedRow = e.range.getRow(); // get edited row number
  var editedCol = e.range.getColumn(); //get editer column number
  if(editedRow > 1 && editedCol == 6 && sheetName == 'Form Responses 1' && e.value == 'TRUE'){
    const lock = LockService.getScriptLock();
    lock.waitLock(30000);
    var sheet = e.source.getSheetByName('Form Responses 1');
    var data = sheet.getRange(editedRow, 1, 1, editedCol -1).getValues()[0]; // get edited row data
    const date = new Date(data[0]).toLocaleDateString(); 
    const name = data[1];
    const affiliation = data[2];
    const country = data[3];

    const tableCells = [name, affiliation, country, date]

    const letter = DocumentApp.openById('Insert Docs ID here')
    const body = letter.getBody();

    const table = body.getTables()[0]
    const tableRow = table.appendTableRow()

    tableCells.forEach(function(cell, index) {
      tableRow.appendTableCell(cell)
    })
    letter.saveAndClose();
    lock.releaseLock();
  }  
}

输出:

参考:

【讨论】:

  • 哇,这正是我所需要的——感谢@Nikko J.!看起来我需要做更多的工作来指定函数将指向的单元格,以确定何时触发 onEdit。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2020-05-19
  • 2023-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多