1

I'm having difficulties to update the values of a Word.TableRow using the Javascript API. I've look to Doc and I can't find any hints that will help me to accomplish my duty...

Here is my Question: What is the best way to set the values of a TableRow inside a Word Document using the Javascript API.

And Here is what I tried:

Word.run((context: Word.RequestContext) => {
    var tables = context.document.body.tables;
    tables.load("items");
    return context.sync().then(() => {
        var firstTableRows = tables.items[0].rows;
        context.load(firstTableRows, "items");
        context.sync().then(() => {
            var header = firstTableRows.items[0];
            header.load("values");
            context.sync().then(() => {
                header.values = ["MyValue"]
                context.sync();
            });
        });
    });
}).catch(errorHandler);

This is a 1x2 table

No errors are thrown and the table is not getting updated...

Am I missing something?

5
  • Try removing the .catch(errorHandler); to see if you are generating an error. Commented Oct 17, 2016 at 20:10
  • Nop nothing is coming up Commented Oct 17, 2016 at 20:11
  • hey guys can you please share the Word build you are trying this on? seems to be an Office.js update issue (we need to ship an update to our preview API) Commented Oct 18, 2016 at 23:37
  • Here is my office.js version appsforoffice.microsoft.com/lib/beta/hosted/office.js Commented Oct 19, 2016 at 2:32
  • And I'm on the insider fast branch Commented Oct 19, 2016 at 2:33

1 Answer 1

0

Sorry for the delayed response here. There are a couple of issues with your code.

  1. the first one is associated with a bug we are currently fixing with keeping references of items mentioned in the load method. There is a related question on stack explaining this issue. Check it out here. (specially the detailed explanation in the comments).

  2. The second issue I see with your code is that you are trying to set the row values using a 1d array, when a 2D array is expected. (and this is an error in our documentation, we need to fix it thanks to you!)

You have a couple of options for changing values, this is the first one:

 Word.run(function (context) {

            var myTables = context.document.body.tables;
            context.load(myTables);
            return context.sync()
                .then(function () {

                var myRows = myTables.items[0].rows;
                context.load(myRows);
                return context.sync()

                .then(function () {
//assumes a table with at least 3 columns!
                   myRows.items[0].values = [["1", "3",  "4"]];
                  return context.sync()

                })
                      
            })
        })
    .catch(function (e) {
                app.showNotification(e.message);
            })
       
    }

Now, until the bug mentioned in the first point is fixed you need to do several trips to the host application in order to set the data you want. a potential shortcut is to use our new navigation options,specially if you want to change the first or last row values (please note that we are planning to rename the first to getFirst() by the time we ship the API, so this code will break in a couple of months, maybe already breaks if you are using the insider fast release), you can do something like this in just one call:

 Word.run(function (context) {
//this gets the first row of the first table in the document and sets its values. Please note the 2D array used!
       context.document.body.tables.first.rows.first.values =[["Juan", "Peter", "Jeff"]];
            
            return context.sync()
            .catch(function (e) {
                 app.showNotification(e.message);
            })
        })

Please give it a try to these options and let me know how it goes. thanks!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.