Send email receipts in Gmail to Wave Accounting automatically in 2 seconds

Wave Accounting is a pretty decent cloud accounting solution that is used by many small business owners the world over.

But let’s be honest, the way you add email receipts for expense tracking can only be described as painful.

You can submit a receipt by email, but the user experience is pretty brutal. First you have to print/save the e-mail as a PDF to your computer. Then you have the compose a new e-mail to [email protected] and then re-upload and attach the PDF file. Finally, you have to remove the unwanted receipt PDF from your computer.

What The Fuck Wtf GIF - Find & Share on GIPHY

Fortunately, there is a much better solution for those who use Gmail or G Suite, which is to leverage a simple Google Apps Script.

This script will process all your email receipts, product a nice printable PDF, and then send the receipt automatically to Wave Accounting.

This will take about 5 minutes to set up, but will save hours of your life. Automation is your friend.

Step 1 – Create Labels

First, go into your Gmail account and create two labels, as shown below.

Step 2 – Set up Google Apps Script

Go to Google Apps Script and create a new script. In the body, paste the code below.

/*  
 * Before running, you need to add the GmailUtils Library.
 * To utilize this library, select Resources > Libraries... and enter the following Script ID:
 * 1CdCFwL5USI7KRfuVfoOWXJUCo4kGTL05fRwDPmpJVzETix09Nuy_v8bK
 * You also need to authorize your account. Instructions -> https://developers.google.com/gmail/api/quickstart/apps-script
 *
 * Note: There is an execution limit of 5 min for a script, which will process around 25 emails. However, the script will
 * continue where it left off on next execution, so this is not a problem.
 */

function SendExpensesToWave()
{
    //query emails that are in the queue to process
    var threads = GmailApp.search('label:receipt');
    
    //get label ID for Submitted so that it can be applied later to completed items
    var submittedLabel = GmailApp.getUserLabelByName('receipt-processed')
    var receiptLabel = GmailApp.getUserLabelByName('receipt')

    for (var i=0; i<threads.length; i++)
    {
    for (var j=0; j<threads[i].getMessageCount(); j++)
    {
        //get message to work on
        var message = threads[i].getMessages()[j];
        //save to pdf
        var pdf = GmailUtils.messageToPdf(message);
        
        //send to Wave, Wave automatically applies receipts to your primary business based on the email address it's coming from
        GmailApp.sendEmail('[email protected]', 'Receipt', '', {attachments: [pdf]});
    }
    //add submitted label and remove receipt from queue
    threads[i].addLabel(submittedLabel);
    threads[i].removeLabel(receiptLabel);
    }
  
}

//test function to list all labels
function listLabelsTest() {
  var response = Gmail.Users.Labels.list('me');
  if (response.labels.length == 0) {
    Logger.log('No labels found.');
  } else {
    Logger.log('Labels:');
    for (var i = 0; i < response.labels.length; i++) {
      var label = response.labels[i];
      Logger.log('- %s', label.name);
    }
  }
}

Now go to Resources, Libraries and add the Script ID for the GMailUtils library:

1CdCFwL5USI7KRfuVfoOWXJUCo4kGTL05fRwDPmpJVzETix09Nuy_v8bK

You should now see a screen like the one below.

Step 3 – Test your setup

Now you have set up the script, go to the menubar and select the listLabelsTest function.

Now click on View, Logs and you should see a list of labels retrieved from your Gmail account, including the two this script uses to process receipts.

Label Test results

Once we know connectivity and libraries are configured correctly, you could also manually run the SendExpensesToWave function after tagging some emails to check it is all working. After a minute or so, you should see an email in your sent folder to Wave with the attached PDF.

Step 4 – Set the script to run automatically

Now you can create a Trigger to run the script every day to process and send your receipts to Wave. From the project Dashboard, open the Triggers page.

Now you can configure your Trigger. Make sure you select the correct function to send the receipts, rather than the label testing function.

You want a Time-driven event, and I chose a day timer (per day) to run in the middle of the night.

Step 5 – Use the script

Assuming you have set everything up correctly, all you need to do now is add the receipt label to email receipts that you want to send to Wave Accounting. Simply press L when viewing or having selected the e-mail, add the label, and wait for your Google Apps Script to run that night.

You can also use the Receipts App to verify the receipt data before posting it to your accounting record.

Credits

This script is based on the original GmailUtils library from PixelCog, which sadly no longer seems to be maintained. I have created my own copy in Google App Scripts, which was updated to work with version 8 of the App Scripts runtime, and this is the library you are using above.