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

107
Visualizações
Get Emails from Gmail using Google App Script as 10 mins before to current time

I want to search email using Google App Script as 10 mins before to current time, I wrote the below script but it's not working, as I have emails with the same subject as defined in query and before 10 mins(even 1 min before mails are there) but GAS showing zero threads.

function testforemails(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Emails");
  var Gmail = GmailApp;
  var lasttime = sheet.getRange("Z1").getValue(); // it will use later as the time of last searched email.
  Logger.log(lasttime);
  var cdate = new Date();
  var ctime = cdate.getTime();
  var qDate = new Date(ctime - 600000); // less 10 minutes from current time.
  Utilities.formatDate(qDate, Session.getScriptTimeZone(), "dd-MMM-yy hh:mm a")
  Logger.log("QDATE IS " + qDate); //  perfectly displaying time less than 10 minutes
  var qtime = qDate.getTime();
  Logger.log("qtime is " + qtime);

  // SEARCH EMAIL
  var query = 'subject: defined subject of emails, after:' + (qDate);
  var threadsNew = Gmail.search(query);
  Logger.log(threadsNew.length);
  var folderid = 'define folder id'
  var folder = DriveApp.getFolderById(folderid);
  for(var n in threadsNew){
    var thdNew  = threadsNew[n]; 
    var msgsNew = thdNew.getMessages(); 
    var msgNew = msgsNew[msgsNew.length-1];
  // GET ATTACHMENT
    var bodyNew = msgNew.getBody();
    var plainbody  = msgNew.getPlainBody();
    var subject = msgNew.getSubject();
    var Etime = msgNew.getDate();
    var attachments = msgNew.getAttachments();
    var attachment = attachments[0];
    
    Logger.log(Etime);
    Logger.log(subject);
  }
    
    Logger.log(threadsNew.length);
    
    
}

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

0

Update

This is accualy can be done, see the asnwer of @Iamblichus. Anyway, my method also works so I keep for the user to choose which method better for his needs.


A workaround to achive this goal:

function testforemails(){
  // SEARCH EMAIL
  let now = new Date();
  let query = 'subject: YOUR_SUBJECT, after:' + now.toISOString().slice(0, 10); // find mails from today
  console.log(query);
  let threadsNew = GmailApp.search(query);
  console.log("threadsNew.length: " + threadsNew.length);

  for(let thread of threadsNew){
    let lastMsgTime = thread.getLastMessageDate();
    let tenMinutesAgo = new Date(now - 600000);

    if(lastMsgTime - tenMinutesAgo > 0) { // if the last message on the thread was less than 10 minutes ago
      // do your magic here
      console.log(thread.getLastMessageDate())
    }  
  }    
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Issue:

In Gmail search operators, before and after timestamps work with seconds, not with milliseconds.

Solution:

Divide the timestamp by 1000, making sure it's an integer:

function testforemails(){
  var cdate = new Date();
  var ctime = cdate.getTime();
  var qDate = new Date(ctime - 600000); // less 10 minutes from current time.
  var query = 'after:' + Math.floor(qDate.getTime()/1000);
  var threadsNew = GmailApp.search(query);
  Logger.log(threadsNew.length);
  // ...
}

Other issues:

  • In your query you provided the date directly, without transforming to the number of milliseconds. This cannot work:
var qDate = new Date(ctime - 600000);
var query = 'subject: defined subject of emails, after:' + (qDate);

Related:

  • Is it possible to query Gmail messages by timestamp?
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