gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:18:43 25 4
gpt4 key购买 nike

我目前正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com