简体   繁体   English

告诉 AppleScript 转到 Excel 中的特定窗口

[英]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.我有一个脚本,可以从 Excel(Mac Excel'04)电子表格中提取信息,并通过本地数据库对其进行处理。 My problem(which is temporary, pending a dedicated scripting machine w/ Excel '08) is when I need to work on another spreadsheet in Excel.我的问题(这是暂时的,等待使用 Excel '08 的专用脚本机器)是我需要在 Excel 中处理另一个电子表格时。 I want to ensure that the AppleScript continues reading data from the correct spreadsheet.我想确保 AppleScript 继续从正确的电子表格中读取数据。

Is there a way to pass reference to the specific Excel file in AppleScript, as opposed to just telling the Application in general?有没有办法在 AppleScript 中传递对特定 Excel 文件的引用,而不是一般只告诉应用程序? Or 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在“计算机脚本”文件夹中的“Common”文件夹中将其另存为 ExcelLib.app

In the beginning of each applescript related to Excel, I add this line :在每个与 Excel 相关的 applescript 的开头,我添加了这一行:

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 HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM