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

457
Views
Conversión de correos electrónicos a PDF si el asunto es "xxxxx" y la fecha es hoy

Escribí un script que tiene un disparador para ejecutarse cada hora, busca mis correos electrónicos y extrae los datos, los convierte en un PDF y los ubica en una carpeta si el asunto es "xxxx", sobrescribe los datos en la carpeta si ya existe.. Me gustaría agregar una nueva función if donde solo recogerá los correos electrónicos del día actual y no el historial (para ahorrar eficiencia del script y los datos)

 function saveGmailAsPDF2() { var gmailLabels = 'xxxxx'; var driveFolder = DriveApp.getFolderById("folder url"); var threads = GmailApp.search("subject:" + gmailLabels,0,20); if (threads.length > 0) { /* Google Drive folder where the Files would be saved */ var folders = DriveApp.getFoldersByName(driveFolder); var folder = folders.hasNext() ? folders.next() : DriveApp.createFolder(driveFolder); /* Gmail Label that contains the queue */ var label = GmailApp.getUserLabelByName(gmailLabels) ? GmailApp.getUserLabelByName(gmailLabels) : GmailApp.createLabel(driveFolder); for (var t=0; t<threads.length; t++) { threads[t].removeLabel(label); var msgs = threads[t].getMessages(); var html = ""; var subject = threads[t].getFirstMessageSubject(); var previousdoc = driveFolder.getFilesByName(subject + ".pdf"); //gets name of csv, if exists in drive then move to trash before creating new file if (previousdoc.hasNext()) { previousdoc.next().setTrashed(true);} /* Append all the threads in a message in an HTML document */ for (var m=0; m<msgs.length; m++) { var msg = msgs[m]; html += "From: " + msg.getFrom() + "<br />"; html += "To: " + msg.getTo() + "<br />"; html += "Date: " + msg.getDate() + "<br />"; html += "Subject: " + msg.getSubject() + "<br />"; html += "<hr />"; html += msg.getBody().replace(/<img[^>]*>/g,""); html += "<hr />";} /* Convert the Email Thread into a PDF File */ var tempFile = DriveApp.createFile("temp.html", html, "text/html"); folder.createFile(tempFile.getAs("application/pdf")).setName(subject + ".pdf"); tempFile.setTrashed(true);}}}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Explicación:

  • Hay una función getLastMessageDate() que da la fecha del mensaje más reciente de un hilo. Puede comparar eso con la fecha de hoy y, si coinciden, ejecutar el resto del código.

  • Dado que usa un bucle for: for (var t=0; t<threads.length; t++) creo que la condición if (threads.length > 0) es redundante porque for loop no se ejecutará de todos modos si threads.length es igual a 0 .

  • Debido a esto último, transfirí la parte de creación de la carpeta de su código al bucle mismo y lo ejecuté solo cuando el último día del hilo es igual a hoy. Pero dejo esta parte para que ustedes decidan.

No puedo probar tu código, pero esto debería funcionar:

 function saveGmailAsPDF2() { var gmailLabels = 'xxxxx'; var driveFolder = DriveApp.getFolderById("folder url"); var threads = GmailApp.search("subject:" + gmailLabels,0,20); // new code const today = new Date(); const ss = SpreadsheetApp.getActive(); const timezone = ss.getSpreadsheetTimeZone(); const today_dt = Utilities.formatDate(today, timezone, 'yyyy-MM-dd'); /////////// for (var t=0; t<threads.length; t++) { // new code var lastMessageDate = threads[t].getLastMessageDate(); var thread_dt = Utilities.formatDate(lastMessageDate, timezone, 'yyyy-MM-dd'); if(today_dt==thread_dt){ /////////// /* Google Drive folder where the Files would be saved */ var folders = DriveApp.getFoldersByName(driveFolder); var folder = folders.hasNext() ? folders.next() : DriveApp.createFolder(driveFolder); /* Gmail Label that contains the queue */ var label = GmailApp.getUserLabelByName(gmailLabels) ? GmailApp.getUserLabelByName(gmailLabels) : GmailApp.createLabel(driveFolder); threads[t].removeLabel(label); var msgs = threads[t].getMessages(); var html = ""; var subject = threads[t].getFirstMessageSubject(); var previousdoc = driveFolder.getFilesByName(subject + ".pdf"); //gets name of csv, if exists in drive then move to trash before creating new file if (previousdoc.hasNext()) { previousdoc.next().setTrashed(true);} /* Append all the threads in a message in an HTML document */ for (var m=0; m<msgs.length; m++) { var msg = msgs[m]; html += "From: " + msg.getFrom() + "<br />"; html += "To: " + msg.getTo() + "<br />"; html += "Date: " + msg.getDate() + "<br />"; html += "Subject: " + msg.getSubject() + "<br />"; html += "<hr />"; html += msg.getBody().replace(/<img[^>]*>/g,""); html += "<hr />";} /* Convert the Email Thread into a PDF File */ var tempFile = DriveApp.createFile("temp.html", html, "text/html"); folder.createFile(tempFile.getAs("application/pdf")).setName(subject + ".pdf"); tempFile.setTrashed(true);}}}
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!