当前位置:首页 > 未分类 > 正文内容

Save Gmail Messages to a Google Spreadsheet

ceacer3周前 (05-02)未分类9562

The Google Script will save the body of email messages from Gmail to the currently active worksheet inside your Google Spreadsheet. You need to specify the Gmail search query and the sheet ID where the matching messages are to be exported. It saves the text content of the message sans any HTML tags or images.

To get started, paste the code in the script editor of a Google Spreadsheet and run SaveEmail from the Run menu.

Also see: Save Gmail Attachment to Google Drive

var SEARCH_QUERY = 'label:inbox is:unread to:me';

/*
 Credit: Alexander Ivanov
 https://gist.github.com/contributorpw/70e04a67f1f5fd96a708
*/

function getEmails_(q) {
  var emails = [];
  var threads = GmailApp.search(q);
  for (var i in threads) {
    var msgs = threads[i].getMessages();
    for (var j in msgs) {
      emails.push([
        msgs[j]
          .getBody()
          .replace(/<.+?>/g, '\n')
          .replace(/^\s*\n/gm, '')
          .replace(/^\s*/gm, '')
          .replace(/\s*\n/gm, '\n'),
      ]);
    }
  }
  return emails;
}

function appendData_(sheet, array2d) {
  sheet.getRange(sheet.getLastRow() + 1, 1, array2d.length, array2d[0].length).setValues(array2d);
}

function saveEmails() {
  var array2d = getEmails_(SEARCH_QUERY);
  if (array2d) {
    appendData_(SpreadsheetApp.getActiveSheet(), array2d);
  }
}

相关文章

How to Create Personalized Images in Bulk with Google Sheets

How to Create Personalized Images in Bulk with Google Sheets

Yesterday marked Friendship Day, and to celebrate, I sent a personalized image to each of my friends...

How to Make Personalized Place Cards with Guest Names

How to Make Personalized Place Cards with Guest Names

Whether it is a wedding party or a business conference, those tent-shaped place cards are ideal for...

How to Generate Dynamic QR Codes to Collect Payments through UPI

How to Generate Dynamic QR Codes to Collect Payments through UPI

The BHIM UPI payment system has transformed the way we pay for goods and services in India. You scan...

Find Product Prices in Google Sheets with Vlookup and Match Functions

Find Product Prices in Google Sheets with Vlookup and Match Functions

You run a coffee shop and you are looking for a spreadsheet formula to quickly look up prices of the...

How to Request Payments with Razorpay and Google Sheets

How to Request Payments with Razorpay and Google Sheets

Razorpay is a popular payment gateway in India that allows you to accept online payments from custom...

Essential Date Functions for Google Sheets

Essential Date Functions for Google Sheets

Dates are internally stored as sequential serial numbers in Google Sheets. This serial number repre...