Forked from latetedemelon/ynab_to_google_sheets.js
Last active
February 5, 2020 08:20
-
-
Save Freika/94763a74b5d4ee85a91ed40dab614e97 to your computer and use it in GitHub Desktop.
Credit to Brady from the YNAB team. Assembled from here: https://support.youneedabudget.com/t/k9rxc9/using-google-apps-script-with-the-api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function get_ynab_categories(accessToken, budgetId) { | |
const groups = fetch_ynab_data(accessToken, "budgets/" + budgetId + "/categories").category_groups; | |
const columns = ["Name", "Budgeted", "Activity", "Balance"]; | |
const rows = []; | |
for (var group_idx = 0; group_idx < groups.length; group_idx++) { | |
// Add the group | |
var group = groups[group_idx]; | |
// Skip internal and hidden categories | |
if (['Internal Master Category', 'Hidden Categories'].indexOf(group.name) >= 0) continue; | |
rows.push([group.name]); | |
// Add the categories | |
for (var category_idx = 0; category_idx < group.categories.length; category_idx++) { | |
var category = group.categories[category_idx]; | |
var name = " " + category.name; // Indent categories a bit so they are offset from groups | |
// Calculate currency amounts from mulliunits | |
var budgeted = category.budgeted / 1000.0; | |
var activity = category.activity / 1000.0; | |
var balance = category.balance / 1000.0; | |
rows.push([name, budgeted, activity, balance]); | |
} | |
} | |
return [columns].concat(rows); | |
} | |
function fetch_ynab_data(accessToken, path){ | |
const url = "https://api.youneedabudget.com/v1/" + path; | |
const options = { | |
"headers": { | |
"Authorization": "Bearer " + accessToken | |
} | |
}; | |
const response = UrlFetchApp.fetch(url, options); | |
const data = JSON.parse(response.getContentText()).data; | |
return data; | |
} | |
function onOpen() { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var entries = [{ | |
name : "Refresh Categories", | |
functionName : "refresh_ynab_categories" | |
}]; | |
sheet.addMenu("YNAB", entries); | |
}; | |
function refresh_ynab_categories() { | |
SpreadsheetApp.getActiveSpreadsheet().getRange('E1').setValue(new Date().toTimeString()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment