Word Javascript API - Can't access TableRows with index

18 Views Asked by At

I observed that it's not possible, to access row items per index. Here is an easy example code that throws an error.

await Word.run(async (context) => {
    const firstTable = context.document.body.tables.getFirst()
    await context.sync();
    firstTable.load("rows/items")
    await context.sync()
    const firstRow = firstTable.rows.items[0]
    firstRow.load("cells/items")
    await context.sync();
});

Error:

name: "RichApi.Error"
 code: "ItemNotFound"
 traceMessages: Array[0]
 innerError: null
▶debugInfo: Object
 code: "ItemNotFound"
 message: "ItemNotFound"
 toString: function toString()
 errorLocation: "TableRowCollection.getItem"
 statement: "var row = rows.getItem(...);"
▶surroundingStatements: Array[6]
 0: "var v = context.root._getObjectByReferenceId("02F!00000658");"
 1: "var rows = v.rows;"
 2: "// >>>>>"
 3: "var row = rows.getItem(...);"
 4: "// <<<<<"
 5: "row.load(["cells/items"]);"
▶fullStatements: Array[1]
 httpStatusCode: 404
 data: undefined

You can see that the code throws one step before executing the firstRow.load statement.

I wonder if I do anything wrong or what is the right approach here? If, for example, I want to access the last row, I don't want to iterate through all rows with .getNext().

0

There are 0 best solutions below