mingsai

Numbers Populate New Table with Row Based Data

Feb 22nd, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. tell application "Numbers"
  2.     activate
  3.     -- for this example script, generate some random data sets
  4.     set generatedData to my generateRandomNumericDataSets(8, 3)
  5.     -- determine row count based on data length
  6.     -- determine column count based on length of first item of data
  7.     -- add 1 to row and column count if table is to include headers
  8.     set useHeaders to true -- change to false to omit headers
  9.     if useHeaders is true then
  10.         set the rowCount to (the count of generatedData) + 1
  11.         set the columnCount to (the count of item 1 of generatedData) + 1
  12.     else
  13.         set the rowCount to the count of generatedData
  14.         set the columnCount to the count of item 1 of generatedData
  15.     end if
  16.     -- create and poplate a table reading the data as row based
  17.     tell document 1
  18.         tell active sheet
  19.             set thisTable to ¬
  20.                 make new table with properties ¬
  21.                     {row count:rowCount, column count:columnCount, name:"Row Data Table"}
  22.             tell thisTable
  23.                 if useHeaders is true then
  24.                     -- set the starting indexes for using headers
  25.                     set rowIndex to 1
  26.                     set columnIndex to 1
  27.                     -- since headers are used, name the column & row headers
  28.                     -- title and style the column header cells
  29.                     set x to 1
  30.                     repeat with i from 2 to the columnCount
  31.                         tell cell 1 of column i
  32.                             set value to "COL " & (x as string)
  33.                             set alignment to center
  34.                             set vertical alignment to center
  35.                         end tell
  36.                         set x to x + 1
  37.                     end repeat
  38.                     -- title and style the row header cells
  39.                     set x to 1
  40.                     repeat with i from 2 to the rowCount
  41.                         tell cell 1 of row i
  42.                             -- set the value and styling of the cell
  43.                             set value to "ROW " & (x as string)
  44.                             set alignment to right
  45.                             set vertical alignment to center
  46.                         end tell
  47.                         set x to x + 1
  48.                     end repeat
  49.                 else
  50.                     -- set the starting indexes for not using headers
  51.                     set rowIndex to 0
  52.                     set columnIndex to 0
  53.                 end if
  54.                 -- populate the table with the data
  55.                 set the rowCellCount to count of cells of row 2
  56.                 repeat with i from 1 to count of the generatedData
  57.                     -- get a data set from the data set list
  58.                     set thisRowData to item i of the generatedData
  59.                     tell row (rowIndex + i)
  60.                         -- iterate the data set, populating row cells from left to right
  61.                         repeat with q from 1 to the count of thisRowData
  62.                             tell cell (columnIndex + q)
  63.                                 set value to item q of thisRowData
  64.                                 set alignment to center
  65.                                 set vertical alignment to center
  66.                             end tell
  67.                         end repeat
  68.                     end tell
  69.                 end repeat
  70.             end tell
  71.         end tell
  72.     end tell
  73. end tell
  74.  
  75.  
  76. on generateRandomNumericDataSets(setCount, setItemCount)
  77.     set randomNumericDataSets to {}
  78.     repeat setCount times
  79.         set thisSet to {}
  80.         repeat setItemCount times
  81.             set the end of thisSet to (random number from -100 to 100)
  82.         end repeat
  83.         set the end of randomNumericDataSets to thisSet
  84.     end repeat
  85.     return randomNumericDataSets
  86. end generateRandomNumericDataSets
Advertisement
Add Comment
Please, Sign In to add comment