Automate Gmail to Google Docs with Apps Script

Sending yourself an email is sometimes the faster way to get a note down. I don’t know if I’ll ever get away from that habit. I’m still bummed Google Voice assistant took that capability away 🙁

But still, I had a thought that it would be convenient to have certain emails automatically add content to a Google Doc, and get them out of my inbox with some automation.

Normally I would use Zapier for something like this. And it does have this capability. But the free plan only supports up to 100 tasks/mo, and the paid tier starts at over $40/month.

With the added support of AI support I realize that using Google Apps Script is waay more accessible these days and worth considering for something like this. This was completely free and runs on a schedule every hour but you could set up various triggers for this.

The script automates the process of appending specific Gmail emails to a Google Doc based on a label you assign. When new emails are tagged (manually or via filters), the script pulls their subject, date, and content, adds them to an ongoing document, and updates their label to mark them as processed—perfect for organizing and centralizing information without manual effort.

 function appendEmailsToDoc() {
  var triggerLabelName = "labelname1"; // Label to identify emails to process
  var processedLabelName = "labelname1-archive"; // Label to apply after processing
  var docId = "XxXxXXXXXXXXXXXXXXXXXXXXXXXXXX"; // Google Doc ID
  // Access Gmail labels
  var triggerLabel = GmailApp.getUserLabelByName(triggerLabelName);
  var processedLabel = GmailApp.getUserLabelByName(processedLabelName);
  if (!triggerLabel) {
    Logger.log("Trigger label not found: " + triggerLabelName);
    return;
  }
  if (!processedLabel) {
    Logger.log("Processed label not found: " + processedLabelName);
    processedLabel = GmailApp.createLabel(processedLabelName); // Create label if it doesn't exist
  }
  // Get threads with the trigger label
  var threads = triggerLabel.getThreads();
  if (!threads || threads.length === 0) {
    Logger.log("No emails found with label: " + triggerLabelName);
    return;
  }
  // Open the Google Doc
  var doc = DocumentApp.openById(docId);
  var body = doc.getBody();
  threads.forEach(thread => {
    var messages = thread.getMessages();
    messages.forEach(message => {
      var subject = message.getSubject();
      var content = message.getPlainBody();
      var timestamp = message.getDate();
      // Append email content to Google Doc
      body.appendParagraph("Subject: " + subject);
      body.appendParagraph("Date: " + timestamp);
      body.appendParagraph(content);
      body.appendParagraph("------------------------------");
    });
    // Remove the trigger label and add the processed label
    triggerLabel.removeFromThread(thread);
    processedLabel.addToThread(thread);
  });
}

Leave a Comment

You must be logged in to post a comment. Don't worry you can easily use your Google or Facebook account I made it easy to do..