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

571
Vistas
¿Por qué se pasa nulo a mi función, cuando se llama desde el editor?

En el siguiente ejemplo, playgroundSheet se define globalmente. Cuando se ejecuta como se indica a continuación con BH2, BL2:

 function testDates(DateLow, DateHigh) { var now = playgroundSheet.getRange('BH2').getValue();; var nowMilli = Number(now.getTime()).toFixed(0); var targetDate = playgroundSheet.getRange('BL2').getValue(); var targetDateMilli = Number(targetDate.getTime()).toFixed(0); var durationMilli = targetDateMilli - nowMilli; // return numberOfDays Logger.log(now); Logger.log(nowMilli); Logger.log(targetDate); Logger.log(targetDateMilli); Logger.log(durationMilli/day); }

Obtengo los resultados que deseo:

 11:00:18 AM Notice Execution started 11:00:20 AM Info Wed Jun 08 00:00:00 GMT-04:00 2022 11:00:20 AM Info 1654660800000 11:00:20 AM Info Sun Jun 12 00:00:00 GMT-04:00 2022 11:00:20 AM Info 1655006400000 11:00:20 AM Info 4.0 11:00:20 AM Info null 11:00:20 AM Info Wed Jun 08 00:00:00 GMT-04:00 2022 11:00:20 AM Info 1654660800000 11:00:20 AM Info Sun Jun 12 00:00:00 GMT-04:00 2022 11:00:20 AM Info 1655006400000 11:00:20 AM Info 4.0 11:00:19 AM Notice Execution completed

Pero ejecutar por llamada a la función:

 var numberOfDays = testDates('BH2','BL2'); Logger.log(numberOfDays); function testDates(DateLow, DateHigh) { var now = playgroundSheet.getRange(DateLow).getValue();; var nowMilli = Number(now.getTime()).toFixed(0); var targetDate = playgroundSheet.getRange(DateHigh).getValue(); var targetDateMilli = Number(targetDate.getTime()).toFixed(0); var durationMilli = targetDateMilli - nowMilli; return numberOfDays;

me sale lo siguiente:

 10:28:03 AM Notice Execution started 10:28:03 AM Info null 10:28:03 AM Error Exception: Argument cannot be null: a1Notation testDates @ Code.gs:13

donde esta la linea 13

 var now = playgroundSheet.getRange(DateLow).getValue();```

He intentado pasar la sintaxis (row, col, #row, #col) sin alegría y no tengo ideas.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Con respecto a

 var numberOfDays = testDates('BH2','BL2'); Logger.log(numberOfDays); function testDates(DateLow, DateHigh) { var now = playgroundSheet.getRange(DateLow).getValue();; var nowMilli = Number(now.getTime()).toFixed(0); var targetDate = playgroundSheet.getRange(DateHigh).getValue(); var targetDateMilli = Number(targetDate.getTime()).toFixed(0); var durationMilli = targetDateMilli - nowMilli; return numberOfDays; }

reemplazar

 var numberOfDays = testDates('BH2','BL2'); Logger.log(numberOfDays);

por

 function myFunction(){ var numberOfDays = testDates('BH2','BL2'); Logger.log(numberOfDays); }

Luego ejecute myFunction en lugar de testDates .

Lo anterior porque llamar a testDates o cualquier otra función desde el editor de script, un menú personalizado, etc. hará que las sentencias en el alcance global se ejecuten antes de ejecutar la función que se llamó, en este caso se ejecutó dos veces, la primera es causado por var numberOfDays = testDates('BH2','BL2'); el segundo por la interfaz de usuario/disparador utilizado para llamar a la función. El error ocurre porque la llamada desde la interfaz de usuario no pasa parámetros a la función.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Pruébalo de esta manera:

 function testDates(DateLow, DateHigh) { const ss = SpreadsheetApp.getActive(); const sh = ss.getSheetByName('pg'); var now = new Date(sh.getRange('BH2').getValue()); var nowMilli = now.valueOf(); var targetDate = new Date(sh.getRange('BL2').getValue()); var targetDateMilli = targetDate.valueOf() var durationMilli = targetDateMilli - nowMilli; }
about 4 years ago · Juan Pablo Isaza Denunciar

0

Gracias a TheMaster y Rubén. La función final llamada desde otra función devuelve el número de días entre dos fechas:

 function testDates(DateLow, DateHigh) { var now = playgroundSheet.getRange('BH2').getValue();; var nowMilli = Number(now.getTime()).toFixed(0); var targetDate = playgroundSheet.getRange('BL2').getValue(); var targetDateMilli = Number(targetDate.getTime()).toFixed(0); var durationMilli = (targetDateMilli - nowMilli).toFixed(0); return durationMilli/day; }

He estado leyendo aquí mucho mientras trato de aprender el idioma y esta es mi primera pregunta. Absolutamente asombrado por las respuestas rápidas y útiles. Siempre aprendiendo, David

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