Mars
Mars

Reputation: 185

Date Stamp for Cell update

Using a Google App Script to show when a Stock Take cell has been entered or updated.

using the following simple script:

function onEdit(ss) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("StockTake"); // StockTake is the sheet where data is entered
  var activeCell = sheet.getActiveCell();
  var col        = activeCell.getColumn();     //  numeric value of column, ColA = 1
  var row        = activeCell.getRow();        //  numeric value of row
  if (col == 2 || col == 3 || col == 4 || col == 5 || col == 6 || col == 7) {
    sheet.getRange(row, col+6).setValue(new Date()).setNumberFormat('DDD, MMM dd');
  }
}

Problem is that sometimes the Column Header will also Date Stamp. I would like to know how to go about keeping the first row from getting a date stamp.

Thank you!

Upvotes: 0

Views: 188

Answers (1)

user3717023
user3717023

Reputation:

To avoid stamping the header row, you want to make sure that row > 1. So, add that to the if-statement.

While doing that, also simplify the condition for column: it's 2 <= col <= 7, which is expressed as

if (row > 1 && col >= 2 && col <= 7) {
  sheet.getRange(row, col+6).setValue(new Date()).setNumberFormat('DDD, MMM dd');
}

Upvotes: 2

Related Questions