(This post is about automating Excel on Mac OS. If you’re not a Mac user this probably won’t interest you.)
My Process
From the creation of a CSV file onwards, automation is key:
Creating a CSV file is one of two things:
- A program on z/OS.
- A piece of javascript code I have that turns HTML tables into CSV.
So that’s not a problem. Where the problem starts is automating Excel, which is what this post is about.
Here is the sequence I generally go through with Excel:
- Excel ingests the CSV file.
- Then it moves onto the business of creating a chart.
- Then resizing the chart.
- Then changing some of the attributes of the chart.
- Finally exporting the chart in the graphical format that I can use in a presentation.
I orchestrate a lot of things with the Keyboard Maestro automation platform. It’s well worth the money, even if it is your own money. I might kick off a Keyboard Maestro macro in one of several ways:
- By a hot key combination
- Using a Stream Deck
- Using the Metagrid app on a USB-attached iPad.
(As an aside on the “hot key” combination, I’ve repurposed the Caps Lock key to pop up a menu of automations – using Karabiner Elements and a Keyboard Maestro conflict palette.)
ApplesScript And Excel
AppleScript it the prevalent automation language on Mac OS. I have to say the AppleScript support in Excel is very obtuse. So most of the value in this post is a few snippets of AppleScript that people trying to use the data model might find useful.
One tip: If you look at the VBA data model for Excel the AppleScript support is very similar to it, language differences apart.
So here are some snippets of code you might find useful.
Changing A Chart’s Type
To change the currently selected (active) chart’s type to a line chart you would code
tell application "Microsoft Excel"
set newChart to active chart
tell newChart
set chart type to line chart
end tell
end tell
Note the name “line chart” is not a string. It is literally line chart
. I think this is confusing. Other chart types I’ve automated have been
xyscatter
column stacked
column stacked 100
This last is where the y axis stops at 100%.
Editing The Title Of A Chart
The following example does a number of things:
- Prompt you for a title for the current (active) chart.
- Sets the chart title to the text you returned.
- Sets the font size of the title to 24 points.
Here is the code:
tell application "Microsoft Excel"
set newChart to active chart
tell chart title of newChart
set theResponse to display dialog "Edit Chart Title" default answer chart title text as string buttons {"Cancel", "Continue"} default button "Continue"
set chart title text to text returned of theResponse
tell its font object
set font size to 24
end tell
end tell
end tell
Setting The Dimensions Of A Chart
I always want the dimensions of a chart to be the same – and suitable for including in a PowerPoint presentation. I have two scripts for setting the dimensions of the active chart:
- Single Width
- Double Width
Only the single width one is right for putting in a presentation:
tell application "Microsoft Excel"
set ch to chart area object of active chart
set height of ch to 567
set width of ch to 850.5
end tell
The double width one came in very handy recently: It was a great way to zoom in on a time line:
tell application "Microsoft Excel"
set ch to chart area object of active chart
set height of ch to 567
set width of ch to 1701
end tell
I’ve set these two up on a pair of buttons in Metagrid – so I can readily swap between the two sizes.
In Conclusion
You might wonder why I’ve created a blog post that is essentially code snippets. Here’s why: It took a lot of fiddling, experimentation and web searching to come up with these snippets of code. That they were hard to come up with or find says something.
This post will be findable from Duck Duck Go etc. I hope it saves people some time and frustration.
If you’re not into automation I might be beginning to warm you to it.
I have a lot of Keyboard Maestro macros for Excel that I have yet to convert to pure AppleScript – and these are mainly fiddling with menus. (Keyboard Maestro is very good at that but it is slower and less reliable to automate that way.) As I convert some more I might well post additional code snippets.
2 thoughts on “Automating Microsoft Excel”