如何使用Applescript循环遍历Excel中的所有行?

我试图循环遍历Excel中的所有行,并在每行上运行一些命令,但我不知道如何!

我最初是在rb-appscript(AppleScript的Ruby包装器)中做这件事的,但是决定在添加另一个DSL之前,先试着让它在Applescript中工作。 有小费吗? (rb-appscript版本也不错,虽然不是必需的)。

谢谢!

范围的坐标是一个可以dynamic创build的string。 循环你的行最简单的方法是这样的:

repeat with thisRow from 1 to 10 tell application "Microsoft Excel" set theRange to "A:" & thisRow & "Z:" & thisRow set theValue to (get value of range theRange) as list -- do something with the row's data end tell end repeat 

更新每个评论:为了得到需要计算的行数,我早就写了一个子程序来帮助解决这个问题。 确保你有一些“密钥”列是一贯填充的(换句话说,没有跳过的单元格)。 这是因为我的所有电子表格都有这些标题,

 on GetItemCount(KeyColumn) set RowNumber to 1 set theText to "cSyoyodylg" -- dummy value tell application "Microsoft Excel" repeat until theText is "" set RowNumber to RowNumber + 1 set theRange to KeyColumn & RowNumber & ":" & KeyColumn & RowNumber set dataRange to range theRange of sheet 1 set theText to (get value of range theRange) end repeat end tell set rowCount to RowNumber - 1 return rowCount end GetItemCount 

使用简单的做法是:将lastRow设置为GetItemCount(“A”)

 repeat with thisRow from 2 to lastRow + 1 -- go attack Excel end repeat 

我使用rubygem“roo”。 这是相当不错的!

Interesting Posts