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

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

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 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