ChatGPT flails at an Apple Script for Excel

Thought I’d share this. I was trying to write a quick script to set the zoom of every tab of a MS Excel workbook to 125%. I was failing. Couldn’t figure out whether the zoom was a property of a worksheet or a window. Anyway, the best I could do was set only the zoom of the active sheet. So I thought I’d see what ChatGPT could do. Ugh. It flailed around trying to script Excel directly and then tried scripting keystrokes through System Events. I gave up after a half-dozen tries. The thread is below if you’re interested in the back and forth.

Per excel’s dictionary, zoom is a property of the window — but it is set independently for each sheet. So, cycle through each sheet and set the window’s zoom.

tell application "Microsoft Excel"
	activate
	set shList to worksheets of workbook 1
	repeat with x in shList -- cycle through each worksheet
		set y to contents of x
		--> worksheet "Sheet3" of workbook 1 of application "Microsoft Excel"
		activate object y -- bring worksheet to fore
		
		delay 0.4
		set zoom of window 1 to 125 -- set worksheet's zoom
		delay 0.4
		
	end repeat
end tell

I added a delay before and after the zoom so you can see the changes as they occur but you should be able to remove them without it affecting the result.

Thanks. That’s genius. I was trying things like looping through each sheet to try and set window properties or “tell workbook 1, set zoom of every window.” It ever only seemed to address the active sheet. Activating each sheet and then setting the zoom is a good idea.

This all feels like GUI-based workarounds though. It’s weird that you can’t tell Excel to set every window of every sheet in the specified document to x%. MS support for AS has never been great. I haven’t spent a lot of time with it since maybe 2011. It wasn’t easy then but I could find ways to do things. Has it gotten worse as Apple has drawn back support for Apple Script?

If there are any settings that you want to apply more-or-less globally… edit the template. For me, when I used excel regularly, after a while I noticed that 90% of my workbooks had only one sheet and always had the same layout. So I deleted the extra sheets, made column A extra-wide and added some formatting for text. The rest of the sheet was centred. After creating a template and making it the default, I didn’t have to adjust any settings again. If I wanted a different layout then I used the original template. It saved a lot of time and effort.

Everybody has a different opinion but I think excel’s applescript support is excellent, albeit complicated. While it’s been stagnant for a while, I can’t really fault MS. There are a lot of apps that I wish could get 10% of the effort that they put into excel and word.

3 Likes