Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda