Read Excel cell contents using AppleScript?

TL;DR In Shortcuts or AppleScript, how can I read the contents of the A2 cell in an excel file?

Hello Automators,

I’m hoping one of you clever people will have run into this before. Here’s my issue:

My work often involves ordering parts, and I have two systems that won’t talk to each other so I’m making a shortcut to save me some hand-editing of excel files. When I export a .xlsx file from our inventory system, it’s got a bunch of junk data in it and all I need is two of the columns, so I’ve made a Shortcut to use AppleScript to delete all columns except C and E, and also delete row 1 to get rid of headers (credit to Dr. Drang for some excellent examples posted on the forum!). So far so good.

The issue comes because I’m expanding the shortcut to deal with more suppliers, and they want different formats. I could just ask for the user to tell me which supplier this is for, but I’d like to detect the supplier and trim accordingly without bothering the user.

The name of the supplier is in the A2 cell. Is there a way to read this information using AppleScript? Once I have it I’d like to either A) pull it out into shortcuts and use an if statement to run different AppleScript actions to transform the file, or B) use logic in the AppleScript to do the same thing.

For some context, here is my hastily modified AppleScript that trims the file, mostly copy-pasted from this post by Dr. Drang:

on run {input, parameters}
	
	tell application "Microsoft Excel"
		activate
		open input -- This is the filepath 
		tell active sheet
			tell used range
				set lastCol to count of columns
			end tell
			repeat (lastCol - 7) times
				delete column 8
			end repeat
			delete column 1
			delete column 1
			delete column 2
			delete column 2
			delete column 2
			delete row 1
		end tell -- active sheet
		save
		close active workbook
	end tell -- Excel
	
	return input
end run

Here is my current pass at reading the A2 cell from the excel file, not working for reasons I don’t understand:

on run {input, parameters}
	
	tell application "Microsoft Excel"
		activate
		open input
		tell active sheet
			set output to (cell "A2")
		end tell
		save
		close active workbook
	end tell
	
	return output
end run

Frankly, I’d export the data to CSV and write a Python program - with the csv library - to process it. Probably quicker and more controllable.

Many years since I’ve used Excel on the Mac (daily On Windows for work), but isn’t there a formula and a value property of a cell? I doubt that would have changed in the interim releases since I used it on Mac.

I suspect that you are getting the cell object but would conceivably want one of those properties of the cell instead.

Maybe something like?

set output to value of cell "A2"
2 Likes

@sylumer has it: value of cell "A2". The tricky thing is that you won’t find that answer by looking through the properties of cell in Excel’s AppleScript dictionary. It’s listed as one of the properties of range, which cell inherits.

2 Likes

Thanks so much @drdrang and @sylumer! I should be able to make it work now.

Just adding a little bit here on @Martin_Packer’s alternative approach, but if you go the Python route, you could actually read the excel file with Pandas. I’m not going to say how it’s done because I’m still very new to Python and Pandas, but I’ve seen sample code and know it can definitely work.

But since you may have it (mostly) working with AppleScript, no reason to away.

1 Like

I agree, the python/csv approach would probably be a lot simpler. However since I’m totally unfamiliar with python I think I’m gonna stick to AppleScript for this one.

Also, I think I heard somewhere that python isn’t installed by default on macOS? Not a problem for just me but I’m sharing the script with others in my company so I want to keep it portable.

Thanks for the reply though!

This may not perfectly align with your task, but I’ve traveled down a similar rabbit hole and have had a good experience with Alfred using Dean Jackson’s I Sheet You Not which searches within Excel files and Derick Fay’s Fuzzy List Filter which parses CSV. Both nicely extend the Alfred search metaphor to spreadsheet files. With these workflow templates you can pass the output out to Keyboard Maestro or, I presume, to Shortcuts via AppleScript.