javascript - 谷歌表格脚本: Automatically remove a row into another sheet

标签 javascript google-apps-script google-sheets google-apps

我目前正在使用 Google 表单来让用户提交网站更改。这一切都很顺利。

我添加了一个包含票证状态的额外列。现在我编写了一个脚本,用于检查何时更新为“完成”,然后将该行移动到另一个标题为“完成”的工作表中。

但是,只有当我更新手动添加的行时,这才有效。如果我更改从表单条目创建的行上的列,它将不起作用。以前有人见过这个问题吗?代码如下。

function onEdit(e){

  /*
   When a user updates a status to done the ticket gets moved to another sheet
  */
  var sheetNameToWatch = "Form responses 1"; // The sheet to watch for changes
  var columnNumberToWatch = 1; // The column where the change happens on the above sheet
  var valueToWatch = "Done"; // What is the text you are looking for
  var sheetNameToMoveTheRowTo = "Done"; // The Sheet name for where the row gets moved to.

  var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get the active spreadsheet
  var sheet = SpreadsheetApp.getActiveSheet(); // Get the active sheet
  var range = sheet.getActiveCell(); // Get the current cell that has just been updated

  // Check that you are on the correct sheet and column. 
  if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
    var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo); // get a reference for the target sheet
    var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1); // get a reference for the target row
    sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange); // copy the row from current sheet to the new sheet
    sheet.deleteRow(range.getRow()); // Delete the row from the old sheet
  }

}

最佳答案

是的,除非您手动执行,否则 onEdit 函数不会触发,您需要使用 onFormSubmit 触发器,该触发器将在收到表单响应时触发。这是一个连接到您的表单的手动安装的触发器,它应该能够像编写的那样执行上面的功能。

 var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
 ScriptApp.newTrigger('myFunction')
     .forForm(form)
     .onFormSubmit()
     .create();

不过,为了安全起见,您可能希望通过 ID 或名称来定义电子表格、工作表和范围,而不是通过它们是否处于事件状态,我相信如果电子表格处于事件状态,getActiveWhatever() 类型的方法将返回 null打开。

关于javascript - 谷歌表格脚本: Automatically remove a row into another sheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29341638/

相关文章:

javascript - 如何在 css 页面加载时启用悬停选项卡?

javascript - 将 _.where 与数组 underscore.js 一起使用

javascript - 为什么在 if 语句中使用双感叹号?

javascript - datejs在javascript中覆盖日期

javascript - 如何从应用程序脚本中使用 googleapis

google-apps-script - 在Google Apps脚本中设置超时

regex - 使用 Google 表格函数组合字符串的相似部分

google-apps-script - 如何使用 javascript 选择谷歌电子表格中的所有复选框?

javascript - 无法使用changeType "OTHER"获取单元格值

javascript - 设置 Google 表格中单元格值的格式