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

224
Vistas
How can I generate the deadlines given 03 parameters in Google Apps Script?

Given 3 parameters, I'd like to know why this is not iterating/generating the dates, but repeating the first iteration.

//Means 01 installement for tomorrow and the other 6 from tomorrow on every 30 days
function generateInstallments() {
let condition = '1 + 6x'
let firstInstallment = 1;//Tomorrow
let intervalInDays = 30;
var dueDate = '';
let deadlines = [];

if (condition.indexOf('+') > -1) { //If it contains this pattern (1 + 4x)
    //Extracts the number preceding x, as this will be the number of installments
    condition= Number(condition.slice(
      condition.indexOf('+ ') + 2,
      condition.lastIndexOf('x'),
    ));

    let firstInstallementDate = addDays(new Date(), firstInstallment );
    deadlines.push(Utilities.formatDate(firstInstallementDate , Session.getTimeZone(), "dd/MM/yyyy"));

    for (let a = 0; a < condition; a++) {
      dueDate = addDays(firstInstallementDate , intervalInDays );//Function can be found below
      deadlines.push(Utilities.formatDate(dueDate, Session.getTimeZone(), "dd/MM/yyyy"));
    }
  }
}

The first and 2 second dates are correct, but the other 4 are the same as the second one, so this is not iterating as expected.

This is the function I'm using to add the days:

function addDays(date, days) {
  var result = new Date(date);
  result.setDate(result.getDate() + days);
  return result;
}

Thank you!

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

0

When I saw your script, firstInstallementDate is used in the loop. In this case, the value of firstInstallementDate is not updated. I thought that this might be the reason for your issue. If my understanding is correct, how about the following modification?

From:

for (let a = 0; a < condition; a++) {
  dueDate = addDays(firstInstallementDate , intervalInDays );//Function can be found below
  deadlines.push(Utilities.formatDate(dueDate, Session.getTimeZone(), "dd/MM/yyyy"));
}

To:

for (let a = 0; a < condition; a++) {
  firstInstallementDate = addDays(firstInstallementDate, intervalInDays);
  deadlines.push(Utilities.formatDate(firstInstallementDate, Session.getTimeZone(), "dd/MM/yyyy"));
}
  • By this modification, the following result is obtained.

      [ 
        '09/03/2022',
        '08/04/2022',
        '08/05/2022',
        '07/06/2022',
        '07/07/2022',
        '06/08/2022',
        '05/09/2022'
      ]
    
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