Range.ParentTable 给出 itemNotFound 异常

Range.ParentTable giving itemNotFound exception

我在word文档插入一个table的过程中写了我的代码。我的代码之前成功运行,但是当我现在 运行 它时,它给出了一个异常:

"ItemNotFound: ItemNotFound\n   at Anonymous function (https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:21:198669)\n   
at yi (https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:21:220646)\n   
at st (https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:21:220733)\n  
at d (https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:21:220553)\n 
at c (https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:21:219139)"

错误位置显示为

"errorLocation":"Range.ParentTable"

我不知道为什么代码现在不是 运行ning。我在一月份写了这段代码。如果有人可以,请帮我找出下面代码中的错误。

我的代码:

 this.insertTable = function () {
    Word.run(function (context) {
        var range = context.document.getSelection();
        var tableDataJsonString;

        if (range.parentTable != null) {
            var parentTable = range.parentTable;
            var parent_ContentController; //Content controller where the table has been inserted.
            var parent_contentController_id;
            var parent_contentController_tag;
            var parent_name; //Section or Element name
            var parent_id; //Section or Element's id
            var table_style; //Table style
            var count;
            var header_rowCount;
            var body_rowCount;
            var columns;
            var footer_rowCount;

            parentTable.load('rowCount');
            parentTable.load('headerRowCount');

            return context.sync().then(function () {
                if (parentTable.rowCount != null) {

                }
            }).then(function () {
        existing_table.setTableProperties(parent_contentController_id, parent, parent_id, body_rowCount, columns, header_rowCount, footer_rowCount);

                //tableDataJsonString = JSON.stringify(tableData);
                context.sync();

                if (count == null) {

                    _self.selectTableDlg(false, existing_table);
                }
                else if (count != 0) {
                    _self.selectTableDlg(true, existing_table);
                }
            }).catch(function (error) {
                showAlert('Exception');
            });
        }
    }).catch(function (error) {
        console.log('Error: ' + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log('Debug info: ' + JSON.stringify(error.debugInfo));
        }
    });
}

代码未通过 'return context.sync().then(function () {' 和 return 异常。

首先,如果代码是在 1 月份写回的,它使用的是 PREVIEW API,可能会发生变化,对此我们深表歉意,感谢您尝试使用它们! 在这种情况下,我们对 NULL 的工作方式进行了细微更改,错误是如何检查 table 是否存在。如此有效,您需要进行一些更改才能使您的代码正常工作。顺便说一句,有更多细节 who 在概念上与这个问题相同。

下面是一些代码,说明您需要如何检查选择是否在 table 内,以及我们通常如何处理可能 return 为空的属性和方法。

只需相应地调整您的代码,它就会起作用。

Word.run(function (context) {
        //you can get parteTable (will rise an exception if null) or parentTableOrNullObject (never throws an exception and lets you check if its null or not using isNullObject property)
        var myTable = context.document.getSelection().parentTableOrNullObject;
        context.load(myTable);
        return context.sync()
            .then(function () {
                if (myTable.isNullObject)
                    console.log("the selecion is NOT in a table");
                else
                    console.log("the selection is within a table");
            })
    })