Home > Mobile >  Google Sheets Script Editor minor adjustment
Google Sheets Script Editor minor adjustment

Time:10-06

I currently have a script written (from help of others on forums) that has worked great for years. I'm now trying to use on another spreadsheet and just need to make a minor tweak in how it works. Here is current script:

function onEdit() {
  var sheetName = "Master List";
  var s = SpreadsheetApp.getActiveSheet();
  if (s.getName() !== sheetName) return;
  var r = s.getActiveCell();
  if( r.getColumn() != 5 ) { //checks the column
    var row = r.getRow();
    var time = new Date();
    time = Utilities.formatDate(time, "GMT-05:00", "MM/dd/yyyy");
    SpreadsheetApp.getActiveSheet().getRange('E'   row.toString()).setValue(time);
  };
 };

Its function previously was to put a date & timestamp in a specified column anytime an edit was made within a particular row. Basically it would tell me when the last edit was made on any given row.

Now I want column E (column 5) to be date stamped when only Column D (column 4) is edited. My use now is for price tracking. Column D will have the price and Column E will tell me the last time I updated the price in the spreadsheet.

I believe the edit needs made on the 5th line of code where it gets ActiveCell it needs a specified cell instead of any cell within the row. Please let me know the appropriate changes I should make. Thanks, Silas

CodePudding user response:

Just replace

if( r.getColumn() != 5 ) { //checks the column

with

if( r.getColumn() == 4 ) { //checks the column

CodePudding user response:

function onEdit(e) {
  const s = e.range.getSheet();
  if (s.getName() == "Master List" && e.range.columnStart == 4 ) {
    s.getRange(e.range.rowStart, 5).setValue(Utilities.formatDate(new Date(), "GMT-05:00", "MM/dd/yyyy"));
  };
}
  • Related