[英]Google Sheets Script : Automatically remove a row into another sheet
I am currently using google forms to enable users to submit site changes. 我目前正在使用Google表单来使用户能够提交网站更改。 This is all working beautifully.
这一切都做得很好。
I have added an extra column that has ticket status. 我添加了一个具有票证状态的额外列。 Now I have written a script that checks for when this is updated to Done and then moves that row into another sheet entitled done.
现在,我编写了一个脚本,该脚本检查何时将其更新为“完成”,然后将该行移至另一个名为“完成”的工作表中。
However this only works if i update a row that i manually added. 但是,这仅在我更新手动添加的行时有效。 If I change the column on a row created from the form entry it will not work.
如果我更改从表单条目创建的行上的列,它将无法工作。 Has anyone seen this issue before.
有人看过这个问题吗? Code is below.
代码如下。
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
}
}
Yep, the onEdit function won't fire unless you do it manually, you'll want to use the onFormSubmit trigger, which will fire when a form response is received. 是的,除非手动进行操作,否则不会触发onEdit函数,您将需要使用onFormSubmit触发器,该触发器将在收到表单响应时触发。 This is a manually installed trigger connected to your form, which should be able to do the function above pretty much as written.
这是连接到表单的手动安装的触发器,应该能够完成上面编写的功能。
var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
ScriptApp.newTrigger('myFunction')
.forForm(form)
.onFormSubmit()
.create();
To be safe, though, you may want to define your spreadsheet, sheet, and range by id or by name rather than by whether they are active, I believe getActiveWhatever() kinds of methods will return null if the spreadsheet isn't open. 但是,为了安全起见,您可能希望通过ID或名称(而不是通过它们是否处于活动状态)来定义电子表格,工作表和范围,因此我相信如果电子表格未打开,则getActiveWhatever()方法会返回null。
As mentioned, using active is not the best idea. 如前所述,使用主动并不是最好的主意。
Instead of: 代替:
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
Try: 尝试:
var range = e.range;
var sheet = range.getSheet();
var ss = sheet.getParent();
If you try this you cannot execute it from the code editor because no event is passed. 如果尝试此操作,则由于无法传递事件,因此无法从代码编辑器中执行它。 You must execute it by actually submitting a form.
您必须通过实际提交表单来执行它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.