Home > Software engineering >  Google Script. Sheets. Add date and text at the last row after a copy paste
Google Script. Sheets. Add date and text at the last row after a copy paste

Time:01-08

I'm breaking my head here trying to figure it out a way to achieve this in a simples way.

I got working a code that copies a range from a sheet and paste in another at the last row.

    function copyInfo() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var copySheet = ss.getSheetByName("Review");
  var pasteSheet = ss.getSheetByName("Log");

  // get source range
  var source = copySheet.getRange(3,2,15,5);
  // get destination range
  var destination = pasteSheet.getRange(pasteSheet.getLastRow() 1,3,15,5);

  // copy values to destination range
  source.copyTo(destination, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);

}

The problem is, at the destination sheet, I still have columns A and B empty. By each row in the source (varies from time to time) copied, I need to place at column A todays date and column B the text "Review".

Like: Date (column A) | Revview (column B) | Pasted Data from Source (column C and other)

Do I need to write a completely new code to check for empty or can I implement both solutions into this code?

Please help, I can't make it work past this!

Thank you in advance

CodePudding user response:

You can do something like this after copying and pasting the source data:

  ...//your code here

  var data = [];
  data.push([Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone(), 'MMMM dd, yyyy'), 'Review']);
  reviewRange = pasteSheet.getRange(pasteSheet.getLastRow(), 1, 1, 2)
  reviewRange.setValues(data); //set the date and 'Review' data

The code should fill the two cells before your data.

  • Related