补充大卫的出色回答...
如果您需要模仿 VBA 表达式,可以将 David 答案中的示例代码放入 处理程序,如下例所示。
on setCellOffsetValue(cl, co, ro, val)
tell application "Numbers"
tell table 1 of sheet 1 of document 1
set c to cell cl's column's address as number
set r to cell cl's row's address as number
set value of cell (c + co) of row (r + ro) to val
end tell
end tell
end setCellOffsetValue
现在您可以通过调用它在同一个脚本中多次使用它,例如:
setCellOffsetValue("A1", 3, 1, "Example")
正如你在这个版本中看到的那样,setCellOffsetValue handler 需要四个参数:
-
cl - 要偏移的单元格。
-
co - 列与单元格的偏移量。
-
ro - 与单元格的行偏移)。
-
val - 偏移单元格的值。
将setCellOffsetValue handler 放在您的脚本中,并根据需要调用它。
上面的处理程序有table、sheet和document硬编码,每个1。但是,在本例中,您还将该信息传递给 处理程序:
on setCellOffsetValue(cl, co, ro, val, t, s, d)
tell application "Numbers"
tell table t of sheet s of document d
set c to cell cl's column's address as number
set r to cell cl's row's address as number
set value of cell (c + co) of row (r + ro) to val
end tell
end tell
end setCellOffsetValue
现在您可以通过调用它在同一个脚本中多次使用它,例如:
setCellOffsetValue("A1", 3, 1, "Example", 1, 1, 1)
或者:
setCellOffsetValue("A1", 3, 1, "Example", "Table 1", "Sheet 1", "Untitled")
最后三个参数可以是它们的数值或名称值,视当时的需要而定。
此版本适用于具有多个表格和/或工作表的文档,并且需要定位table 1 of sheet 1 of document 1以外的其他对象。
正如你在这个版本中看到的,setCellOffsetValue handler 需要七个参数:
-
cl - 要偏移的单元格。
-
co - 与单元格的列偏移量。
-
ro - 与单元格的行偏移)。
-
val - 偏移单元格的值。
-
t - 表号或名称。
-
s - 工作表编号或名称。
-
d - 文档编号或名称。
注意:示例 AppleScript 代码就是这样,并且不包含任何可能适当的错误处理。用户有责任根据需要或需要添加任何错误处理。查看 AppleScript Language Guide 中的 try statement 和 error statement。另见Working with Errors。