1

From https://www.youtube.com/watch?v=GSeGoXYfyGw and https://www.youtube.com/watch?v=gdgCVqtcIw4&

I would be able to search an email thread and save it's metadata to a spreadsheet.

However, I am not sure how to save that email thread as a pdf to google drive or local PC.

  • The pdf contents would be as if you were printing an email thread as a PDF.
  • Is this outside the scope of google sheet scripting?

Any guidance would be much appreciated.


Code from https://www.youtube.com/watch?v=gdgCVqtcIw4&

var ui = SpreadsheetApp.getUi();
function onOpen(e){
  
  ui.createMenu("Gmail Manager").addItem("Get Emails by Label", "getGmailEmails").addToUi();
  
}

function getGmailEmails(){
  var input = ui.prompt('Label Name', 'Enter the label name that is assigned to your emails:', Browser.Buttons.OK_CANCEL);
  
  if (input.getSelectedButton() == ui.Button.CANCEL){
    return;
  }
  
  var label = GmailApp.getUserLabelByName(input.getResponseText());
  var threads = label.getThreads();
  
  for(var i = threads.length - 1; i >=0; i--){
    var messages = threads[i].getMessages();
    
    for (var j = 0; j <messages.length; j++){
      var message = messages[j];
      if (message.isUnread()){
        extractDetails(message);
        GmailApp.markMessageRead(message);
      }
    }
    threads[i].removeLabel(label);
    
  }
  
}

function extractDetails(message){
  var dateTime = message.getDate();
  var subjectText = message.getSubject();
  var senderDetails = message.getFrom();
  var bodyContents = message.getPlainBody();
  
  var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  activeSheet.appendRow([dateTime, senderDetails, subjectText, bodyContents]);
}
@MoayadAbuRmilah
SeriousTyro
  • 934
  • 3
  • 10
  • 16
  • 1
    You can refer to these threads for reference in creating a pdf from an email: https://stackoverflow.com/questions/16946168/automatically-convert-emails-with-a-gmail-label-to-pdf-and-send-it-to-an-email-a https://stackoverflow.com/questions/59690350/google-apps-script-convert-html-with-styling-to-pdf-and-attach-to-an-email-c – Jason E. May 06 '21 at 15:13

1 Answers1

0

Another reference https://www.labnol.org/code/19117-save-gmail-as-pdf

Thanks @Jason E.


Seems like I'll have to combine google sheets and apps scripting.

SeriousTyro
  • 934
  • 3
  • 10
  • 16