Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda