AppleScript for Numbers would write to cells but not read

So basically I trying write an automated script to run some functions for me


here is what I need it to do


To iterate through all rows in the first table of the active sheet and check if the text in column F contains “win” or “loss”. If it does, it will replace the corresponding value in column G with 1 or -1.1, respectively. If it doesn’t contain either, it will replace the value in column G with 0. It will then iterate through each unique name in column A and sum up the corresponding values in column G. The total value for each individual will be placed in column H. Finally, it will multiply the total for each individual in column H by 100 and place it in column I.


Can anyone show me why I getting errors, I have included screenshot of the error as well as the model I am trying to replicate using apple scripts. I have no results.


Thanks for attention to this...


So far I wrote:


tell application "Numbers"


tell the active sheet of the front document


set lastRow to the count of rows of table 1


repeat with i from 2 to lastRow


set cellValue to value of cell ("F" & i)


if cellValue contains "Win" then


set value of cell ("G" & i) to 1


else if cellValue contains "Loss" then


set value of cell ("G" & i) to -1.1


else


set value of cell ("G" & i) to 0


end if


end repeat



set nameList to {}


repeat with i from 2 to lastRow


set nameValue to value of cell ("A" & i)


if nameValue is not in nameList then


set end of nameList to nameValue


end if


end repeat



repeat with i from 1 to count of nameList


set totalValue to 0


repeat with j from 2 to lastRow


set nameValue to value of cell ("A" & j)


if nameValue is equal to item i of nameList then


set totalValue to totalValue + (value of cell ("G" & j))


end if


end repeat



set value of cell ("H" & (i + 1)) to totalValue



set value of cell ("I" & (i + 1)) to totalValue * 100



end repeat



end tell


end tell


Finally, when I run the above script, I get an error


MacBook Air, macOS 14.0

Posted on Oct 9, 2023 2:31 PM

Reply
Question marked as Top-ranking reply

Posted on Oct 9, 2023 4:11 PM

I think the beginning simply needs to be written like this. You can't get the cell of a sheet, it belongs to a table.


tell application "Numbers"

tell table 1 of active sheet of the front document

set lastRow to the count of rows

25 replies

Oct 26, 2023 10:12 AM in response to SGIII

There was supposed to be additional text with the previous post, I don't know where it went. Here it is.

PART 1



use scripting additions -- essential to use (current date) with framework "Foundation"
use framework "Foundation"

-- NSTimeZone | Apple Developer Documentation
-- https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/AppendixA-AppleScriptObjCQuickTranslationGuide.html

global systemZone

tell class "NSTimeZone" of current application to resetSystemTimeZone()

set aTZ to current application's NSTimeZone
set localZone to aTZ's localTimeZone()
set systemZone to aTZ's systemTimeZone()
set defaultZone to aTZ's defaultTimeZone()

-- this is done in two steps to preserve the string for use in other langauges by avoiding conversion during compilation
set dateWinterString to "1/1/2023"
set dateSummerString to "1/7/2023"
set dateWinter to date dateWinterString
set dateSummer to date dateSummerString

log ["Today", (current date)]
log [dateWinter, correctedDate(dateWinter, 1)]
log [dateSummer, correctedDate(dateSummer, 1)]
-- try that part also after reading dates from Numbers
-- tell application "Numbers" to set dateWinter to value of cell 1 of table 1 of sheet 1 of document 1

set defaultZone to current application's NSTimeZone's alloc's initWithName:"Europe/Helsinki"
-- alternative version
-- set defaultZone to current application's NSTimeZone's timeZoneWithName:"Europe/Helsinki"



To be continued...

Oct 26, 2023 10:15 AM in response to SGIII

PART 2


log ["name", localZone's |name|() as string, systemZone's |name|() as string, defaultZone's |name|() as string]
log ["description", localZone's |description|() as string, systemZone's |description|() as string, defaultZone's |description|() as string]

log ["abbreviation", localZone's abbreviation as string]
log ["abbreviation for", dateWinter, (localZone's abbreviationForDate:dateWinter) as string]
log ["abbreviation for", dateSummer, (localZone's abbreviationForDate:dateSummer) as string]

log ["localized name of zone", (localZone's localizedName:(current application's NSTimeZoneNameStyleStandard) locale:(current application's NSLocale's currentLocale())) as string]

log ["seconds from GMT today", localZone's secondsFromGMT(), systemZone's secondsFromGMT()]
log ["seconds from GMT for date", short date string of dateWinter, {localZone's secondsFromGMTForDate:dateWinter, systemZone's secondsFromGMTForDate:dateWinter}]
log ["seconds from GMT for date", short date string of dateSummer, {localZone's secondsFromGMTForDate:dateSummer, systemZone's secondsFromGMTForDate:dateSummer}]

log ["daylightSavingTimeOffset today", localZone's daylightSavingTimeOffset(), systemZone's daylightSavingTimeOffset()]
log ["daylightSavingTimeOffset for date", dateWinter, (localZone's daylightSavingTimeOffsetForDate:dateWinter), (systemZone's daylightSavingTimeOffsetForDate:dateWinter)]
log ["daylightSavingTimeOffset for date", dateSummer, (localZone's daylightSavingTimeOffsetForDate:dateSummer), (systemZone's daylightSavingTimeOffsetForDate:dateSummer)]

set nDSTT to localZone's nextDaylightSavingTimeTransition() as date
log ["next time transition", nDSTT]
log ["and the next", (localZone's nextDaylightSavingTimeTransitionAfterDate:(nDSTT + 1 * days)) as date]

log ["isDaylightSavingTime today", localZone's isDaylightSavingTime(), systemZone's isDaylightSavingTime()]
log ["isDaylightSavingTime for date", dateSummer, localZone's isDaylightSavingTimeForDate:dateSummer]

to be continued...

Oct 26, 2023 10:16 AM in response to SGIII

PART 3


log "---"

set tzNames to aTZ's knownTimeZoneNames()
log ["knownTimeZoneNames", count of tzNames, tzNames's item 1 as record]
repeat with i from 1 to count of tzNames
	log [i, tzNames's item i as string]
end repeat
log "---"

set tzDict to aTZ's abbreviationDictionary()
log ["knownAbbreviatons", count of tzDict, tzDict's item 2 as record]
set allKeys to tzDict's allKeys()
repeat with theKey in allKeys
	log [theKey as text, (tzDict's valueForKey:theKey) as text]
end repeat


-- To read dates from Numbers and cancel the effect of daylight time adjustment, use this handler
to correctedDate(_date, _fromNumbers)
	-- _fromNumbers must be 1 when reading from Numbers, -1 when writing to Numbers
	if class of _date is date then
		return _date + _fromNumbers * ((systemZone's secondsFromGMT()) - (systemZone's secondsFromGMTForDate:_date))
	else
		return _date
	end if
end correctedDate


** Complete **


For some reason it was not possible to post all three parts together.

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 for Numbers would write to cells but not read

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