首页 > 解决方案 > 如何使用 AppleScript 遍历 excel 工作表?

问题描述

我是 AppleScript 的新手,所以这可能是也可能不是一个明显的问题。我需要遍历一个 excel 电子表格,其中每一行是一个设备,而列是我需要获取的有关该设备的信息。本质上,它是一个嵌套的 for 循环,我在其中遍历行,并且对于每一行,我都遍历其所有列。有人知道怎么做这个吗?提前致谢。

标签: excelapplescript

解决方案


这里。这将做到这一点:

 set NumOfColumns to 5
    set NumOfRows to 5

    tell application "Microsoft Excel"
        tell document 1
            tell worksheet 1

                --to iterate through rows
                repeat with z from 1 to NumOfRows

                    --to iterate through columns
                    repeat with i from 1 to NumOfColumns

                        --to set a value (here it is adding a number "i as a string". 
                        --I know, you are not asking how to set a value, but here it is anyway!)
                        set value of column i of row z to (i as string)

                        --to retrieve a value (here it is copying the number to a variable ValueX)
                        set ValueX to (value of cell i of row z)
                    end repeat

                end repeat
            end tell
        end tell
    end tell

我希望它有所帮助。


推荐阅读