Automate invoice management in Gmail with Document AI and Google Apps Script

Stéphane Giron
4 min readMar 26, 2023

--

Are you tired of manually managing your invoices in Gmail? Say goodbye to the hassle and hello to efficiency with Document AI and Google Apps Script. In this article, we’ll show you how to automate your invoice management process and streamline your workflow. Get ready to save time and increase productivity with these powerful tools.

What is Document AI

Document AI, a cutting-edge AI tool from Google, empowers you to extract structured data from documents. With its pre-trained models for data extraction, Document AI offers a comprehensive solutions suite that includes the Document AI Workbench for creating new custom models or uptraining existing ones, and the Document AI Warehouse for searching and storing documents.

What is Apps Script

If you follow me you now I’m in love with Apps Script an amazing tool to build cool apps on top of Google Workspace.

If you just jump on this article randomly Google Apps Script is a scripting platform developed by Google for light-weight application development in the Google Workspace platform. It offers a user-friendly platform for creating powerful applications, whether you’re a seasoned developer or just starting out.

Setup Document AI

Go to your Google Cloud Platform console (link) and create a new project.

Enable billing, price of Document AI is 0.10$ per invoice lower than 10 pages. If you create a new GCP account you will get a 300$ credit, so you can run a test on 3 000 invoices !

Go to the “Processor Library” (link) and search for “Invoice”, then click “Create Processor”.

Create Document AI Processor in GCP

In the next Screen you can define a name and a region (US or Europe).

On the processor screen you will need to copy the “Prediction endpoint”;

Document AI Invoice processor screen in Google Cloud

Now all is ok we can now query the processor with Apps Script.

Apps Script Code to extract data from invoice file

The full code will be shared at the end of the article, in this section we just cover the code for sending invoice to Document AI and receive the response.

function queryDocumentAI(bytes,contentType){
const url = 'https://us-documentai.googleapis.com/v1/projects/123456789:process';
const encoded = Utilities.base64Encode(bytes);
let body = {
"skipHumanReview": true,
"fieldMask": "entities",
"rawDocument": {
"content": encoded,
"mimeType": contentType
}
}
var param = {
method : "POST",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
"contentType" : "application/json",
"payload":JSON.stringify(body, null, 2),
muteHttpExceptions:true,
};
var txt = UrlFetchApp.fetch(url,param).getContentText();
return JSON.parse(txt)
}

Request is quite easy :

To retrieve all entities from the filie we use “fieldMask”: “entities”. For authentication we add the GCP scope in the manifest file of the Apps Script file so we can get the Access Token from ScriptApp.getOAuthToken().

At the end we return the response as a JSON file.

How we automate invoice management

Full diagram of invoice automation in Gmail with Document AI and Apps Script

Explanation :

  • Emails are received in Gmail, they can stay in the inbox or be in a specific label.
  • Apps script will check if there is new emails, when there is a new emails it extracts the data and send the invoices attached to Document AI.
  • Document AI perform analysis of the file and return a json structured file with all items identified.
  • Apps Script will then store the data extracted in a Google Sheets and archive the file in a dedicated Google Drive folder.

Here the database :

Example of data extracted from Document AI

Result

It is now possible to extract data from invoice and gather it in a Google Sheets with file archived in Google Drive. With this script you can start analysing the invoices received by your company.

Although Google Sheets may not be the optimal storage location for data collection, utilizing an API allows for the flexibility to save data in a preferred location. The crucial aspect is the ability to begin analyzing invoices and determining the feasibility of implementing automated processing.

Give this program a try for just one month and then analyse the output. Could be a good surprise how far you are to automate treatment and if it is not the case, you can determine the way of improvement.

Code

You can access the code on the Devoteam G Cloud GitHub repository : link

📢 If you are a Google Workspace client ad want some help to setup the script you can book a slot here : link

--

--