Sunday, December 4, 2022

#1 2022-10-24 12:29:04 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 1091

Change value specified by any type to new type in Numbers

Simple example in Numbers to change a value specified by any type to new type

Could easily be adapted to some more useful or give ideas to build a Library.

Applescript:

tell application "Numbers"
   make new document with properties {document template:template "Simple Budget"}
   activate
end tell

(**
* [changeValueByName(string, string, integer, anything, anything)]
*)

its changeValueByName("Budget", "Expenses", 1, "Shopping", "Food")
on changeValueByName(theSheet, theTable, theColumn, oldValue, newValue)
   tell application "Numbers"
       set theDoc to front document
       tell theDoc
           tell sheet theSheet
               tell table theTable
                   repeat with i from 1 to (count row)
                       set currentValue to value of the cell i of column theColumn
                       if currentValue contains oldValue then
                           set value of cell i of column theColumn to newValue
                       end if
                   end repeat
               end tell
           end tell
       end tell
   end tell
end changeValueByName


Node-RED makes it easy to automate IoT

Offline

 

#2 2022-10-25 05:03:41 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1091

Re: Change value specified by any type to new type in Numbers

This one will change the value based on category in a table in Numbers.

The category is left cell and the value is right cell

Applescript:

(**
* [changeValueByCategory(string, string, integer, anything, anything)]
*)

its changeValueByCategory("Budget", "Expenses", 1, "Shopping", 1000)
on changeValueByCategory(theSheet, theTable, theColumn, theCategory, newValue)
   tell application "Numbers"
       set theDoc to front document
       tell theDoc
           tell sheet theSheet
               tell table theTable
                   repeat with i from 1 to (count row)
                       set category to value of the cell i of column theColumn
                       if category contains theCategory then
                           set value of cell i of column (theColumn + 1) to newValue
                       end if
                   end repeat
               end tell
           end tell
       end tell
   end tell
end changeValueByCategory

Last edited by Fredrik71 (2022-10-25 05:06:49 am)


Node-RED makes it easy to automate IoT

Offline

 

#3 2022-10-25 05:54:17 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1091

Re: Change value specified by any type to new type in Numbers

This one do not use repeat loop but instead use the method: indexOfObject (ASObjC)

Applescript:

(**
* [changeValueByCategory2(string, string, integer, anything, anything)]
*)

its changeValueByCategory2("Budget", "Expenses", 1, "Shopping", 1000)
on changeValueByCategory2(theSheet, theTable, theColumn, theCategory, newValue)
   tell application "Numbers"
       set theDoc to front document
       tell theDoc
           tell sheet theSheet
               tell table theTable
                   set theNames to its name of every row
                   if theNames contains {theCategory} then
                       set theIndex to ((current application's NSArray's arrayWithArray:theNames)'s indexOfObject:theCategory)
                       set value of cell (theIndex + 1) of column (theColumn + 1) to newValue
                   end if
                   
               end tell
           end tell
       end tell
   end tell
end changeValueByCategory2

Last edited by Fredrik71 (2022-10-25 05:59:26 am)


Node-RED makes it easy to automate IoT

Offline

 

#4 2022-10-25 06:35:41 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1091

Re: Change value specified by any type to new type in Numbers

And if we have list of categorys with same amount of new values we could add a repeat loop.

Applescript:

(**
* [changeValuesByCategorys(string, string, integer, list of anything, list of anything)]
*)

its changeValuesByCategorys("Budget", "Expenses", 1, {"Shopping", "Transport"}, {1000, 1300})
on changeValuesByCategorys(theSheet, theTable, theColumn, theCategorys, newValues)
   tell application "Numbers"
       set theDoc to front document
       tell theDoc
           tell sheet theSheet
               tell table theTable
                   set theNames to its name of every row
                   if theNames contains theCategorys then
                       repeat with i from 1 to (count theCategorys)
                           set theIndex to ((current application's NSArray's arrayWithArray:theNames)'s indexOfObject:(item i of theCategorys))
                           set value of cell (theIndex + 1) of column (theColumn + 1) to (item i of newValues)
                       end repeat
                   end if
                   
               end tell
           end tell
       end tell
   end tell
end changeValuesByCategorys

Last edited by Fredrik71 (2022-10-25 06:37:56 am)


Node-RED makes it easy to automate IoT

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)