【问题标题】:Tell AppleScript to go to a specific window in Excel告诉 AppleScript 转到 Excel 中的特定窗口
【发布时间】:2020-07-02 03:14:39
【问题描述】:

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

有没有办法在 AppleScript 中传递对特定 Excel 文件的引用,而不是一般地告诉应用程序?或者可能只是引用电子表格而不必打开它...?

【问题讨论】:

    标签: excel macos applescript


    【解决方案1】:

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

    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 保存在“Computer Scripts”文件夹中的“Common”文件夹中

    在每个与 Excel 相关的 applescript 的开头,我添加了这一行:

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

    到了拿到工作簿和工作表的时候,我就用这个:

    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
    

    【讨论】:

      【解决方案2】:

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

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

      tell application "Microsoft Excel"
          set WB_Name to name of workbook 1
      
          tell workbook WB_Name
              -- Do your stuff
          end tell
      end tell
      

      很遗憾,您无法处理“已关闭”的文档...

      HTH

      【讨论】:

        猜你喜欢
        • 2012-04-16
        • 2011-12-10
        • 1970-01-01
        • 2015-11-13
        • 2013-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-21
        相关资源
        最近更新 更多