AppleScript read a Numbers file for multiple instances of the same fomatted value and the cell above fromatted value

I want to write an AppleScript that reads a Numbers file and looks for 0.00% for every instance of it. I want to print 0.00% and the formatted value of the cell above each instance of 0.00%. I have the following working code that can find the instance of 0.00% once but I can't figure out how to make it continue looking for multiple instances of 0.00% and print them along with the cell above the cell that has 0.00%


tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1

set theValues to the formatted value of every cell

set theKey to "0.00%"

set found to "key unavailable"

set nbColumns to count columns

set indx to 0

repeat with r from 1 to count rows

repeat with c from 1 to nbColumns

set indx to indx + 1

if item indx of theValues = theKey then

set found to (the formatted value of cell r of column c)

end if

end repeat

end repeat

end tell

found

MacBook Air 13″, macOS 14.5

Posted on Jul 4, 2024 3:57 PM

Reply
9 replies

Jul 6, 2024 3:11 AM in response to ike_like

Not sure what you mean by "print" (so I just logged it in the Replies panel) or what your Numbers table looks like, but this should give you some ideas:




The script:


tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1
	set theKey to "0.00%"
	set theValues to the formatted value of every cell of every column --> list of lists
	set numRows to length of item 1 of theValues --> number of rows is length of a list
	repeat with aColumn in items of theValues -- each item is a column
		tell aColumn
			repeat with itemNum from 1 to numRows
				if (item itemNum as text) is theKey then
					set valAbove to item (itemNum - 1) as text
					tell me to log valAbove & return & "this is 0.00%"
				end if
			end repeat
		end tell
	end repeat
end tell


SG

Jul 6, 2024 3:01 PM in response to SGIII

FINALLY! An AppleScript that doesn't cough up an indecipherable error! That's great after bouncing off days worth of errors being flagged by every other example. Even another of your older scripts failed me. :(

You'd think there'd be plenty of scripts available to interact with Numbers, but it seems to be a fat mystery.


So, I've reversed the column and row extraction order since all my spreadsheets are left to right, so in my sheet, rows grow with additional data, but columns are fixed. Is that not typical behavior? I don't know.


But do you have a trick to extract both values and formulas row by row? That would be a dream come true :^)


tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1

	set theKey to "$555,000"

	set theValues to the formatted value of every cell of every row --> list of lists

	set numColumns to length of item 1 of theValues --> number of rows is length of a list

	repeat with aColumn in items of theValues -- each item is a column

		tell aColumn

			repeat with itemNum from 1 to numColumns

				if (item itemNum as text) is theKey then

					set valAbove to item (itemNum - 1) as text

					tell me to log valAbove & return & "this is 0.00%"

				end if

			end repeat

		end tell

	end repeat

end tell

Jul 7, 2024 4:10 AM in response to Jakib

Jakib wrote:

But do you have a trick to extract both values and formulas row by row?


For example? It's hard to visualize the use case for that. You can post a screenshot with shift-command-4, drag to select area with cross-hairs, release, start new post here and use the mountains-and-moon 'Image insertion' icon beneath the compose window to insert the screenshot file from the Desktop.


Some useful examples and explanations of how to use AppleScript with Number can be found here.


SG

Jul 7, 2024 11:58 PM in response to Jakib

Good point about the rows. I came to the same realization on my own. Makes more sense. Then, given the application, I don't even care about the problem for automation because it is so convoluted how to write a solution that scans left to right and up and down with a loop. I mean, I like the challenge sometimes but I have taken a break from checking other's code attempts. More just giving good comments upvotes, writing poetry, doing some more research of other topics, working. I don't have the energy to deal with these hackers. I thought AppleScript would have some built in automation for data analysis but evidently they want me to pay to write that for them too. If I pay to write it is going to be about a romantic interest besides a Macintosh that doesn't do much other than act as a quantum target for my personal information and banking info. Just remember, no matter what you spend on a computer, no computational framework can give anything resembling a promissary that it won't cost money to run. Gas make cash. Digital makes waste. I'm a carbon offset genius. Not an Apple genius. They like to think California can cook eggs real well evidently. East coast is clear across the country and I can cook a 3 egg omelette better than they can stripe mine to finance their mistakes. The omelette has value. A computer is negative value. Negative work. Negative offsets. Way negative.

Jul 13, 2024 5:18 PM in response to Camelot

I am not an AI bot. And I don't use ChatGPT for describing my problems on the forums. Perhaps I should use such things by now. The more I use small robotic algorithms the more I understand about how to code. It's just every time I get an answer or a step forward on the forums, it has another whole independent study of the persons solution.

Jul 13, 2024 5:38 PM in response to ike_like

I solved the problem myself. The automation of this type of problem set is too labor intensive. It is easier to edit the structure of the Numbers file to one column of data and copy and paste the data of interest to a social media site for free data for the community that isn't endless work on typo errors on a forum. Readable data that can easily show the community something that they don't have to master in order to be free from the typo game.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript read a Numbers file for multiple instances of the same fomatted value and the cell above fromatted value

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.