AppleScript Numbers: Get value from cell specified with variables
Environment:
- Mojave MacOS 10.14.5
- Numbers 6.1 (6369)
- Automator 2.9 (444.42)
Issue:
Using an AppleScript run as a Service from within Numbers I am trying to access the value of a cell immediately to the left of the current cell with a custom function with parameters for the current cell's row and column information. (I intend to get the formula from the cell, modify it, and then place the modified version into the current cell - but first things first).
Whenever I try to get the value of the desired cell with the parameterized variables - or even a hard-coded cell reference (e.g.: "B3") I get this error message:
The action "Run AppleScript" encountered an error: "The operation couldn't be completed. (com.apple.Automator error -212.)"
A slightly stripped down version of the code:
on run {input, parameters}
tell application "Numbers"
tell table 1 of active sheet of front document
set currentcell to the first cell of the selection range
set cCol to name of column of currentcell -- [*A*]
set aa to getCellValueFromLeft(3, id of cCol) of me
-- display notification "AA = '" & aa & "'" -- [*B*]
set value of cell "E10" to aa -- [*C*]
end tell
end tell
return input
end run
on getCellValueFromLeft(r, c)
try
set ch to character id (c - 1)
set cellName to ch & r
display notification "New Cell Name: '" & cellName & "'" -- [*D*]
set cv to value of cell "B3"
--or-- set cv to value of cell r of column ch
--or-- set cv to value of cell cellName
display notification "'" & c & " - 1' => '" & ch & "'" -- [*E*]
return ch -- [*F*]
end try
end getCellValueFromLeft
Notes:
- [*A*] I could use '.. to address of ..' instead and then could just pass cCol as-is rather than using 'id of ...', I've been doing lots of variations of the code and haven't settled on that decision yet.
- [*B*] If this 'display' is not commented out I'll get an error about 'aa' not being set if the getCellValueFromLeft() function doesn't return a value, so it's commented out for now.
- [*C*] Temporary to verify I'm retrieving what I want.
- [*D*] Works just fine. If I start from "C4" it says "New Cell Name: 'B4'"
- [*E*] Works just fine IFF I comment out the setting of 'cv', thus this posting
- [*F*] Eventually this will be [a modified version of] 'cv'
If I comment out the setting of 'cv' the code "works" but doesn't do what I ultimately want.
All my attempts to get the value of a cell using variables (and hard-coding a column-row specification) seem to fail. I cannot figure out what I'm doing wrong. Hoping someone here can enlighten me.
MacBook Air 13", macOS 10.14