Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

Google SpreadSheet Script to Set Current Date

I have a google sheet script that moves a row from one sheet (Review) to another (Reviewed) when the checkbox in a cell (column 20) on the row is true. Please see a copy of the script below.

I need assistance in modifying the script so it sets a timestamp {setValue(new Date())} on the adjacent cell (21) when the cell (column 20) is set to true or as soon as it is moved to the target sheet (Reviewed). Thanks in advance.

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();

  if(s.getName() == "Review" && r.getColumn() == 20 && r.getValue() == true) {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Reviewed");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);```
question from:https://stackoverflow.com/questions/65600326/google-spreadsheet-script-to-set-current-date

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Explanation:

If you want to set the timestamp in the active sheet s then:

s.getRange(row,21).setValue(new Date());

or if you want in the target sheet targetSheet then:

targetSheet.getRange(targetSheet.getLastRow(),21).setValue(new Date());

Solution:

function onEdit(event){
  var ss = event.source;
  var s = ss.getActiveSheet();
  var r = event.range;
  if(s.getName() == "Review" && r.getColumn() == 20 && r.getValue() == true) {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Reviewed");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
    s.getRange(row,21).setValue(new Date());  
  //targetSheet.getRange(targetSheet.getLastRow(),21).setValue(new Date()); 
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...