Get count of duplicates based on two columns in a multidivisional array and add count to the array Google Script

102 views
Skip to first unread message

Timothy Hanson

unread,
Dec 1, 2022, 1:55:39 PM12/1/22
to Google Apps Script Community
Get the count of duplicates based on two columns in a multidivisional array and add the count to the array Google Script.

Have
have.png

Need
need.png

I need to count the duplicates in an array based on the conditions.

values[i][2]==values[i+1][2] && values[i][1]==values[i+1][1])

 and add the duplicate value to the column `Total Gifts`

And sum the totals for `ig_Flow` and output the results to the column `Total Flow`

I have tried modifying this but with no success

function count_duplicate() {
  let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Need");
  let values = sheet.getDataRange().getValues();

  let counts = new Map();

  for (let i = 0; i < values.length; i++) {
    const data = counts.get(values[i].toString());
    if (data) data.count++;
    else counts.set(values[i].toString(), { value: values[i], count: 1 });
  }

  const result = [];
  for (let [, { value, count }] of counts) {
    result.push([...value, count]);
  }
  console.log(result);
  return result;
}

Here is a Google sheet with the data

Thanks for assistance

 

Tanaike

unread,
Dec 1, 2022, 9:45:02 PM12/1/22
to Google Apps Script Community
In your situation, how about the following sample script?

let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Need");
let [, ...values] = sheet.getDataRange().getValues();
const obj = values.reduce((o, [, b, c, , e]) => (o[b + c] = o[b + c] ? [++o[b + c][0], o[b + c][1] + e] : [1, e], o), {});
const res = values.map(([, b, c]) => obj[b + c] || null);
console.log(res);


When your sample Spreadsheet is used, `res` is `[[1,1],[2,5],[2,5],[1,15],[3,9],[3,9],[3,9],[1,1],[1,7],[4,12],[4,12],[4,12],[4,12]]`. I thought that in this case, the order of search objects is not related to the result. So, I used a JSON object for searching the values instead of the Map object.

If I misunderstood your goal, I apologize.

Jason Cobble

unread,
Dec 2, 2022, 7:35:40 AM12/2/22
to google-apps-sc...@googlegroups.com
You do realize.... Youre helping these assholes hack and harass ppl right? Or do you even care?

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/21554867-c0aa-4381-ba22-32d0d8082925n%40googlegroups.com.

Timothy Hanson

unread,
Dec 2, 2022, 10:06:38 AM12/2/22
to Google Apps Script Community
Tanaike, thank you!  That works great.  I will have to look up reduce and see what I can make of your code.

Thanks again

Timothy Hanson

unread,
Dec 2, 2022, 10:20:35 AM12/2/22
to Google Apps Script Community
Tanaike, wow, do I not understand this part :)

const obj = values.reduce((o, [, b, c, , e]) => (o[b + c] = o[b + c] ? [++o[b + c][0], o[b + c][1] + e] : [1, e], o), {});

Can you explain it some?

Thanks

Timothy Hanson

unread,
Dec 2, 2022, 5:13:06 PM12/2/22
to Google Apps Script Community
I have been studying and what exactly is o[b + c]

Tanaike

unread,
Dec 2, 2022, 8:43:08 PM12/2/22
to Google Apps Script Community
About "const obj = values.reduce((o, [, b, c, , e]) => (o[b + c] = o[b + c] ? [++o[b + c][0], o[b + c][1] + e] : [1, e], o), {}); Can you explain it some?",

In this part, an object for searching the values of column "E" and the count. And, the values of columns "B" and "C" are used as a key. From your sample Spreadsheet, I thought that this key can be used as the unique value.

About "I have been studying and what exactly is o[b + c]",

In this part, the unique key is used using the values of columns "B" and "C".

Reply all
Reply to author
Forward
0 new messages