1
votes

Beta Library: https://appsforoffice.microsoft.com/lib/beta/hosted/office.js

Word 2016 Office insider Fast, Version 1610 (Build 7416.1000)


I've got a Word Document that has a simple Table in it (I'm using generating a table with a few rows and columns every time I test, no existing data), and I'm iterating through the rows attempting to populate the cells with text.

I get this error when I attempt this:

Error: ItemNotFound: ItemNotFound
Debug info: {"errorLocation":"TableRowCollection.getItem"}

My application is the Demo WordWebAddin created by MS visual studio, and my code looks like this:

Word.run(function (context) {
        var tables = context.document.body.tables
        tables.load({ expand:"rows/cells/items/body/text"})

        return context.sync().then(function () {
            var rows = tables.items[0].rows
            rows.items[1].cells.items[1].body.insertText("ryanasoefijasojef", Word.InsertLocation.replace)

            return context.sync();
        })
    })
    .catch(errorHandler);

I have tried a variety of mechanisms for navigating to the cell body - if I use cells.items[0].body.text I get the cell text body back successfully, so I'm able to use the cells the way I'd expect. But no matter what I do, I get some kind of itemNotFound exception. I've also tried a variety of values (or not values for the insert location parameter including (nothing), references to the InsertLocation objects, and the strings "replace", etc. Definitely a possibility I am using the API incorrectly, but I believe I am using it correctly according to the docs.

edit I would love any kind of confirmation. Either that I am doing it right, or wrong, or that it's not working for you, or something. Any help is appreciated!

1
I have the same problem. I'm not able to insert text inside a cell... I've tried to add text by passing a string[] to the "values" property of a "Word.Row" and the same problem occured.C1rdec
Hello Ryan, we actually think there is a Office.js sync problem for your specific build, we are investigating right, will update this question with an answer as soon as it becomes available.Juan Balmori
Just to confirm that this is exactly the issue, we will update the preview Office.js in a few days so that your code works with no problems. by then i will update with an answer including a sample on how to change specific cell values. Thanks for trying the preview!!!Juan Balmori

1 Answers

1
votes

My recommendation to you is to use the getCell method of the Table object. This method takes the zero-based coordinates of the specific cell you need within the table, then you can have access to the cell body easily. Check out the sample code below, also note that you dont need to expand anything for this code to work.

BTW you might have to switch back to the August Fork (16.7369.XXX) as there are breaking changes coming in the Office.js introduced in September fork, we renamed a few methods, our plan is to update the Office.js on the preview CDN once the September fork ships. Thanks for using the preview!

 Word.run(function (context) {
            var tables = context.document.body.tables
            tables.load()
            return context.sync().then(function () {
               tables.items[0].getCell(0, 0).body.insertText("ryanasoefijasojef", Word.InsertLocation.replace)
               return context.sync();
            })
        })
    .catch(function (e) {
                app.showNotification(e.message);
            })
       
    }