Get QuickBooks Data into Google Sheets with Apps Script
A recent project involved pulling payments, invoices and accounting data from QuickBooks online into a Google Spreadsheet in near real-time. The integration was done through Google Apps Script and the QuickBooks API (v3). You also need to include OAuth 1.0 library in your Google Script project (QBO doesn’t support the OAuth 2.0 protocol yet).
To get started, go to your QuickBooks Sandbox, create a sample app and get the Consumer Key and Consumer Secret. Next authorize the connection to let Google Sheets access your company inside QuickBooks. The companyId will be stored as a property inside Google Scripts and all subsequent API calls will be made for the authorized company.
Here’s a sample snippet that fetches the invoices data from QuickBooks into a Google Spreadsheet. We’ve added a filter in the SELECT query to only fetch invoices that were created in the last hour. You can set this is a time-based trigger to auto-fetch QuickBooks data into the spreadsheet.
function getInvoicesFromQuickBooks() {
try {
var service = getQuickBooksService_();
if (!service || !service.hasAccess()) {
Logger.log('Please authorize');
return;
}
var props = PropertiesService.getUserProperties(),
companyId = props.getProperty('QuickBooks.companyID');
var date = new Date(new Date().getTime() - 1000 * 60 * 60).toISOString();
var query = "SELECT * FROM Invoice WHERE Metadata.CreateTime > '" + date + "'";
var url = 'https://quickbooks.api.intuit.com/v3/company/';
url = +companyId + '/query?query=' + encodeURIComponent(query);
var response = service.fetch(url, {
muteHttpExceptions: true,
contentType: 'application/json',
headers: {
Accept: 'application/json',
},
});
var result = JSON.parse(response.getContentText());
var invoices = result.QueryResponse.Invoice;
for (var i = 0; i < invoices.length; i++) {
var Invoice = invoices[i];
sheet.appendRow([
Invoice.Id,
Invoice.time,
Invoice.Deposit,
Invoice.DocNumber,
Invoice.DepartmentRef.name,
Invoice.CustomerRef.name,
Invoice.ShipAddr.Line1,
JSON.stringify(Invoice.Line),
Invoice.ShipDate,
Invoice.TrackingNum,
Invoice.PaymentMethodRef.name,
Invoice.TotalAmt,
Invoice.Balance,
]);
}
} catch (f) {
log_('INVOICES ERROR: ' + f.toString());
}
}
The script can be further enhanced to extract details of individual line items like the SKU / Part number, Quantity left, and so. This would however require a separate Rest API call to the following endpoint.
https://quickbooks.api.intuit.com/v3/company/companyId/item/' + itemId
Amit Agarwal
Google Developer Expert, Google Cloud Champion
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.
Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory