mingsai

Numbers Write Data from TSV into Table Selection

Feb 22nd, 2014
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. tell application "Numbers"
  2.     activate
  3.     try
  4.         if not (exists document 1) then error number 1000
  5.         tell document 1
  6.             try
  7.                 tell active sheet
  8.                     set the selectedTable to ¬
  9.                         (the first table whose class of selection range is range)
  10.                 end tell
  11.             on error
  12.                 error number 1001
  13.             end try
  14.             tell selectedTable
  15.                 -- determine the attributes of the selection range
  16.                 set the selectionRowCount to count of rows of selection range
  17.                 set the selectionColumnCount to count of columns of selection range
  18.                 set the selectedCellNames to the name of every cell of the selection range
  19.                 set the selectedCellCount to the length of the selectedCellNames
  20.                
  21.                 -- prompt user for the TSV file to read
  22.                 set thisTSVFile to ¬
  23.                     (choose file of type "public.tab-separated-values-text" with prompt ¬
  24.                         "Pick the TSV (tab separated values) file to import:")
  25.                
  26.                 -- read the data
  27.                 set thisTSVData to my readTabSeparatedValuesFile(thisTSVFile)
  28.                
  29.                 -- determine data structure
  30.                 set the dataGroupCount to the count of thisTSVData
  31.                 set the dataSetCount to the count of item 1 of thisTSVData
  32.                
  33.                 -- create read data summary
  34.                 set the infoMessage to ¬
  35.                     "The read TSV data is composed of " & dataGroupCount & ¬
  36.                     " groups, with each group containing " & dataSetCount & " items." & ¬
  37.                     return & return
  38.                
  39.                 -- prompt for desired data layout method
  40.                 set dialogMessage to ¬
  41.                     infoMessage & "Is the tabbed data to be grouped by row or column?"
  42.                 display dialog dialogMessage ¬
  43.                     buttons {"Cancel", "Row", "Column"} default button 1 with icon 1
  44.                 set the groupingMethod to the button returned of the result
  45.                
  46.                 -- check to see that data parameters match the selection parameters
  47.                 if the groupingMethod is "Row" then
  48.                     if selectionRowCount is not equal to dataGroupCount then ¬
  49.                         error "SELECTION MISMATCH. " & ¬
  50.                             "The number of rows in the table selection (" & selectionRowCount & ¬
  51.                             "), does not match the count of rows in the imported data (" & ¬
  52.                             dataGroupCount & ")."
  53.                     if selectionColumnCount is not equal to dataSetCount then ¬
  54.                         error "SELECTION MISMATCH. " & ¬
  55.                             "The number of columns in the table selection (" & selectionColumnCount & ¬
  56.                             "), does not match the count of columns in the imported data (" & ¬
  57.                             dataSetCount & ")."
  58.                 else
  59.                     if selectionColumnCount is not equal to dataGroupCount then ¬
  60.                         error "SELECTION MISMATCH. " & ¬
  61.                             "The number of columns in the table selection (" & selectionColumnCount & ¬
  62.                             "), does not match the count of columns in the imported data (" & ¬
  63.                             dataGroupCount & ")."
  64.                     if selectionRowCount is not equal to dataSetCount then ¬
  65.                         error "SELECTION MISMATCH. " & ¬
  66.                             "The number of rows in the table selection (" & selectionRowCount & ¬
  67.                             "), does not match the count of rows in the imported data (" & ¬
  68.                             dataSetCount & ")."
  69.                 end if
  70.                
  71.                 -- THE SELECTED CELLS WILL BE TARGETED USING THEIR CELL NAMES
  72.                 -- So order the cell name list to be all cells by row or column
  73.                 if the groupingMethod is "Row" then
  74.                     -- by default, Numbers provides cell names by top to bottom row, left to right
  75.                     set the orderedCellNames to selectedCellNames
  76.                     -- {"B2", "C2", "D2", "E2", "F2", "B3", "C3", "D3", "E3", "F3", "B4", "C4", "D4", "E4", "F4"}
  77.                 else
  78.                     -- sort the row-based list of cell names to column-based
  79.                     set the orderedCellNames to {}
  80.                     set groupCount to selectedCellCount div selectionRowCount
  81.                     repeat with i from 1 to groupCount
  82.                         repeat with q from i to selectedCellCount by groupCount
  83.                             set the end of the orderedCellNames to item q of selectedCellNames
  84.                         end repeat
  85.                     end repeat
  86.                     -- {"B2", "B3", "B4", "C2", "C3", "C4", "D2", "D3", "D4", "E2", "E3", "E4", "F2", "F3", "F4"}
  87.                 end if
  88.                
  89.                 -- import the data
  90.                 set counter to 1
  91.                 repeat with i from 1 to the count of thisTSVData
  92.                     set thisDataGroup to item i of thisTSVData
  93.                     repeat with q from 1 to the count of thisDataGroup
  94.                         set the targetCellName to item counter of orderedCellNames
  95.                         set the value of cell targetCellName to item q of thisDataGroup
  96.                         set the counter to counter + 1
  97.                     end repeat
  98.                 end repeat
  99.                
  100.                 (*
  101.                 set font size of selection range to 16
  102.                 set alignment of selection range to center
  103.                 *)
  104.             end tell
  105.         end tell
  106.     on error errorMessage number errorNumber
  107.         if errorNumber is 1000 then
  108.             set alertString to "MISSING RESOURCE"
  109.             set errorMessage to "Please create or open a document before running this script."
  110.         else if errorNumber is 1001 then
  111.             set alertString to "SELECTION ERROR"
  112.             set errorMessage to "Please select a table before running this script."
  113.         else
  114.             set alertString to "EXECUTION ERROR"
  115.         end if
  116.         if errorNumber is not -128 then
  117.             display alert alertString message errorMessage buttons {"Cancel"}
  118.         end if
  119.         error number -128
  120.     end try
  121. end tell
  122.  
  123. on readTabSeparatedValuesFile(thisTSVFile)
  124.     try
  125.         set dataBlob to (every paragraph of (read thisTSVFile))
  126.         set the tableData to {}
  127.         set AppleScript's text item delimiters to tab
  128.         repeat with i from 1 to the count of dataBlob
  129.             set the end of the tableData to ¬
  130.                 (every text item of (item i of dataBlob))
  131.         end repeat
  132.         set AppleScript's text item delimiters to ""
  133.         return tableData
  134.     on error errorMessage number errorNumber
  135.         set AppleScript's text item delimiters to ""
  136.         error errorMessage number errorNumber
  137.     end try
  138. end readTabSeparatedValuesFile
Advertisement
Add Comment
Please, Sign In to add comment