Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
Reciba correos electrónicos de Gmail usando Google App Script como 10 minutos antes de la hora actual

Quiero buscar correos electrónicos usando Google App Script 10 minutos antes de la hora actual. Escribí el siguiente script pero no funciona, ya que tengo correos electrónicos con el mismo asunto definido en la consulta y antes de 10 minutos (incluso 1 minuto antes de que se envíen los correos). allí) pero GAS muestra cero subprocesos.

 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 answers
Answer question

0

Actualizar

Esto es exactamente lo que se puede hacer, vea la respuesta de @Iamblichus. De todos modos, mi método también funciona, así que dejo que el usuario elija qué método se adapta mejor a sus necesidades.


Una solución para lograr este objetivo:

 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 Report

0

Tema:

En los operadores de búsqueda de Gmail, las marcas de tiempo before y after funcionan con segundos, no con milisegundos.

Solución:

Divide la marca de tiempo por 1000, asegurándote de que sea un número entero:

 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); // ... }

Otros asuntos:

  • En su consulta proporcionó la fecha directamente, sin transformar a la cantidad de milisegundos. Esto no puede funcionar:
 var qDate = new Date(ctime - 600000); var query = 'subject: defined subject of emails, after:' + (qDate);

Relacionado:

  • ¿Es posible consultar los mensajes de Gmail por marca de tiempo?
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!