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

133
Visualizações
App Script COUNTIF for maximum date in column

I have a list of dates in column A:

Column A
2022-02-28
2022-02-28
2022-02-28
2022-02-14
2022-02-14
2022-02-07

I'm trying to write a script that counts the number of times the largest date occures. I wrote the below script

function maxcount() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var devdeploy = ss.getSheetByName("Sheet1")
  var Avals = devdeploy.getRange("A2:A").getValues();  
  var Alength = Avals.filter(String).length;
  var max = Avals[0][0]
  var unique_count = 0

  for (i=0; i < Alength; i++){
    if (Avals[i][0] == max){
      unique_count++;
    } 
  }
  Logger.log(unique_count) 
}

This script works if I use integer values and have the maximum value in cell A2. However, when I use dates instead of integers it always returns a value of 1. Any ideas on why the if loop does not work on dates, but works on integers/strings? Also is there a way to improve the script to look for the maximum value in column A then find how many times it occurs?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If your date in the column "A" is the date object, how about the following modification? I thought that in your script, by var max = Avals[0][0], only 1st element is compared. And, if the values of column "A" are the date object, the date object is compared. I thought that this might be the reason for your issue.

Modified script:

function maxcount() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var devdeploy = ss.getSheetByName("Sheet1")
  var Avals = devdeploy.getRange("A2:A" + devdeploy.getLastRow()).getValues().filter(([a]) => a.toString() != "");
  var values = Avals.map(([a]) => a.getTime());
  var max = Math.max(...values);
  var unique_count = values.filter(v => v == max).length;
  console.log(unique_count)
}
  • In this modification, the values are converted to the unix time, and retrieved the maximum value. And, the number of the maximum value can be retrieved.

References:

  • map()
  • filter()
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