Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
google app script how to count an array and show summary unique values

in this post google app script i have a question how to count a duplicate values

I want to enter a numeral in G2:G3 (source sheet) and want to show a unique count values in B2:B4 (destination sheet) what i have to do https://docs.google.com/spreadsheets/d/1HN0XCLrEzlRkInIv6xFnhCFnhZabu7Y-Tz1dvYvsGQM/edit?usp=sharing

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In your situation, how about the following sample script?

Sample script:

function myFunction() {
  const srcSheetName = "source"; // Please set the source sheet name.
  const dstSheetName = "des"; // Please set the destination sheet name.

  // Retrieve source and destination sheets.
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const [srcSheet, dstSheet] = [srcSheetName, dstSheetName].map(s => ss.getSheetByName(s));

  // Retrieve source values and create an object for putting to the destination sheet.
  const srcValues1 = srcSheet.getRange("A1:C" + srcSheet.getLastRow()).getValues();
  const obj1 = srcValues1[0].map((_, c) => srcValues1.map(r => r[c])).reduce((o, [h, ...v]) => (o[h] = v, o), {});
  const srcValues2 = srcSheet.getRange("F2:G" + srcSheet.getLastRow()).getValues();
  srcValues2.forEach(([f, g]) => {
    if (g > 1) {
      for (let i = 0; i < g - 1; i++) {
        obj1[f] = [...obj1[f], ...obj1[f]];
      }
    }
  });
  const obj2 = Object.values(obj1).flat().reduce((o, a) => (o[a] = o[a] ? o[a] + 1 : 1, o), {});

  // Retrieve the values of column "A" from the destination sheet and create an array for putting to Spreadsheet.
  const dstRange = dstSheet.getRange("A2:A" + dstSheet.getLastRow());
  const dstValues = dstRange.getDisplayValues().map(([a]) => [obj2[a] || 0]);

  // Put the result values to the column "B" of the destination sheet.
  dstRange.offset(0, 1).setValues(dstValues);
}
  • From your provided Spreadsheet, the sample sheet names are source and des. Please modify this for your actual situation.
  • When you run this script, the values are retrieved from the source sheet, and the groups are increased using the values from column "G", and the count of each value (1, 2, 3) is calculated. And, the result values are put in column "B" of the destination sheet.

Note:

  • This sample script is for your provided Spreadsheet. When you change the structure of the Spreadsheet, this script might not be able to be used. Please be careful about this.

References:

  • reduce()
  • map()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!