Save Paypal Email Receipts in Google Spreadsheet

S
Published in: Gmail - Google Sheets - paypal

When you make a purchase through PayPal, the payment company sends you an email receipt with the details of the transaction. The Google script will scan your Gmail mailbox for all Paypal receipts, extracts the details from the message body using regex and saves them in a Google Spreadsheet for quick reference. The script extracts the transaction ID, the item purchased, the shipping cost and other details.

Also see: Sell Digital Goods with Paypal and Google Drive

function searchGmail() {
  var threads = GmailApp.search('from:paypal', 0, 10);

  var sheet = SpreadsheetApp.getActiveSheet();

  var header = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];

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

    for (var m = 0; m < msgs.length; m++) {
      var response = extractPayPalDetails_(msgs[m]);

      var row = [];

      for (var h = 0; h < header.length; h++) {
        if (header[h] in response) {
          row.push(response[header[h]]);
        } else {
          row.push('');
        }
      }

      sheet.appendRow(row);
    }
  }
}

function extractPayPalDetails_(msg) {
  var result = {};

  var body = msg.getPlainBody().replace(/\s+/g, ' '),
    html = msg.getBody().replace(/\s+/g, ' ');

  var match = /[A-Z]{3}\s\d+,\s\d{4}\s\d{2}:\d{2}:\d{2}\s.{9}/g.exec(body);
  if (match) result['Transaction Date'] = match[1];

  match = /<tr bgcolor="#ffffff"><td.*?>(.*?)<\/td><td.*?><a href.*?>(.*?)<\/a><\/td><td.*?>(.*?)<\/td><td.*?>(.*?)<\/td><td.*?>(.*?)<\/td><\/tr>/g.exec(
    html
  );
  if (match) {
    result['Item #'] = match[1];
    result['Item Title'] = match[2];
    result['Quantity'] = match[3];
    result['Price'] = match[4];
    result['Subtotal'] = match[5];
  }

  match = /Shipping & Handling:\s+\(.*?\)(.*?)\s+Shipping/g.exec(body);
  if (match) result['Shipping and Handling'] = match[1];

  match = /Shipping Insurance.*?:(.*?)\s+Total:\s*(.*? .*?)\s+/g.exec(body);
  if (match) {
    result['Shipping Insurance'] = match[1];
    result['Total'] = match[2];
  }

  match = /credit card statement as "(.*?)".*?purchased from:(.*?)\s+Receipt id:([\d\-]+)/gi.exec(
    body
  );
  if (match) {
    result['Name in Statement'] = match[1];
    result['Purchased From'] = match[2];
    result['Receipt ID'] = match[3];
  }

  match = /international shipping.*?Total:(.*?)\s+.*credit card statement as "(.*?)"/gi.exec(
    body
  );
  if (match) {
    result['International Shipping Total'] = match[1];
    result['International Name in Statement'] = match[2];
  }

  return result;
}
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Gmail - Google Sheets - paypal

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube