Goal of the function:
Rows with a TOSync value of TRUE shall be copied and pasted over to the Destination sheet using the rowid in the source sheet.
The current sheet where the data is copied from can be filtered and sorted.
Source sheet contains a checkbox with a true/false value. True means it should be flagged for syncing/copying to destination sheet.
Row ID refers to these row numbers in the destination sheet.
Triggering the function should copy all rows flagged as TRUE/checked, then copy them over to the corresponding row index in the destination.
Example using picture above:
- First flag has a row ID of 1741.
- The function will copy the data range from source sheet (trackerSheet)
- and paste it to row# 1741 of the destination sheet.
The problem:
The data contains more than 2,000 rows of records. My current code is pretty slow when the function runs, iterating to every row and doing the copy and paste per row from source (trackersheet) to destination (mmics or bau sheet).
The destination setValue is not for adjacent rows or a series of rows like 1-100, 500-600, etc, but a random one like (1,3,8,54,798, etc).
What is the fastest way to do this?
My code:
function copyToDestination(){
/**** setting up external/destination sheets ******/
const toSyncDataRange = trackerSheet.getRange(firstRowTrackerData,colToSync,dataRowsCount,1).getValues();
const dataRowsCount = lastRowTrackerData - firstRowTrackerData;
const sourceSpreadSheet = SpreadsheetApp.openById(DataSourceSSId);
const BAUSheetName = sheet.getRange("BAUSheetName").getValue();
const MMICSSheetName = sheet.getRange("MMICSSheetName").getValue();
const mmicsSheet = sourceSpreadSheet.getSheetByName(MMICSSheetName);
const bauSheet = sourceSpreadSheet.getSheetByName(BAUSheetName);
//trackerSheet is the source sheet
let toUpdate = false;
//reiterate through the ToSync Column to check for TRUE values
for (let i = 0; i < dataRowsCount; i++) { //this can still be optimized
if(toSyncDataRange[i][0] === true){ //if toSync is checked or TRUE
let currentRow = i + 1 + TrackerHeaderRow; //+1 coz index starts at 0; trackerheaderrow is the header row
//this is the data range to be copied to destination
const editableData = trackerSheet.getRange(currentRow,colFirstEditableTracker,1,numColsTrackerData).getValues();
//get rowId within source sheet
const rowId = trackerSheet.getRange(currentRow,colRowId).getValue();
const source = trackerSheet.getRange(currentRow,colSourceTracker).getValue();
//if source at column B is ICS, then use the MMICS sheet; else use BAU sheet
if(source === "ICS"){
const mmics = mmicsSheet.getRange(rowId,1,1,numColsTrackerData);//.setValues(editableData);
mmics.setDataValidation(null);
mmics.setValues(editableData); //this can be optimized
}
else if(source === "BAU"){
const bau = bauSheet.getRange(rowId,1,1,numColsTrackerData);//.setValues(editableData);
bau.setDataValidation(null);
bau.setValues(editableData); //this can be optimized
}
trackerSheet.getRange(currentRow,colLastUpdated,1,1).setValue("UPLOADED"); //this can be optimized
toUpdate = true;
}
}
if(toUpdate){
SpreadsheetApp.getActive().toast("Finished uploading changes to original source...");
}
}