Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript to evaluate data, copy result to new table

Hi there and thank you for any help you can offer,


I am looking to utilize AppleScript to insert a range of values (1-n) from a table into a calculator I have set up within the Numbers.app spreadsheet, copying the resulting numerical outputs to the table.


Here is a visual example of what I am looking to do:

User uploaded file


I would like to take the value highlighted in red, copy it to the golden colored cell, then copy the resulting blue and purple values to the table prior to moving on the the subsequent value if one exists.


I understand I will need to identify the table name and range for this to work, though I am still very novice in the fine art of AppleScript.


Thanks for taking a look and I hope you are having a great day,


Jordan

MacBook Pro with Retina display

Posted on Mar 23, 2015 5:15 AM

Reply
8 replies

Mar 23, 2015 10:01 AM in response to t quinn

Hi TQ,


Need to use AppleScript because I base a myriad of calculations off of the input. Unfortunately it is not a situation that can be 'filled' out into a table.


In this scenario I am taking our total sales and calculating resource requirements to support the sale. This requires I process the sales numbers through a host of ratios which define our variable and semi-fixed costs.


This leads to a situation where the number of sales entered provides one output - I would love to have the ability to create a graph out of this one location, but I understand that I need to create a data table in order to create my graph from. This is where the AppleScript comes into play, as I would be able to have the script run through a defined set of sales numbers and copy the resulting figures into a table from which I can graph from.

Mar 23, 2015 3:03 PM in response to jordoapp

It sounds as if you want to plot the results of different scenarios.


How complicated is the calculation chain from sales to semi-fixed costs, etc? If not too complicated it might be to your advantage to arrange the values in simple table form rather than as you have in your REVENUES table, e.g. something like:


Sales Fixed Semi-Fixed Total



Have you had a look for ideas at the Break-Even Analysis template? (File > New > Business section of template chooser)


SG

Mar 25, 2015 12:47 PM in response to SGIII

SG,


Thanks for the reply. The calculation chain is quite complex, with a couple calculations splitting the input into different paths based on defined market ratios which are in turn creating "choke" points; this is what is not allowing me to follow your guidance above.


My initial reaction was to seek a Numbers feature to plot this relationship by my identifying cell locations and stating a range, creating a program supported function; I am unaware how to do this if the functionality does exist however.


So this brought me to using AppleScript, an applicable solution. Here's the insight that I can provide thus far for the script:

  • Create a 2D array from existing "Data" table
  • Insert first item from array into cell "C2" of table “Calculations”
  • Copy resulting sales calculation to a defined data store ("calculatedSales")
  • Copy resulting semi-fixed costs calculation to a defined data store ("calculatedSemiFixedCosts")
  • Copy resulting variable costs calculation to a defined data store ("calculatedVariableCosts")
  • Repeat for the remaining items in the 2D array
  • Write 'calculatedSales' to B column (+1 due to header) of "Data" table
  • Write 'calculatedSemiFixedCosts' to C column of "Data" table
  • Write 'calculatedVariableCosts' to D column of "Data" table


Here's a visualization of the above:

User uploaded file


With this completed, a data set could be created on demand and plotted by Numbers.


Here's what I have thus far:

tell application "Numbers"


activate


-- for this script, grab data set from "Data" table (1 Column with 25 rows)

set scriptData to my scriptDataSets(1, 25)


-- for this script, create a store for calculated values

set calculatedSales to my scriptDataSets(1, 25)

set calculatedSemiFixedCosts to my scriptDataSets(1, 25)

set calculatedVariableCosts to my scriptDataSets(1, 25)

end tell


tell application "Numbers"


activate


-- for this script, grab data set from "Data" table (1 Column with 25 rows)

set scriptData to my scriptDataSets(1, 25)


-- for this script, create a store for calculated values

set calculatedSales to my scriptDataSets(1, 25)

set calculatedSemiFixedCosts to my scriptDataSets(1, 25)

set calculatedVariableCosts to my scriptDataSets(1, 25)


-- todo: identify the correct table here

set the columnCellCount to count of cells of column 2

repeat with i from 1 to count of the scriptData


-- get a data set from the data set list

set salesColumnData to itemi of the calculatedSales

tell column (columnIndex + i)


-- iterate the data set, populating row cells from to to bottom

repeat with q from 1 to the count of salesColumnData

tell cell (columnIndex + q)

set value to item q of salesColumnData

end tell

end repeat

set semiFixedColumnData to itemi of the calculatedSemiFixedCosts

tell column (columnIndex + i)


-- iterate the data set, populating row cells from to to bottom

repeat with q from 1 to the count of semiFixedColumnData

tell cell (columnIndex + q)

set value to itemq of semiFixedColumnData

end tell

end repeat

set variableColumnData to itemi of the calculatedVariableCosts

tell column (columnIndex + i)


-- iterate the data set, populating row cells from to to bottom

repeat with q from 1 to the count of variableColumnData

tell cell (columnIndex + q)

set value to item q of variableColumnData

end tell

end repeat

end tell

end tell

end tell

end repeat

end tell



My apologies for the formatting above. My largest issues relate to identifying the correct locations of tables to be read from and write to, then writing the values correctly. This process could have been done through Automator or via keystrokes, however SGIII's post on a stack overflow ticket inspired a fundamental approach to this. Any help would be welcomed and thanks from everyone who forgot to say thanks.

Jordan

Mar 27, 2015 12:30 PM in response to jordoapp

Hi Jordan,


It seems that the SGIII that posted in stackoverflow may be the same SGIII as the one in this forum.😉 Yes, in Numbers (just as in Excel) you can easily use AppleScript to place values from a range of cells into a "list" or "list of lists" (1D or 2D array).


For example, to list the values in column A of a table named 'DATA' in sheet 'Sheet 1' in the front document, you could do this:


property src : {d:1, s:"Sheet 1", t:"DATA", col:"A"}


tell application "Numbers"

set vv to document (src's d)'s sheet (src's s)'s table (src's t)'s column (src's col)'s cells's value

end tell



I'm having trouble following what you are trying to do here, though.


Is the idea to feed values from column A of 'DATA' into a calculation, and place the results of the calculation into columns B, C & D?


What does the calculation look like?


SG

Mar 27, 2015 12:53 PM in response to jordoapp

Hello


You may try something like the following script.



_main() on _main() script o property |SHEET| : 1 -- name or index of target sheet property |DATA| : "DATA" -- name or index of data table property |CALC| : "CALCULATIONS" -- name or index of calculator table property |RANGE| : {2, 26} -- data row range of data table property |IN_MAPS| : {{1, "C2"}} property |OUT_MAPS| : {{"D2", 2}, {"C5", 3}, {"C6", 4}} (* each map {x, y} in |IN_MAPS| denotes that table |DATA|'s column x maps to table |CALC|'s cell y each map {x, y} in |OUT_MAPS| denotes that table |CALC|'s cell x maps to table |DATA|'s column y *) property pp : {} set {i1, i2} to |RANGE| tell application "Numbers" activate tell document 1's sheet |SHEET| -- get input data array from table |DATA| according to |IN_MAPS| tell table |DATA| tell rows i1 thru i2 repeat with m in my |IN_MAPS| set {x, y} to m set my pp's end to cell x's value end repeat end tell end tell repeat with i from i1 to i2 -- for each row in |RANGE| -- set input data in table |CALC| according to |IN_MAPS| repeat with k from 1 to count |IN_MAPS| set {x, y} to my |IN_MAPS|'s item k tell table |CALC| set cell y's value to my pp's item k's item (i - i1 + 1) end tell end repeat -- get output data from table |CALC| and set value in table |DATA| according to |OUT_MAPS| repeat with k from 1 to count |OUT_MAPS| set {x, y} to my |OUT_MAPS|'s item k tell table |CALC| set v to cell x's value end tell tell table |DATA| set row i's cell y's value to v end tell end repeat end repeat end tell end tell end script tell o to run end _main




Before running script:


User uploaded file



After running script:


User uploaded file



CALCULATIONS (excerpt) C2 C3 C4 50 C5 =VLOOKUP(C2,LUT::A:C,3) C6 =C4+C5 D2 =VLOOKUP(C2,LUT::A:B,2) D3 D4 D5 D6



In this example, CALCULATIONS table uses dummy VLOOKUP() formulae to populate C5 and D2 from LUT table for any given value in C2.


Tested with Numbers v2 under OS X 10.6.8.



Hope this may help,

H

Mar 27, 2015 4:25 PM in response to Hiroto

Non-essential update to CALCULATIONS table. I just corrected my misunderstanding about variable costs. It does not affect the script.



Before running the script:


User uploaded file



After running the script:


User uploaded file



CALCULATIONS (excerpt) C2 C3 C4 50 C5 =VLOOKUP(C$2,LUT::A:D,3) C6 =VLOOKUP(C$2,LUT::A:D,4) C7 C8 D2 =VLOOKUP(C$2,LUT::A:D,2) D3 D4 D5 D6 D7 =SUM(C4:C6) D8 =D2-D7




Cheers,

H


EDIT: replaced screenshots with correct ones.

AppleScript to evaluate data, copy result to new table

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