YNAB Weekly Spending Reports with Google Apps Script
You Need A Budget is an online subscription-based budgeting application that I’ve been using since mid-2015. It’s incredibly helpful in maximizing savings and reducing the guilt in making large purchases, among countless other things. Reddit user doctors_like_cash was looking for a way to email a weekly summary of activity by category, so I whipped up an example using Google Apps Script, which allows you to excute code on a schedule without having to host a server or run anything locally.
Building the Report
You will need to have a Google/Gmail account for this to work, since Google Apps Script uses your Gmail account to send the emails. You will also need a YNAB account, of course.
Generate a YNAB Personal Access Token
First we have to get a Personal Access Token for YNAB. Check out YNAB’s Authentication Overview and follow the instructions to generate a Personal Access Token.
Create a Google Apps Script Project
Google Apps Script lets us run Javascript on Google’s servers for free, which is awesome! Create a new project by clicking here, or by clicking Start Scripting on the Google Apps Script page. I’ve named my project YNAB Weekly Spending Report.
Add The Report Code
Replace the contents of the default Code.gs with the following script. One of the things the report does is go into your Gmail inbox and mark the message as unread. This is necessary if you’re sending to and from the same address, since Google will automatically mark the received message as read. If you’re sending to a non-Gmail address you should remove lines 71-80.
Due to requests from various users on Reddit, I have provided a few alternative scripts that change the way the reports are specified and reported on.
- Standard Report (the version below) - Child categories are specified, the report shows totals for each child category.
- Category Group Detail - Master categories are specified, the report shows totals for each child category.
- Category Group Summary - Master categories are specified, the report shows totals for each master category.
Fill in these fields with your own details:
accessToken
: Your YNAB Personal Access Token
budgetName
: Name of the budget to use
categories
: Your desired category names to monitor, in Javascript array syntax (example provided)
recipient
: Email recipients, comma separated
Run the Report
From the Select function dropdown, choose the sendYnabReport function, then press Run (the play button).
You will need to authorize the project to do a few things:
- Access your Gmail to mark the sent email as unread, otherwise it just sit in your sent box
- Send the report email
- Connect to api.youneedabudget.com
After authorizing the project, you should receive an email that looks like the example at the top of the page.
Schedule the Report
All of this is great, but now we have to schedule the script to run weekly.
Click the timer icon and create a new trigger. I’ve setup a weekly trigger that runs every Monday between midnight and 1 AM.
You’re Done!
And that’s it, now you should get a report every week on your spending habits.
I encourage you to dig through the code and look into adding more columns, links to categories, etc. The opportunities are endless.