【问题标题】:Insert table inside content control with Office.js使用 Office.js 在内容控件中插入表格
【发布时间】:2018-02-15 00:15:43
【问题描述】:

我正在尝试在内容控件中插入一个表格。这是我的代码:

function insertTable() {
    Word.run(function (context) {
        var range = context.document.getSelection();
        var cc = range.insertContentControl();
        cc.title = "My Table";
        var values = [["Apple", "red", "round"], ["Banana", "yellow", "long"], ["Pear", "green", "oblong"]];
        context.load(cc);
        return context.sync().then(function () {
                var table = cc.insertTable(3, 3, 'Start', values);
            })
            // Synchronize the document state by executing the queued commands, 
            // and return a promise to indicate task completion.
            .then(context.sync);
    })
    .catch(function (error) {
        console.log('Error: ' + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log('Debug info: ' + JSON.stringify(error.debugInfo));
        }
    });
}

但是我收到了这个错误。

Error: {"name":"OfficeExtension.Error","code":"InvalidArgument","message":"InvalidArgument","traceMessages":[],"innerError":null,"debugInfo":{"code":"InvalidArgument","message":"InvalidArgument","errorLocation":""},"stack":"InvalidArgument: InvalidArgument\n   at Anonymous function (https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:21:211625)\n   at yi (https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:21:249536)\n   at st (https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:21:249623)\n   at d (https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:21:249443)\n   at c (https://appsforoffice.microsoft.com/lib/beta/hosted/word-win32-16.01.js:21:248029)"}

我正在使用 beta 单词 api:

<script src="https://appsforoffice.microsoft.com/lib/beta/hosted/office.js" type="text/javascript"></script>

因为在 api 版本 1.1 上没有方法 insertTable。知道为什么它不起作用吗?我在文档中看到此方法可用于 api 版本 1.3,它们是否已发布?

谢谢

【问题讨论】:

    标签: javascript ms-word office365 office-js


    【解决方案1】:

    我遇到了同样的问题。事实证明,您不能在段落中间(或包含其他内容的段落中)插入表格。当你第一次添加一个段落,并在这个段落中插入表格时,你会得到想要的效果。请看下面的代码。

    所有学分都属于Cindy Meister

    function placeTable() {
    
        Word.run(function (context) {
            var values = [["Apple"]];
            var selectionRange = context.document.getSelection();
            var paragraph = selectionRange.insertParagraph("", "Before");
    
            return context.sync()
                .then(function () {
                     var table = paragraph.insertTable(1, 1, "Before", values);
                     var contentControl = table.insertContentControl();
                })
                .then(context.sync)
                .catch(function (error) {
                    console.log(error);
                });
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多