'Timestamp for Google Spread Sheets (for only filled rows data)

In the spreadsheet, I want to add a timestamp for a pre-selected (E20: N48) row and column data.

For example: If the E20: E22 cell of Sheet-1 is filled with some data, then the timestamp will be set from cell A2 to cell A4 in column 1 of Sheet-2. Similarly, the timestamp will be set for the data that will be up to the last row each time.

As you can see in my code, 29 is given as the last row, due to which the first 2 rows are full of data but the timestamp is being set up to the remaining 28 rows including the first 2 rows.

Special note: I only want to add timestamps for rows filled with data.

Here is my code:

function save() {

  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var ss = sheet.getSheetByName('Sheet-1'); //sheetstore
  var data = sheet.getSheetByName('Sheet-2');
  var getValue = ss.getRange('E20:N48').getValues();
  var lastRow = data.getLastRow()+1;
  var updateTable = data.getRange(lastRow,2,29,10).setValues(getValue); //From column 2, total 29 column, total 10 rows

  //Timestamp for column 1
  var timestamp = Utilities.formatDate(new Date(), "GMT+6:00", "yyyy-MM-dd HH:mm:ss");
  Logger.log(lastRow)
  var date = data.getRange(lastRow,1,29,1).setValue(timestamp); //From columns 1 to 29 rows, every time print in column 1
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source