5

Is it possible to iterate through table rows and column after it is added in the document or worksheet.

I am using the following code to add a table and I want on the success of this code i should be able to access the rows and column should be able to replace the values of cells after some conversion. Bellow is the code that i am using to create the table.

     var tableData = new Office.TableData();
            var headers = [placeholder.columns.map(function (c) { return c.column; })];
            tableData.headers = headers
            tableData.rows = rows;
            var document = Office.context.document;

            document.setSelectedDataAsync(tableData, function (result) {
                var placeholder = Office.context.document.settings.get(results.binding.id);
                    if (officeCallSucceded(result, true)) {
                        document.bindings.addFromSelectionAsync(Office.BindingType.Table, function (result) {
                            if (officeCallSucceded(result)) {
                               //SOME  LOGIC FOR BINDING HERE TO ADD //EVENT handlers to the table just added
                            }
                        });
                    }
                }
                );
            }
Pradeep Gaba
  • 415
  • 3
  • 17

1 Answers1

0

Yes, here's the code to retrieve any individual row of a Table in Excel:

Excel.run(function (ctx) { 
    // substitute 'Table1' with the table you want and '0' with your row index
    var myRow = ctx.workbook.tables.getItem('Table1').rows.getItemAt(0);
    myRow.load('values');
    return ctx.sync().then(function() {
        console.log(myRow.values);
    });
});

To replace content in a row:

Excel.run(function (ctx) { 
    var myNewRow = [["a", "b", "c"]];
    // substitute 'Table1' with the table you want and '0' with your row
    var row = ctx.workbook.tables.getItem('Table1').rows.getItemAt(0);
    row.values = myNewRow;
    return ctx.sync();
});

In Word there's a similar TableRowCollection.getItem method, but it's still in preview: https://github.com/OfficeDev/office-js-docs/blob/WordJs_1.3_Openspec/word/resources/tablerowcollection.md#getitemindex-number

Michael Saunders
  • 2,662
  • 1
  • 12
  • 21
  • thanks for your reply @Michael but with code i will be able to replace the values of cell ?? – Pradeep Gaba Jul 14 '16 at 18:15
  • thanks @Michael this works i tried some code for word as well that i will mention . I was asking this question because in cell there is html thats coming from DB and I wanted that html in cell i should be able to access that i am able to access now but not able to convert the cell html into normal text. :( – Pradeep Gaba Jul 15 '16 at 06:41
  • //this code is for two rows and two columns table that can be chnaged as per need Word.run(function (ctx) { var table = ctx.document.body.tables.first; ctx.load(table); return ctx.sync().then(function () { table.values[0][0] = [["

    One

    ", "

    Two

    "], ["

    Three

    ", "
    VVVV
    "]]; return ctx.sync().then(function () { }); }).catch(function (e) { }); }) .catch(function (e) { console.log(e.message); });
    – Pradeep Gaba Jul 15 '16 at 06:45