当前位置:首页 > 未分类 > 正文内容

Get QuickBooks Data into Google Sheets with Apps Script

ceacer3周前 (05-02)未分类2723

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

相关文章

How to Automatically Rename Files in Google Drive with Apps Script and AI

How to Automatically Rename Files in Google Drive with Apps Script and AI

Do you have image files in your Google Drive with generic names like IMG_123456.jpg or Screenshot.pn...

How to Mail Merge with Outlook and Google Sheets

How to Mail Merge with Outlook and Google Sheets

The Mail merge add-on for Gmail lets you send personalized emails to your contacts in bulk. You can...

How to Transcribe Audio and Video Attachments in Gmail

How to Transcribe Audio and Video Attachments in Gmail

The Save Gmail to Google Drive add-on lets you automatically download email messages and file attach...

How to Play an MP3 File in Google Sheets

How to Play an MP3 File in Google Sheets

You can put the link of any MP3 audio file in Google Sheets but when you click the file link, the au...

How to Use Conditional Formatting in Google Sheets to Highlight Information

How to Use Conditional Formatting in Google Sheets to Highlight Information

Conditional formatting in Google Sheets makes it easy for you to highlight specific cells that meet...

How to Use Hyperlinks in Google Sheets

How to Use Hyperlinks in Google Sheets

This guide explains how you can easily create and manage hyperlinks in Google Sheets. An entire cell...