【问题标题】:Get Current rows cells values one by one on "BindingSelectionChanged" event from table in html form in Word using office.js使用office.js从Word中的html表单中的表格中的“Binding SelectionChanged”事件中一一获取当前行单元格值
【发布时间】:2016-11-13 08:17:39
【问题描述】:

您好,我正在使用 office.js 创建一个应用程序,该应用程序将用于 excel 和 word 应用程序 addIn,并且我编写了一些代码,实际上逐个单元格地给出了整个行的文本。但是因为我的要求是维护每个单元格的样式并将它们存储在数据库中,这样当插件再次运行时,它应该以存储的相同格式加载数据。目前这只是我得到的回应。我已经问过一个类似的问题,即从当前单元格中获取具有真正效果的样式的文本。 How do I get the formatting the Current cell of the table in Word using office.js

如果可以按行和列位置获取单元格 html,那么还有一件事也可以解决问题。 谢谢!

【问题讨论】:

    标签: add-in office-js office-app


    【解决方案1】:

    您好,我找到了我的问题的解决方案,但这是仅适用于 word 的解决方案,这在 excel 中不起作用,但这对我有用,所以我在这里写:-

       function Addtable() {
        var document = Office.context.document;
        var headers = [["Cities"]];
        var rows = [['<b>Hello there</b> <ul><li>One</li><li>Two</li></ul>'], ['Roma'], ['Tokyo'], ['Seattle']];
        var html = '<table>';
        html += '<thead>';
        for (var i = 0; i < headers.length; i++) {
            html += '<tr>';
            var cells = headers[i];
            for (var j = 0; j < cells.length; j++) {
                html += '<th>' + cells[j] + '</th>';
            }
            html += '</tr>';
        }
        html += '</tr>';
        html += '</thead>';
        html += '<tbody>';
        for (var i = 0; i < rows.length; i++) {
            html += '<tr>';
            var cells = rows[i];
            for (var j = 0; j < cells.length; j++) {
                html += '<td>' + cells[j] + '</td>';
            }
            html += '</tr>';
        }
        html += '</tbody>';
        html += '</table>';
    
        Office.context.document.setSelectedDataAsync(html, { coercionType: Office.CoercionType.Html }, function (asyncResult) {
            document.bindings.addFromSelectionAsync(Office.BindingType.Table, function (result) {
                console.log(result);
    
                var binding = result.value;
                binding.addHandlerAsync(Office.EventType.BindingSelectionChanged, onBindingSelectionChanged);
            });
        });
    }
    

    上面是我想生成一个包含html值的表格时调用的函数。

    下面是我用来获取当前单元格的值并替换为一些虚拟值的代码。

      var onBindingSelectionChanged = function (results) {
        if (!isExecuted) {
            Word.run(function (context) {
                var tableCell = context.document.getSelection().parentTableCell;
                context.load(tableCell);
    
                return context.sync()
                    .then(function () {
                        if (tableCell.isNull == true) {
                            //selection is not within a cell..... 
                            console.log("selection not in a header");
                        }
                        else {
                            // the selection is inside a cell! lets get the content....
                            var body = tableCell.body;
                            var html = tableCell.body.getHtml();
    
                            var tableHtml = tableCell.body.getHtml();
    
                            context.sync()
                               .then(function () {
                                   var cellHtml = html.value;
                                   var newHtml = "<table><tr><td><ul><li>yellow</li></ul></td></tr></table";
    
                                   // Option 1
                                   body.insertHtml(newHtml, Word.InsertLocation.replace);
    
                                   // Option 2
                                   //body.clear();
                                   //body.insertHtml(newHtml, Word.InsertLocation.end);
    
                                   return context.sync().then(function () {
                                       console.log('HTML was successfully replaced.');
                                   }).catch(function (e) {
                                       console.log(e.message);
                                   });
                               }).catch(function (e) {
                                   console.log(e.message);
                               });
    
                        }
                    }).catch(function (e) {
                        console.log(e.message);
                    });
            });
            isExecuted = true;
        }
        else {
            isExecuted=false;
        }
    
    };
    

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多