简体   繁体   English

Google表格脚本:自动将一行删除到另一张表格中

[英]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.

相关问题 通过for循环谷歌表格脚本将行复制到另一张表格 - Copy row to another sheet via for loop google sheets script Google表格将行移动到新表格的脚本 - Script for Google Sheets to Move Row to New Sheet Google表格将一行移动到当前行中2个单元格条件的另一张表格 - Google Sheets move a row to another sheet on 2 cell criteria in current row Google Sheet Script - 删除包含特定数据的行 - Google Sheet Script - remove row with specific data Google Spreadsheet脚本为包含特定单词的行上色,并自动将行移动到另一张表 - Google Spreadsheet script to color rows containing a certain word and automatically move the row to another sheet 从Google Sheets脚本的Sheet中的选项卡导入A2行 - Import row A2 from tab within Sheet in Google Sheets Script 如果值存在于工作表 1 和工作表 2 中,则复制行并将其粘贴到工作表 3。Google 表格。 Google Apps 脚本 - Copy row if value exists in both sheet 1 and sheet 2 and paste it to sheet 3. Google Sheets. Google Apps Script Google表格:根据单元格值将一行数据移动到另一张表格 - Google Sheets: Move a row of data to another sheet based on cell value 谷歌表格 - 根据特定列将行移动到另一张表格 - Google Sheets - Moving row to another sheet based on specific column Google表格脚本,用于将一个表格上的一些单元格值传递到新的表格行 - Google Sheets Script for passing a few cell values on one sheet to new sheet row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM