使用Office.js在内容控件中插入表 [英] Insert table inside content control with Office.js

查看:94
本文介绍了使用Office.js在内容控件中插入表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i'm trying to insert a table inside the content control. Here is my code:

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));
        }
    });
}

但是我遇到了这个错误.

But i got this error.

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:

I'm using the beta word api:

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

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

Because on api version 1.1 there is not the method insertTable. Any idea why it doesn't work? I've seen on the documentation that this method is available on api version 1.3, are they released?

谢谢

推荐答案

我也遇到了同样的问题.事实证明,您不能在段落中间(或包含其他内容的段落中)插入表格.当您第一次添加一个段落,并将表格插入此段落中时,您将获得所需的效果.请参见下面的代码.

I was stuck with the same problem. It turns out you cannot insert a table in the middle of a paragraph (or in a paragraph that contains something else). When you first add a paragraph, and insert the table in this paragraph you get the desired effect. Please see the code below.

所有积分都属于 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);
            });
    });

这篇关于使用Office.js在内容控件中插入表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆