Advertisement
rccharles

applescript to excel example

Apr 9th, 2019
969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. try
  2.         -- I have no idea why this might fail, but since I found another way, I'll include it.
  3.         tell application "Microsoft Excel" to activate
  4.     on error errMsg number n
  5.         log "activate error..." & errMsg & " with number " & n
  6.         try
  7.             -- I have no idea why this might fail, but since I found another way, I'll include it.
  8.             do shell script "open '/Applications/Microsoft Office 2011/Microsoft Excel.app'"
  9.         on error errMsg number n
  10.             log "do shell script..." & errMsg & " with number " & n
  11.             display dialog "Goodbye. Error. " giving up after 12
  12.             return
  13.         end try -- for do shell script 
  14.        
  15.     end try -- for tell appp "Microsoft Excel"
  16.    
  17.    
  18.     delay 5 -- let excel load.
  19.    
  20.     tell application "Microsoft Excel"
  21.        
  22.         -- mystery why above tell doesn't work. Was working in the before as it was commented out below.
  23.         try
  24.             -- I have no idea why this might fail, but since I found another way, I'll include it.
  25.             activate
  26.         on error errMsg number n
  27.             log "activate error..." & errMsg & " with number " & n
  28.             display dialog "Activate Error.  Continuing. " giving up after 12
  29.         end try -- for do shell script 
  30.         try
  31.             -- this sometime fails.  Just today.  Run again.           
  32.             open excelName
  33.         on error errMsg number n
  34.             log "open  error..." & errMsg & " with number " & n
  35.             display dialog "Could not open the Excel document.  Giving up.  Excel should open to a spreadsheet, but sometimes excel wants to be 'helpful' and display something else. " & return & "Suggest you try again. " giving up after 20
  36.             return
  37.         end try
  38.        
  39.         tell active sheet
  40.             tell used range
  41.                 set rowCount to count of rows
  42.             end tell
  43.             -- Is the sheet blank?
  44.             if rowCount > 1 then
  45.                 -- sheet not blank, better add stuff at the end.
  46.             else
  47.                
  48.                 -- blank sheet. Insert headers
  49.                 -- This is a little odd.  Notice how the numbers are going across.
  50.                 tell row 1
  51.                     set value of cell 1 to "Number"
  52.                     set font style of font object of cell 1 to "Bold"
  53.                    
  54.                     set value of cell 2 to "Question"
  55.                     set font style of font object of cell 2 to "Bold"
  56.                     set column width of range 2 to 40
  57.                    
  58.                     set value of cell 3 to "Option A"
  59.                     set font style of font object of cell 3 to "Bold"
  60.                    
  61.                     set value of cell 4 to "Option B"
  62.                     set font style of font object of cell 4 to "Bold"
  63.                    
  64.                     set value of cell 5 to "Option C"
  65.                     set font style of font object of cell 5 to "Bold"
  66.                    
  67.                     set value of cell 6 to "Option D"
  68.                     set font style of font object of cell 6 to "Bold"
  69.                    
  70.                     set value of cell 7 to "Solution"
  71.                     set font style of font object of cell 7 to "Bold"
  72.                     set column width of range 7 to 40
  73.                     set wrap text of range 7 to true
  74.                    
  75.                     set value of cell 8 to "Added Date & Time"
  76.                     set font style of font object of cell 8 to "Bold"
  77.                     set column width of range 8 to 40
  78.                 end tell
  79.                
  80.                 tell column 2
  81.                     set wrap text to true
  82.                 end tell
  83.                 tell column 8
  84.                     set wrap text to true
  85.                 end tell
  86.             end if
  87.             set rowCount to rowCount + 1
  88.            
  89.             log "rowCount is " & rowCount
  90.         end tell -- active sheet
  91.     end tell -- excel  
  92.    
  93. -- continuation of example ---------------------------------------------------------------
  94.  
  95.         -- fill in a row of the excel sheet
  96.         tell application "Microsoft Excel"
  97.             activate
  98.             tell active sheet
  99.                 tell row rowCount
  100.                     set value of cell 1 to questionActualNumber
  101.                     set value of cell 2 to questionText
  102.                     repeat with i from 1 to count of answersArray
  103.                         set value of cell (2 + i) to item i of answersArray
  104.                     end repeat
  105.                     -- solution
  106.                     set value of cell 7 to answerLetter
  107.                     -- date & time
  108.                    
  109.                     set value of cell 8 to presentDT
  110.                 end tell
  111.             end tell -- active sheet
  112.         end tell -- excel  
  113.        
  114.         log "filled in excel row " & rowCount
  115.         set rowCount to rowCount + 1
  116.        
  117.     end repeat -- collectionSize
  118.    
  119.    
  120.     tell application "Microsoft Excel"
  121.        
  122.         close workbooks
  123.         quit
  124.        
  125.     end tell -- excel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement