【问题标题】:Accessing cells with offset reference in Numbers with Applescript使用 Applescript 在 Numbers 中访问具有偏移参考的单元格
【发布时间】:2018-10-15 18:31:38
【问题描述】:

如何像 Excel VBA 一样在 Applescript 中引用相对于另一个单元格的单元格? 在 Excel VBA 中,我可以使用“偏移量”来设置单元格 D2 的值:

Range("A1").Offset(1,3).Value = "Example"

我到处搜索,但 Numbers Applescript 中似乎没有“偏移”命令,尽管它非常方便。

非常感谢任何帮助!

【问题讨论】:

    标签: applescript applescript-numbers


    【解决方案1】:

    补充大卫的出色回答...

    如果您需要模仿 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 放在您的脚本中,并根据需要调用它。


    上面的处理程序tablesheetdocument硬编码,每个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 statementerror statement。另见Working with Errors

    【讨论】:

      【解决方案2】:

      编辑:感谢 user3439894 指出我的第一个答案的问题 - 这是完全错误的,基于 Excel 而不是 Numbers。

      这是一个完整的脚本示例,展示了在 Numbers 中实现目标的一种方法:

      tell application "Numbers"
          tell table 1 of sheet 1 of document 1
              set c to cell "A1"'s column's address as number
              set r to cell "A1"'s row's address as number
              set value of cell (c + 3) of row (r + 1) to "Example"
          end tell
      end tell
      

      您可以将 set 命令压缩为一行,如下所示:

      tell application "Numbers"
          tell table 1 of sheet 1 of document 1
              set value of cell ((cell "A1"'s column's address as number) + 3) of row ((cell "A1"'s row's address as number) + 1) to "Example"
          end tell
      end tell
      

      【讨论】:

      • 感谢您指出这一点 - 我的第一个答案完全不合适。
      • 不错的编辑和出色的答案! +1 我还添加了一个答案来补充您的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 2018-03-28
      • 2010-11-19
      • 1970-01-01
      相关资源
      最近更新 更多