Home > Back-end >  Google Sheets Copy Source to Target and Append Todays Date
Google Sheets Copy Source to Target and Append Todays Date

Time:03-14

I want to copy data from my source sheet

SourceSheet

to my target sheet

TargetSheet with Today's Date

For each new row that I add to the target, I want to add today's date in the "Date" column. I have spent a week trying to solve this with no luck, any help would be greatly appreciated. Here is my code below, how do I add the new Date function?

function copyRangeValuesAPPEND() {
let spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
let sourceSheet = spreadSheet.getSheetByName('Source');

let sourceRange = sourceSheet.getRange("A2:D6");
let sourceValues = sourceRange.getValues();

let rowCount = sourceValues.length;
let columnCount = sourceValues[0].length;

let targetSheet = spreadSheet.getSheetByName('Target');
let targetRange = targetSheet.getRange(targetSheet.getLastRow()   1,1,rowCount,columnCount);
targetRange.setValues(sourceValues);

CodePudding user response:

In your situation, how about the following modification?

From:

let targetRange = targetSheet.getRange(targetSheet.getLastRow()   1,1,rowCount,columnCount);
targetRange.setValues(sourceValues);

To:

let targetRange = targetSheet.getRange(targetSheet.getLastRow()   1, 1, rowCount, columnCount   1);
var date = new Date();
targetRange.setValues(sourceValues.map(e => [...e, date]));
  • By this modification, date is put to the last column to each row.

References:

  • Related