Jump to content

Get QuickBooks Data into Google Sheets with Apps Script


Recommended Posts

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

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. to insert a cookie message