简体   繁体   English

用于在 Google 表格中移动和时间戳行的 Google Apps 脚本

[英]Google Apps Script for moving and timestamping row in Google Sheets

Let me start by saying writing scripts is not in my wheelhouse.让我首先说编写脚本不在我的驾驶室里。 I hobbled together my first one by by utilizing forums like this, but never really understood how it worked.我通过使用这样的论坛来完成我的第一个,但从来没有真正理解它是如何工作的。 Now, that script is no longer functioning and I'm stumped as to why.现在,该脚本不再起作用,我不知道为什么。 In addition, I'd like to add to that script, but am unsure as to how.此外,我想添加到该脚本中,但不确定如何添加。 Here's my situation and objective...这是我的情况和目标...

I have a spread sheet comprised of 2 sheets, and there are 2 things I'd like to happen when a specific dropdown is selected for any row.我有一张由两张纸组成的电子表格,当为任何行选择特定下拉列表时,我希望发生两件事。
First is for that row to move from sheet 1 to sheet 2. I achieved this by using the following script:首先是该行从表 1 移动到表 2。我通过使用以下脚本实现了这一点:

function onEdit(event) {
  // assumes source data in sheet named Repairs
  // target sheet of move to named Closed
  // test column with yes/no is col 4 or D
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();
  if(s.getName() == "Repairs" && r.getColumn() == 6 && r.getValue() == "Closed") {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Closed");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
  }
}

This worked well for sometime, but recently stopped.这在一段时间内运作良好,但最近停止了。

In addition to getting this to work again, the second function I would like to incorporate is to add a timestamp.除了让它再次工作之外,我想加入的第二个 function 是添加一个时间戳。 Ideally, once the row is moved to the 2nd sheet, I would like a column to be added that includes the time/date of the move.理想情况下,一旦将行移动到第二张纸,我希望添加一列,其中包括移动的时间/日期。

If anyone is able to help with this I'd greatly appreciate it.如果有人能够对此提供帮助,我将不胜感激。 If more detail is required please let me know and I'll do my best to provide it.如果需要更多详细信息,请告诉我,我会尽力提供。 Thanks for reading!谢谢阅读!

I've modified your script to remove redundant commands.我已经修改了您的脚本以删除多余的命令。 Everything you need to know about the edit is in the event object.您需要了解的有关编辑的所有信息都在event object 中。

function onEdit(event) {
  // assumes source data in sheet named Repairs
  // target sheet of move to named Closed
  // test column with yes/no is col 4 or D
  var s = event.range.getSheet();
  if( s.getName() == "Sheet1" && event.range.getColumn() == 6 && event.value == "Closed") {
    var row = event.range.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = event.source.getSheetByName("Sheet2");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    target.offset(0,numColumns).setValue(new Date());
    s.deleteRow(row);
  }
}

Reference参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果单元格以特定文本开头,则删除行 - Google Sheets / Apps Script - Delete Row if cell starts with certain text - Google Sheets / Apps Script Google Sheets/Apps Script Copy 公式到新行 - Google Sheets/Apps Script Copy Formula to new row Google Apps 脚本表 PasteDataRequest 粘贴到列而不是行? - Google Apps Script Sheets PasteDataRequest paste to column instead of row? Google Sheets + Apps Script + Webapps:更新与 ID 匹配的现有行 - Google Sheets + Apps Script + Webapps: update existing row that matches the ID Google Apps 脚本/Google 表格 - 仅复制 IF - Google Apps Script/ Google Sheets - Only copy IF 在移入Google表格之前检查行 - check the row before moving in google sheets Google Apps Script - 如何将此代码应用于 Google 表格中的多个表格 - Google Apps Script - how to applies this code for multiple sheets in google sheets 如果值存在于工作表 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 Apps Script Google Sheets Gmail 处理新邮件追加行数组 - Google Apps Script Google Sheets Gmail Processing New Emails Append Row Array Google Apps Script - 按标签颜色获取工作表 - Google Apps Script - Get sheets by tab color
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM