0

我正在尝试在我的 word 文档中插入一个表格,但每次我InvalidArgument在控制台中遇到异常时。

这是我的代码:

Word.run(function (context) {
            var selectionRange = context.document.getSelection();
            var paragraph = selectionRange.insertParagraph("", Word.InsertLocation.before);

            return context.sync()
                .then(function () {
                    var table = paragraph.insertTable(3, 4, Word.InsertLocation.before, [
                        ["Column1", "Column2", "Column3", "Column4"],
                        ["test1", "test2", "test3", "test4"],
                        ["test5", "test6", "test7", "test8"]
                    ]);
                    table.styleBuiltIn = Word.Style.gridTable1Light;
                })
                .then(context.sync)
                .catch(function (error) {
                    console.log(error);
                });

        })
        .catch(function (error) {
            console.log('Error: ' + JSON.stringify(error));
            if (error instanceof OfficeExtension.Error) {
                console.log('Debug info: ' + JSON.stringify(error.debugInfo));
            }
        });

有什么我做错了吗?

4

2 回答 2

0

尝试改变

var paragraph = selectionRange.insertParagraph("", Word.InsertLocation.before);

var table = paragraph.insertTable(3, 4, Word.InsertLocation.before, [
                        ["Column1", "Column2", "Column3", "Column4"],
                        ["test1", "test2", "test3", "test4"],
                        ["test5", "test6", "test7", "test8"]
                    ]);

var paragraph = selectionRange.insertParagraph("", "Before");

var table = paragraph.insertTable(3, 4, "Before", [
                        ["Column1", "Column2", "Column3", "Column4"],
                        ["test1", "test2", "test3", "test4"],
                        ["test5", "test6", "test7", "test8"]
                    ]);
于 2021-06-03T13:46:46.220 回答
0

我发现了问题。仅 Word API 版本 1.3 支持 insertTable,Office 2019 支持该版本。我正在使用仅适用于 1.1 的 Office 2016。

微软文档的参考链接: https ://docs.microsoft.com/de-de/office/dev/add-ins/reference/requirement-sets/word-api-requirement-sets

于 2021-06-04T05:43:45.117 回答