2

在 VBA 中,我可以做这样的事情来遍历 Excel 中的选定单元格:

for each c in Selection
   ' do things
next

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

set c to count of selection

结果是 c 设置为 0。

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

谢谢

4

3 回答 3

3

事实证明,您必须使用“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 编辑器上有更快的方法,但我是命令行人员。

于 2012-10-05T15:16:20.933 回答
0

你可以尝试这样的事情:

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

你可以在这里阅读更多

你也可以试试:

set cellCount to count of (cells of selection)
于 2012-10-05T07:29:29.560 回答
0

这显然是一个老问题,但我认为这是一个很好的问题,其中一个评论提出了复杂选择的问题。所以这里有几种方法可以让你使用 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
于 2021-07-15T10:40:16.303 回答