Tell AppleScript to go to a specific window in Excel
I've got a script that pulls information from an Excel(Mac Excel'04) spreadsheet, and processes it through a local database. My problem(which is temporary, pending a dedicated scripting machine w/ Excel '08) is when I need to work on another spreadsheet in Excel. I want to ensure that the AppleScript continues reading data from the correct spreadsheet.
Is there a way to pass reference to the specific Excel file in AppleScript, as opposed to just telling the Application in general? O开发者_如何学Pythonr possibly just referencing the spreadsheet without having to have it open ... ?
I defined once for all in a separate lib this function (among many others)
on getActiveBookAndSheet()
try
tell application "Microsoft Excel"
set theBook to workbook (get name of active workbook)
set theSheet to worksheet (get name of active sheet) of theBook
# set theSheet to worksheet (get entry index of active sheet of theBook) -- alternative
return {theBook, theSheet}
end tell
on error
error "Could't find any opened document in Excel."
end try
end getActiveBookAndSheet
Saved this as ExcelLib.app in a folder "Common" in "Computer Scripts" folder
In the beginning of each applescript related to Excel, I add this line :
set myExcelLib to load script alias ((path to library folder as string) & "Scripts:Common:ExcelLib.app")
And when comes the time to get the workbook and worksheet, I just use this :
set {myBook, mySheet} to myExcelLib's getActiveBookAndSheet()
Then each time you want to work on a specific range of the sheet, just do it this way :
set value of range "A2:F3" of mySheet to "etc." -- for a single line
or
tell mySheet
set value of range "A2:F3" to "etc." -- if you need to perform a whole list of operations
end
If I understand correctly, you wish to launch a script that works on a given workbook while you work on another.
the following construct should do the trick:
tell application "Microsoft Excel"
set WB_Name to name of workbook 1
tell workbook WB_Name
-- Do your stuff
end tell
end tell
Unfortunately, you cannot work on "closed" documents...
HTH
精彩评论