Current Issue:
Hey everyone, appreciate any help here as I'm still beginning my journey in coding.
I'm trying to see if I can make a script that will:
- Look for duplicates (in column D), and
- delete any data from the following duplicates after the 1st match in columns E-L (see desired outcome if that doesn't make sense verbally).
- The script would need to use the column header names (ex. "snacks") instead of hard-coded column references
*So for example, the script finds ABC001, deletes only the duplicates for ABC001 in the corresponding columns then moves on to ABC004 and performs the same action.
I'm not sure how to write a script that would do this, and keep going to find duplicates after the 1st set is found. I think I know how to do a for loop now, but it's not clear to me how to make it do a search loop and stop after it find the first match and keep going.
Code so far below. I think I would need to incorporate something like JSmith showed in this example? Or would I need to incorporate some form of .length
with the duplicate range in a for
statement so that it can find the duplicates, get the # of them, and then only perform the action on everything past the 1st instance?
function duplicateRemoval() {
ss = SpreadsheetApp.getActive().getSheetByName('Sheet1');//gets sheet by name
const [aB,...cd] = ss.getDataRange().getValues();//literal assignment that assigns aB to the header array and the rest of the data to 'cd'
let column = {}
let iData = {};//index into the row array for each column header
aB.forEach((a,i)=>{column[a] = i+1;iData[a]=i});//building column and iData so that headers can move anywhere
}//let & forEach & const const [aB,...cd] derived from (https://stackoverflow.com/questions/70101896/search-column-for-text-and-use-array-list-to-insert-text-in-another-cell) @Cooper
Raw Data:
Name | Owner | Snack | Transaction # | # of snacks requested | #2 | #3 | #4 | #5 | #6 | #7 | #8 |
---|---|---|---|---|---|---|---|---|---|---|---|
Bill Example | Snacktown | celery | ABC001 | 4 | 1 | 2 | 3 | 4 | 5 | 6 | 4 |
Bill Example | Snacktown | celery | ABC001 | 4 | 1 | 2 | 3 | 4 | 5 | 6 | 4 |
Bill Example | Snacktown | celery | ABC001 | 4 | 1 | 2 | 3 | 4 | 5 | 6 | 4 |
Jane Doe | Snacktown | chips | ABC002 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
Jane Doe | Chipworld | chips | ABC003 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
Jane Doe | Chipworld | chips | ABC004 | 5 | 5 | 1 | 1 | 1 | 1 | 1 | 5 |
Jane Doe | Chipworld | chips | ABC004 | 5 | 5 | 1 | 1 | 1 | 1 | 1 | 5 |
Jane Doe | Chipworld | chips | ABC004 | 5 | 5 | 1 | 1 | 1 | 1 | 1 | 5 |
Jane Doe | Chipworld | chips | ABC004 | 5 | 5 | 1 | 1 | 1 | 1 | 1 | 5 |
Sources:
google app script array delete duplicate value from top
Google Script App Delete Duplicate Rows with a Specific Value in Specific Column in Google Sheet