AppleScript Numbers: Process data from Quicken
Environment:
- Mojave
- Numbers 6.1
- Automator 2.9
- Both Numbers and Automator are set via System Preferences > Security & Privacy > Accessibility
Rationale For Post:
I'd been working on this for a number of days, and got a lot of valuable help (from SG and others), and I figured providing a somewhat detailed summation of the code was a way of paying-back / paving the way for others.
General Description of Project:
Periodically I get dumps of my mother's financial data from her Quicken, and I wanted to be able to look at the information in various ways to get a clearer picture of what it all means. I started with what I considered to be a relatively simple spreadsheet. One sheet had the detailed information broken down by date, amount, and categorization key:
A second sheet then tallied the amounts associated with each key on a month-by-month basis:
I proceeded to populate each cell in the table with a formula like:
=SUMIFS($Amount,$Date,">=01/01/2016",$Date,"<02/01/2016",$Key,"=IN:MISC*")
Where '$Amount', '$Date', and '$Key' are Numbers' variables refering the columns of the "Details" sheet
This was all pretty nice and I was reasonably proud of myself (:-)) but I then realized I had to make a distinct formula for each 'key' row, and then I had to also make a distinct version of the formula for each month column. With approximately 40 'key's and 12 months in the year we're talking about over 480 manual edits to the formulas.
Well, I realized then that I could put all the key-row formalas for a given month into a text-file and use query-replace to change the month information and then copy/paste it into the Numbers table. Not terrible, but still a bit time-consuming, and it was only going to get worse as I added more and more years of data to the "Details" sheet.
I then thought that might be able to use column-row references like $AK4 or AK$4 and then just copy-paste the formulas around without going through the query-replace step. To this end, I modified the table to include month numbers and key strings to reference:
Unfortunately, I then realized that I couldn't use such a reference within a string - i.e. ">=$E2" and "<$F2"
Enter AppleScript:
So I started thinking about AppleScript and ran into a number of questions about coding and started posting questions in the Apple discussion forums (thanks again to SG and others for responding):
In the course of doing so and plodding through ideas while waiting for answers, the design model morphed a bit:
- How to interact with specific sheets in numbers via AppleScript?
- AppleScript Numbers: Get value from cell specified with variables
- AppleScript Numbers: Get formula from cell (as modifiable text)
- AppleScript Numbers: modifying individual cells within a selected range
- AppleScript Numbers: Formatting cells for currency - with options?
- AppleScript Numbers: design options for performance
(thanks again to SG and others for responding)
So, ultimately I decided to construct formulas using the information I put in the row and column headings, setting the cell to the formula, and then replacing it with the result of the formula (to significantly reduce the number of formulas within the table). I'm still playing around with the code a bit, but this works and I hope provides a reasoable example for others who are trying to do the same or similar type things.
(Due to size limitations on post, the code will be put into one or more Reply's)
MacBook Air 13", macOS 10.14