Skip to content

Instantly share code, notes, and snippets.

@CodeLeom
Created March 1, 2024 20:03

Revisions

  1. CodeLeom created this gist Mar 1, 2024.
    24 changes: 24 additions & 0 deletions GmailToSheet.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    function logEmailsToSheet() {
    // Specify the subject line to search for in Gmail
    var query = 'subject:"Feedback Submission"';
    // Access the active spreadsheet and the first sheet
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    // Search Gmail with the given query
    var threads = GmailApp.search(query);

    for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();

    for (var j = 0; j < messages.length; j++) {
    var message = messages[j];
    // Extract date, sender, subject, and body from each email
    var date = message.getDate();
    var from = message.getFrom();
    var subject = message.getSubject();
    var body = message.getPlainBody();

    // Append the data to the sheet
    sheet.appendRow([date, from, subject, body]);
    }
    }
    }