How can I access a table added in Word or Excel and iterate through each row and cell using Office.js

Is it possible to iterate through the rows and columns of a table after adding to a document or worksheet.

I use the following code to add a table, and I want that, when this code runs successfully, I must have access to the rows, and the column should be able to replace the cell values ​​after some conversion. Below is the code that I use 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
                            }
                        });
                    }
                }
                );
            }
+4
source share
1 answer

Yes, there is code to extract any single string 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);
    });
});

:

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();
});

Word TableRowCollection.getItem, : https://github.com/OfficeDev/office-js-docs/blob/WordJs_1.3_Openspec/word/resources/tablerowcollection.md#getitemindex-number

0

All Articles