使用Javascript for Automation复制Numbers中的单元格

Dav*_*eAZ 0 applescript javascript-automation

我想使用JXA自动更新Numbers电子表格.例如,将一系列单元格从一个电子表格复制到另一个具有不同结构的单元格.

在这一点上,我只是测试一个简单的程序来设置或读取单元格的值,我无法让它工作.

当我尝试设置一个值时,我得到"错误-1700:无法转换类型".当我尝试读取值时,我会返回[object ObjectSpecifier]而不是文本或数字值.

这是代码的示例:

Numbers = Application('Numbers')
Numbers.activate()
delay(1)
doc = Numbers.open(Path('/Users/username/Desktop/Test.numbers'))
currentSheet = doc.Sheets[0]
currentTable = currentSheet.Tables[0]
console.log(currentTable['name'])
console.log(currentTable.cell[1][1])
currentTable.cell[1][1].set(77)
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我获取并输出[object ObjectSpecifier]两个console.logs,然后输出错误-1700:尝试设置单元格时无法转换类型.

我尝试过访问或设置属性的其他几种变体但无法使其工作.

提前致谢,

戴夫

小智 5

这是一个脚本,用于设置和获取单元格的值,然后在同一个表中设置不同的单元格值:

// Open Numbers document (no activate or delay is needed)

var Numbers = Application("Numbers")

var path = Path("/path/to/spreadsheet.numbers")
var doc = Numbers.open(path)

// Access the first table of the first sheet of the document

// Note:
//  .sheets and .tables (lowercase plural) are used when accessing elements
//  .Sheet and .Table (capitalized singular) are used when creating new elements

var sheet = doc.sheets[0]
var table = sheet.tables[0]

// Access the cell named "A1"

var cell = table.cells["A1"]

// Set the cell's value

cell.value = 20

// Get the cell's value

var cellValue = cell.value()

// Set that value in a different cell

table.cells["B2"].value = cellValue
Run Code Online (Sandbox Code Playgroud)

查看Numbers脚本字典(选择JavaScript作为语言)以查看类及其属性和元素.元素部分将显示元素的名称(例如,Document类包含工作表,Sheet类包含表,等等).要打开脚本字典,请在"脚本编辑器"的菜单栏中选择"窗口">"库",然后在库窗口中选择"数字".

关于您看到的日志记录 - 我建议使用类似于此的函数:

function prettyLog(object) {
    console.log(Automation.getDisplayString(object))
}
Run Code Online (Sandbox Code Playgroud)

Automation.getDisplayString为您传递给它的任何对象提供"漂亮的打印"版本.然后,您可以使用它来更好地进行诊断记录.