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

发布于 2024-09-02 00:38:35 字数 238 浏览 1 评论 0原文

我有一个脚本,可以从 Excel(Mac Excel'04) 电子表格中提取信息,并通过本地数据库对其进行处理。我的问题(这是暂时的,等待使用 Excel '08 的专用脚本机)是当我需要在 Excel 中处理另一个电子表格时。我想确保 AppleScript 继续从正确的电子表格中读取数据。

有没有办法在 AppleScript 中传递对特定 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? Or possibly just referencing the spreadsheet without having to have it open ... ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

長街聽風 2024-09-09 00:38:35

我在一个单独的库中一次性定义了这个函数(以及许多其他函数)

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

将其另存为 ExcelLib.app 在“计算机脚本”文件夹中的“Common”文件夹

这一行:

set myExcelLib to load script alias ((path to library folder as string) & "Scripts:Common:ExcelLib.app")

中 在与 Excel 相关的每个 applescript 的开头,我添加了 当需要获取工作簿和工作表时,我只需使用以下方法:

set {myBook, mySheet} to myExcelLib's getActiveBookAndSheet()

然后每次您想要处理工作表的特定范围时,只需这样做:

set value of range "A2:F3" of mySheet to "etc." -- for a single line

tell mySheet
set value of range "A2:F3" to "etc." -- if you need to perform a whole list of operations 
end

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
太傻旳人生 2024-09-09 00:38:35

如果我理解正确的话,您希望在处理另一个工作簿时启动一个适用于给定工作簿的脚本。

以下构造应该可以解决问题:

tell application "Microsoft Excel"
    set WB_Name to name of workbook 1

    tell workbook WB_Name
        -- Do your stuff
    end tell
end tell

不幸的是,您无法处理“封闭”文档...

HTH

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文