Tech Blog :: gdocs :: gdocs


Mar 24 '11 10:34am

Track Freshbooks Expenses in Google Docs with PHP and XML

I've been trying to automate as much of my financial forecasting as possible, with coding up front that will last a while. My primary tools are Freshbooks (for expense and invoice tracking) and Google Docs for spreadsheets. I wrote yesterday about pulling data from one spreadsheet into another using importRange. Last night I took it several steps further, pulling expenses from the Freshbooks API into XML, then XML to GDocs, and automating tax calculations based on expense category.

1. Freshbooks Expenses to XML

Building on an existing freshbooks-php library, I wrote a PHP script called freshbooks_expenses_xml. (Link goes to GitHub.)

To get it set up, create a keys.php file, and put the whole package on your server somewhere. Play with the parameters described in the readme to get different XML output.

2. XML to Google Docs

In cell A1 of a clean spreadsheet, enter this function:
=importXML("http://your-site.com/freshbooks-expenses/expenses.php?date_from=2011-01-01&date_to=2011-12-31&headers=1&", "//expenses/*").
GDocs will fetch the data and populate the spreadsheet. (Note: I had some trouble making the headers consistent with the columns, and worked around it; you might want to do the same by omitting headers=1 in the function and putting in your own.)

3. Making useful tax calculations with the data

For estimated quarterly taxes (as an LLC), I need to know my revenue (calculated in another spreadsheet, not yet but possibly soon also pulled automatically from Freshbooks) minus my business expenses. As I learned doing taxes for 2010, not all expense categories are equal: Meals & Entertainment, for example, is generally deducted at 50%, while others are 100%. This is easy to do with custom GDocs functions. Next to my expenses (pulled automatically), I have a column for Month, a column for Quarter (using a custom function), and a column for Deduction, using the amount and the category. (To write a custom function, go to Tools > Scripts > Script Editor.)

Finally, in my income sheet, I use sumif() on the range in the other [expenses] sheet with the calculated deductions for that quarter, times my expected tax rate, and I know how much quarterly taxes to pay!

(Update: A revised version of this post now appears on the FreshBooks Developer Blog.)

Mar 23 '11 7:58pm
Tags

Google Docs tip: importRange function to pull in other spreadsheet data

A great Google Docs Spreadsheets function I've been using a lot lately is importRange, for pulling in data from one spreadsheet into another. For example, I have one spreadsheet with my income, including calculating my taxes, and another spreadsheet with my budget, where I pull taxes in using importRange. Then I can fill out each sheet and not worry about the other.

The syntax as described in the documentation is:

=importrange("abcd123abcd123", "sheet1!A1:C10")
"abcd123abcd123" is the value in the "key=" attribute on the URL of the target spreadsheet and "sheet1!A1:C10" is the range which is desired to be imported.

A few notes/lessons I've learned:

  • Make sure to use double quotes not single quotes, or it'll give a big "ERROR".
  • The values update when you refresh the spreadsheet, and possibly on some timers, but if you want to update on the fly (say, with both sheets open), go to the start of the importRange block and press Ctrl+R (for Refresh).

Another great function I've been using is sumif (i.e. a conditional sum) - very useful for financial spreadsheets.

Apr 26 '10 9:26pm
Tags

New Google Docs Features

I was wondering why I still wasn't seeing the new Google Docs features weeks after they announced them, then realized I had to turn them on.