【问题标题】:How to loop over selected cells in Excel with Applescript?如何使用 Applescript 循环遍历 Excel 中的选定单元格?
【发布时间】:2012-09-26 07:02:37
【问题描述】:

在 VBA 中,我可以执行类似的操作来遍历 Excel 中的选定单元格:

for each c in Selection
   ' do things
next

我试图在 AppleScript 中做同样的事情,但我似乎没有得到任何结果。我确实得到了当前单元格,但即使我这样做了

set c to count of selection

结果是 c 设置为 0。

Excel applescript 手册似乎没有帮助,谷歌搜索也没有。

谢谢

【问题讨论】:

    标签: excel macos applescript


    【解决方案1】:

    事实证明,您必须使用“count large”来获取选择中的单元格数量。一旦你到达那里,就很简单了——就像这样:

    tell application "Microsoft Excel"
        repeat with j from 1 to count large of selection
             -- do stuff with the cell
             set value of cell j of selection to "cell_" & j
        end repeat
    end tell
    

    为了到达那里我不得不做

    tell application "Microsoft Excel"
       set c to properties of selection
    end tell
    return c
    

    然后遍历属性列表,直到找到一个有希望的属性。这是进入房产清单的好方法。也许 AppleScript 编辑器上有更快的方法,但我是命令行人员。

    【讨论】:

    • 注意:如果您选择几个单元格,这不起作用,向下跳过一点,然后再选择几个单元格(当它们在同一列时我尝试过)。
    【解决方案2】:

    你可以试试这样的:

    tell application "Microsoft Excel"
        set range1 to range "A1:A5"
        set value of range1 to {{1.0}, {2.0}, {3.0}, {4.0}, {5.0}}
        set range2 to range "B1:B5"
        set value of range2 to {3}
    
        repeat with i from 1 to 5
            set formula of row i of column 3 to "=A" & i & "+B" & i
        end repeat
    end tell
    

    您可以阅读更多here

    你也可以试试:

    set cellCount to count of (cells of selection)
    

    【讨论】:

    • 实际上问题出在选择中的单元格 - 没有选择单元格(这是有据可查的)。不过还是非常感谢您的回答。
    • 我读你的问题太快了。我以为你在问如何“循环遍历 Excel 中的单元格”。
    【解决方案3】:

    这显然是一个老问题,但我认为这是一个很好的问题,其中一个 cmets 提出了复杂选择的问题。所以这里有几种方法可以让你使用 Mac Excel(在我的例子中是 2011 年)来解决这个问题。没有错误处理,但底部是一个脚本,它将预先填充工作表以进行演示。根据您的选择有多大,它可能会崩溃。

    首先,您可以做一件非常基本的事情:计算选定的单元格。这应该适用于简单和复杂的选择。

    tell application "Microsoft Excel" to count of cells of selection
    

    单区域选择

    其次,如果您有一个简单的选择,那么对单元格进行操作仍然不会太冗长。对于此处的测试,它适用于 2x2 范围。

    tell application "Microsoft Excel"
        select range "D2:E3"
        set c to selection
        value of areas of c
        --> {{7.0, 21.0}, {8.0, 24.0}} -- note hierarchy
        
        set voop to {}
        set ab to count of cells in c
        repeat with x from 1 to ab
            set end of voop to value of cell x of c as integer
        end repeat
        
        voop
        --> {7, 21, 8, 24} -- note absence of hierarchy
    end tell
    

    多区域选择

    第三,这是一种适用于复杂或简单选择的方法。同样,底部的脚本将填充合适的示例数据。

    areas of selection 返回选定范围的列表。它始终是一个列表,因此即使您只有一个范围(甚至一个单元格),您也会得到一个列表。考虑到这一点,这将遍历选定范围的列表,然后遍历每个范围中的每个单元格。该脚本比实际需要的要庞大,因为我包含了一些示例,您可以执行这些操作以及进行选择和返回结果。

    tell application "Microsoft Excel"
        activate
        -- make complex selection
        set aran to range "A:B 3:4"
        set bran to range "D2:E3"
        set cran to range "G5:H7"
        select (union range1 aran range2 bran range3 cran)
        
        with timeout of 10 seconds
            set c to areas of selection
            --> {range "[Workbook1]Sheet1!$A$3:$B$4" of application "Microsoft Excel", range "[Workbook1]Sheet1!$D$2:$E$3" of application "Microsoft Excel", range "[Workbook1]Sheet1!$G$5:$H$7" of application "Microsoft Excel"}
            
            set voop to {} -- value
            set coop to {} -- cell address
            
            repeat with euRg in c -- each range area
                set ceuRg to cells of euRg -- hierarchical cell list -- critical step!
                repeat with xy in ceuRg -- each cell in range area
                    -- do random stuff with each cell
                    copy contents of xy to end of coop
                    copy value of xy as integer to end of voop
                    set value of xy to ((value of xy) / 2)
                end repeat
            end repeat
            
            -- do more random stuff
            set AppleScript's text item delimiters to space
            display dialog voop as text -- display original values
            --> 8 66 6 44 7 21 8 24 1 2 3 4 5 6
            coop -- flattened cell list
            --> {cell "$A$3" of application "Microsoft Excel", cell "$B$3" of application "Microsoft Excel", cell "$A$4" of application "Microsoft Excel", cell "$B$4" of application "Microsoft Excel", cell "$D$2" of application "Microsoft Excel", cell "$E$2" of application "Microsoft Excel", cell "$D$3" of application "Microsoft Excel", cell "$E$3" of application "Microsoft Excel", cell "$G$5" of application "Microsoft Excel", cell "$H$5" of application "Microsoft Excel", cell "$G$6" of application "Microsoft Excel", cell "$H$6" of application "Microsoft Excel", cell "$G$7" of application "Microsoft Excel", cell "$H$7" of application "Microsoft Excel"}
            
        end timeout
    end tell
    

    预填充选择

    tell application "Microsoft Excel"
        activate
        set value of range "A:B 3:4" to {{8, 66}, {6, 44}}
        set value of range "D2:E3" to {{7, 21}, {8, 24}}
        set value of range "G5:H7" to {{1, 2}, {3, 4}, {5, 6}}
    end tell
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多