I have 2 sheets named source (appsheet included) and target. If column "Finalized" is yes, I want that row to be copied into target. However, I do not want duplicate data.
Here is my attempt below. Not too sure what I am doing wrong. Entire pastebin here. Thanks in advance.
function moveRows() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const targetSheet = SpreadsheetApp.openById('1s6B6OURQ7FFmP106mrE9laqZ3XLwiXtjway6iGyWsOk').getSheetByName('Sheet1');
const targetLastRow = targetSheet.getLastRow();
let lastRow = sheet.getLastRow();
let sortRange = sheet.getSheetValues(2,1,lastRow, 4);
let targetCounter = 1;
Logger.log(lastRow)
for (var i = 1; i <= sortRange.length; i++) {
let finalized = sheet.getRange(i,1).getValue();
console.log(i + " - " + finalized);
let duplicateCheck = targetSheet.getRange(1, 1, targetLastRow, 4).getValues();
let isDuplicate = false;
for (var j = 0; j < duplicateCheck.length; j++) {
if (JSON.stringify(duplicateCheck[j]) === JSON.stringify(sortRange[i - 1])) {
isDuplicate = true;
break;
}
}
if (!isDuplicate && finalized == "Yes") {
let rowValues = sheet.getRange(i, 1, 1, 4).getValues();
targetSheet.getRange(targetLastRow+targetCounter, 1, 1, 4).setValues(rowValues);
targetCounter++;
}
};
};
Found the solution.