简体   繁体   中英

Google Sheets Script Editor - Creating a Timestamp and keeping that Timestamp from changing after first entry

I am new to Script Editing in Google Sheets. I am attempting to create a Timestamp in a cell in rows 31 and 32 when input is received in rows 13 and 19. Row 13 is for inputting names and row 19 is for tracking the projects completion. The names on the projects will change throughout the duration, so once the first name is created in a cell, I want to make sure the Timestamp will not change. Any ideas on how to make this work?

Editable link to an example of the sheet I am creating: https://docs.google.com/spreadsheets/d/1bsA8h0VGudmvB_qHOJrpz4UI9aRvIwRLXR3086rWAVw/edit?usp=sharing

The current script I have created:

function onEdit(e) {

var row = e.range.getRow();
var col = e.range.getColumn();

if(col === 13){
e.source.getActiveSheet().getRange(row,31).setValue(new Date()); 
if(e.source.getActiveSheet().getRange(row,31).getValue() == ""){
}
}

if(col === 19){
e.source.getActiveSheet().getRange(row,32).setValue(new Date()); 
}




}

For your case, I would recommend you to use the getCell(row, column) method and then evaluate if the cell is blank using the isBlank() method. This is an example code that will help you:

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var row = e.range.getRow();
  var col = e.range.getColumn();
  // col M = 13, col S = 19
  if(col === 13) setTimestamp(ss, row, 31); // col AE = 31
  else if(col === 19) setTimestamp(ss, row, 32); // col AF = 32
}

function setTimestamp(ss, row, col){
  // Get the modified Cell
  var cell = ss.getRange(row, col).getCell(1, 1); 
  var isCellBlank = cell.isBlank();
  // Check if it's blank
  if(isCellBlank) cell.setValue((new Date()));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM