我似乎无法通过 Applescript 在 Excel 中使用列号和行号访问单元格或范围。
最小示例:
tell application "Microsoft Excel"
-- works
log range (get address row 1, column 1) of sheet 1 of active workbook
-- fails
set r to (range (get address row 1, column 1) of sheet 1 of active workbook)
end tell
为什么
log range (get address row 1, column 1) of sheet 1 of active workbook
边工作
set r to (range (get address row 1, column 1) of sheet 1 of active workbook)
失败?
log
命令之所以有效,是因为它执行的操作与 set
命令尝试执行的操作非常不同。
set
命令尝试将变量设置为无效语句的结果。结果出错了。
error "Microsoft Excel got an error: The object you are ¬
trying to access does not exist" number -1728 from range ¬
{"$1:$1", column "$A:$A"} of sheet 1 of active workbook
如果删除语句前面的命令,您将得到相同的错误。
(range {get address row 1, column 1} of sheet 1 of active workbook)
关键部分是下面这个,这不是有效范围。相反,它尝试将包含两个单独项目(字符串和列)的列表放入范围对象中:
range {"$1:$1", column "$A:$A"}
log
所做的事情是比较被动的。这是来自语言指南
要显示的值。表达式已计算,但对象说明符未解析。
本例中的对象说明符是 range,但由于语法不正确,因此无法解析。因此,
log
显示了零碎的内容,但 set
失败了。
如果您希望有一个包含所有第 1 行和所有 A 列的范围,那么这将有效:
tell application "Microsoft Excel"
set rowRange to range "1:1"
set colRange to range "A:A"
set r to union range1 rowRange range2 colRange
--> range "[Workbook1]Sheet1!$1:$1,$A:$A" of application "Microsoft Excel"
select r
end tell
您还可以模仿结果的语法:
tell application "Microsoft Excel"
set r to range "[Workbook1]Sheet1!$1:$1,$A:$A"
end tell
-- OR
tell application "Microsoft Excel"
tell active sheet of workbook 1
set r to range "$1:$1,$A:$A"
end tell
end tell