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