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

375
Visualizações
How to get a value if the present time is within a time window?

I have been trying to copy the value in A9 into the cell in column D, when the current time (now or time.new) corresponds to the time specified in the same row in column C.

I managed to get a script that pulls the value D1 once in an hour into the last cell in column D, which would be perfectly fine if the "time-driven" trigger would not pick a random time in the hour. It needs to be exactly 2 minutes past every full hour (+/- 30 seconds). So I thought to make the script run every minute and paste the value in a cell if the time in colum C is within 1h2m30s and 1h1m30s before the present time... But with my script there seems to be an error.

Please find the test document here, where the function is currently being executed once every hour.

The Apps Script that runs at a random time every hour is as follows (currently running):

function myFunction() {
  var ss= SpreadsheetApp.getActiveSpreadsheet();
  var sheet= ss.getSheetByName('FACTS & PREDICTIONS');
  var Direction=SpreadsheetApp.Direction;
  let aLast =sheet.getRange("D"+(sheet.getLastRow()+1)).getNextDataCell(Direction.UP).getRow();
  let val= sheet.getRange('A9').getDisplayValue();
  sheet.getRange(`D${aLast+1}`).setValue(val);
}

The new script, that I have been trying to make is as follows:

function myFunction() {
  var ss= SpreadsheetApp.getActiveSpreadsheet();
  var sheet= ss.getSheetByName('Admin Panel');
  var Direction=SpreadsheetApp.Direction;
   if(e.source.getActiveSheet().getName()=="FACTS & PREDICTIONS") {
    if(col == 4 && cell.offset(0,-1).getValue() > new Date()-TIME(1,2,30) && col == 4 && cell.offset(0,-1).getValue() < new Date()-TIME(1,1,30)){
      let val= sheet.getRange('A9').getDisplayValue();
     e.source.getActiveSheet().getRange(row,4).setValue(val);
    }
  }
}

The above script is actually patched together because I have no clue of Apps Scrips. I have no idea what is wrong with the code or what I need to change.

Edit: Just fount this, where you can make a Trigger locked to a specific minute, BUT it has +/- 15 minutes deviation. Useless...

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Using a time-driven trigger to run every minute might be the right approch but the expressions comparing Date objects are wrong because comparing them always will be false.

As you are new to Google Apps Script, you might find worthy to spend some time learning the basics and common quirks about Google Apps Script and JavaScript, i.e. Compare two dates with JavaScript

Also you might have to rethink if Google Sheets / Apps Script is the right tool for this task specially when requiring to work with precise times / small time tolerances and requiring high reliability. This because the Google Apps Script triggers are not 100% reliable, i.e. the trigger might fail to run all and every minute, time-driven triggers have a daily total time execution quota.

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