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

222
Visualizações
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 Respostas
Responde à pergunta

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