Skip to content

Instantly share code, notes, and snippets.

@misaka
Last active March 19, 2023 13:04
Show Gist options
  • Save misaka/1e18b6b444c846e4693b84fe64f926f7 to your computer and use it in GitHub Desktop.
Save misaka/1e18b6b444c846e4693b84fe64f926f7 to your computer and use it in GitHub Desktop.
JXA script to categorise transactions in Numbers.app
// JXA script to categorise selected rows of bank transactions in Numbers.app. Paste
// this script into ScriptEditor.app and run it there.
//
// See these for more info on using JXA:
//
// https://developer.apple.com/library/archive/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/Introduction.html
// https://www.macstories.net/tutorials/getting-started-with-javascript-for-automation-on-yosemite/
//
//
//
// Select a range of cells in Numbers and then run this script. It will examine column
// 2 of the range and if it matches any of the categories listed, will set the value
// of column 4 in the range to the appropriate category.
//
// Column 2 of the table is the Reference/Description
// Column 4 of the table is the Category
//
// Check log history window for console output
// debugger;
var numbers = Application('Numbers');
var categories = {
"eating_out": [/benugo/i, /starbucks/i, /pret a manger/i],
"bills": [/thames water/i],
};
for (const doc of numbers.documents()) {
for (const table of doc.activeSheet.tables()) {
const docTable = doc.name() + " | " + table.name();
const selection = table.selectionRange();
if (selection != undefined) {
console.log(docTable + " | " + selection.name());
for (const row of selection.rows()) {
for (category of Object.keys(categories)) {
const matchCategoryFun = function(pattern) {
return row.cells[2].value().match(pattern);
}
categoryMatchers = categories[category]
if (categoryMatchers.some(matchCategoryFun)) {
row.cells[4].value = category;
console.log(docTable + " | " + row.name() + " = " + category);
}
}
}
}
}
}
// currentApp = Application.currentApplication();
// currentApp.includeStandardAdditions = true;
// action = currentApp.displayAlert('outwhat', {
// message: table.name()
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment