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

165
Vistas
How can I check if a date in a cell is this week? Script not sheet function

Im trying to check if the dates in column A are this week, then show the row.

my problem is my variable "t" is undefined in the debugger and when run i get the following error...

Exception: Invalid argument: date. Should be of type: Date

 function weekNumberTest()
{
      var ss = SpreadsheetApp.getActiveSpreadsheet();
    var s = ss.getSheetByName("Test");
        var v = s.getRange("A1:A").getValues();
  var data = Utilities.formatDate(new Date(), "GMT", "w");//Exception: Invalid argument: date. Should be of type: Date

for (var i = s.getLastRow(); i > 2; i--) 
{
        var t = v[i ];
        if (t != "") 
        {
            var testDateCell=Utilities.formatDate(t, "GMT", "w");
            if (testDateCell==data ) 
            {
                s.showRows(i);
            }
        }
    }
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Dates in current week

function weekNumberTest() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Test");
  const vs = sh.getRange(2,1,sh.getLastRow()-1).getValues().flat();
  const wk = parseInt(Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone(), "w"));
  vs.forEach((d,i) => {
    let w =parseInt(Utilities.formatDate(new Date(d),ss.getSpreadsheetTimeZone(),"w"));
    if(w == wk || w == wk - 1) {//try this for last two weeks. This may not work at beginning of year...I don't know for sure.
      sh.showRows(i + 2);
    } else (
      sh.hideRows(i + 2)
    )
  });
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

function weekNumberTest() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName("Test");
  var v = s.getRange("A1:A").getValues();
  var data = Utilities.formatDate(new Date(), "GMT", "w");

  for (var i = s.getLastRow(); i > 2; i--) {
    var t = new Date(v[i][0]); // <---------------- the error was here
    if (t != "") {
      var testDateCell = Utilities.formatDate(t, "GMT", "w");
      if (testDateCell == data) {
        s.showRows(i);
      }
    }
  }
}

To get the dates from this and last week you can change one line this way:

if (testDateCell == data || testDateCell == data-1) {

Explanation

var v = s.getRange("A1:A").getValues(); it gets you a 2D array [[a],[b],[c]]. To get values from the a 2D array you need to use two indexes: v[0][0], v[0][1], v[0][2].

Alternatively you can use the flat() method to convert a 2D array into 1D array this way: var v = s.getRange("A1:A").getValues().flat();. In this case you can address the values of the array with one index: v[0], v[1], v[2].

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