首页 > 解决方案 > 尝试删除包含表中内容控件的行时,Word Addin 崩溃

问题描述

我正在开发一个 Word 插件(Word API + Office.js),我正在使用内容控件,我正在尝试读取内容控件中的表格内容,我需要在其中删除空行

示例:我在内容控件中有这个表我必须删除空白行

在此处输入图像描述

我可以使用此代码实现此功能,但是如果表包含一个空白的内容控件,那么当我尝试删除该行时,插件本身就会崩溃。

在此处输入图像描述

function checktable(element) {
        Word.run(function (context) {
            // Queue a command to get the current selection and then
            // create a proxy range object with the results.
            var contentControl = context.document.contentControls.getByTag('control1').getFirst();

            var table = contentControl.tables.getFirst();

            context.load(contentControl, 'tables');

            table.load('values');

            return context.sync()
                .then(function () {
                    // Get the longest word from the selection.
                    if (contentControl.tables.items.length === 0) {
                        document.getElementById('lblstatus').innerText += "No Tables found";
                    }
                    else {

                        document.getElementById('lblstatus').innerText += " Tables found";
                        var Tablevaules = table.values;

                        for (var i = 0, len = Tablevaules.length; i < len; i++)
                        {
                            var nullcheck = "";
                            var inner=Tablevaules[i];

                             // inner loop applies to sub-arrays
                            for (var j = 0, len2 = inner.length; j < len2; j++) {
                                // accesses each element of each sub-array in turn
                                if (inner[j] == "") {
                                    if (nullcheck != "False") {
                                        nullcheck = "True";
                                    }
                                }
                                else {
                                    nullcheck = "False";

                                }
                            }

                            if (nullcheck == "True") {
                                table.deleteRows(i);
                            }
                        }
                    }


                })
                .then(context.sync)
                .then(function () {

                    // Queue a command to highlight the search results.
                    document.getElementById('lblstatus').innerText += element + ":" + "Successs";

                });
        })
            .catch(errorHandler);
    }

请让我知道我是否遗漏了什么或已知的错误!

标签: ms-wordoffice-jsword-addinsword-web-addins

解决方案


推荐阅读