Skip to content

Commit

Permalink
fix: Update Removing Duplicates to use object for uniqueness check (#385
Browse files Browse the repository at this point in the history
)
  • Loading branch information
vinay-google authored Feb 13, 2023
1 parent 290cd98 commit cf41613
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions sheets/removingDuplicates/removingDuplicates.gs
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,21 @@
*/
function removeDuplicates() {
// [START apps_script_sheets_sheet]
let sheet = SpreadsheetApp.getActiveSheet();
let data = sheet.getDataRange().getValues();
const sheet = SpreadsheetApp.getActiveSheet();
const data = sheet.getDataRange().getValues();
// [END apps_script_sheets_sheet]
// [START apps_script_sheets_new_data]
let newData = [];
// [END apps_script_sheets_new_data]
for (let i in data) {
let row = data[i];
let duplicate = false;
for (let j in newData) {
if (row.join() == newData[j].join()) {
duplicate = true;
}
}
const uniqueData = {};
for (let row of data) {
const key = row.join();
// [START apps_script_sheets_duplicate]
if (!duplicate) {
newData.push(row);
}
uniqueData[key] = uniqueData[key] || row;
// [END apps_script_sheets_duplicate]
}
// [START apps_script_sheets_clear]
sheet.clearContents();
// [START apps_script_sheets_new_data]
const newData = Object.values(uniqueData);
// [END apps_script_sheets_new_data]
sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
// [END apps_script_sheets_clear]
}
Expand Down

0 comments on commit cf41613

Please sign in to comment.