mingsai

Numbers Populate New Table with Column Based Data

Feb 22nd, 2014
179
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 column count based on data length
  6.     -- determine row 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 columnCount to (the count of generatedData) + 1
  11.         set the rowCount to (the count of item 1 of generatedData) + 1
  12.     else
  13.         set the columnCount to the count of generatedData
  14.         set the rowCount to the count of item 1 of generatedData
  15.     end if
  16.     tell document 1
  17.         tell active sheet
  18.             set thisTable to ¬
  19.                 make new table with properties ¬
  20.                     {row count:rowCount, column count:columnCount, name:"Column Data Table"}
  21.             tell thisTable
  22.                 if useHeaders is true then
  23.                     -- set the starting indexes for using headers
  24.                     set rowIndex to 1
  25.                     set columnIndex to 1
  26.                     -- since headers are used, name the column & row headers
  27.                     -- title and style the column header cells
  28.                     set x to 1
  29.                     repeat with i from 2 to the columnCount
  30.                         tell cell 1 of column i
  31.                             -- set the value and styling of the cell
  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 columnCellCount to count of cells of column 2
  56.                 repeat with i from 1 to count of the generatedData
  57.                     -- get a data set from the data set list
  58.                     set thisColumnData to item i of the generatedData
  59.                     tell column (columnIndex + i)
  60.                         -- iterate the data set, populating column cells from top to bottom
  61.                         repeat with q from 1 to the count of thisColumnData
  62.                             -- set the value and styling of the cell
  63.                             tell cell (rowIndex + q)
  64.                                 set value to item q of thisColumnData
  65.                                 set alignment to center
  66.                                 set vertical alignment to center
  67.                             end tell
  68.                         end repeat
  69.                     end tell
  70.                 end repeat
  71.             end tell
  72.         end tell
  73.     end tell
  74. end tell
  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