I have a script that copies information from one sheet to another, but it keeps adding more rows each time I run it. I want to change this so it replaces the existing data instead of creating new rows.
Right now my code takes the last row from the first sheet and puts it at the bottom of the second sheet. This makes the second sheet grow bigger every time. What I need is for it to always overwrite the same row in the second sheet, so there is only one row of data at any time.
Can someone help me modify this to replace data instead of appending it?
function copySheetData() {
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('DataSource');
var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('DataTarget');
var lastRow = sourceSheet.getLastRow();
var lastCol = sourceSheet.getLastColumn();
var sourceData = sourceSheet.getRange(2,1,lastRow-2,lastCol).getDisplayValues();
var processedData = sourceData.map(row => {
return [row[0], row[2], row[4], row[6], row[8], row[10], row[12], row[14], row[16], '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''];
})
targetSheet.getRange(3,1,lastRow-2,37).setValues(processedData);
}