Javascript Office插件开发:在Word 2016中插入表格

Javascript Office插件开发:在Word 2016中插入表格,javascript,ms-office,office-addins,office-js,Javascript,Ms Office,Office Addins,Office Js,我试图使用Office.js在文档正文中插入一个表,但没有成功 我使用了以下代码: function insertSampleTable() { showNotification("Insert Table", "Inserting table...") Word.run(function (context) { // Create a proxy object for the document body. var body = context

我试图使用Office.js在文档正文中插入一个表,但没有成功

我使用了以下代码:

function insertSampleTable() {

    showNotification("Insert Table", "Inserting table...")

    Word.run(function (context) {
        // Create a proxy object for the document body.
        var body = context.document.body;

        body.insertTable(2, 2, Word.InsertLocation.end, ["a"]);

        // Synchronize the document state by executing the queued commands, and return a promise to indicate task completion.
        return context.sync();
    })
    .catch(errorHandler);

}
但单击按钮后,会出现以下错误:

Error: TypeError: Object doesn't support property or method 'insertTable'
任何帮助都将不胜感激。我曾尝试查看Microsoft Office开发网站,但他们没有类似的示例

谢谢

您可以在任何范围/正文/段落对象上使用来完成此任务。代码如下:

Word.run(function (context) {
    context.document.body.insertHtml(
        "<table><tr><td>a</td><td>b</td></tr><tr><td>1</td><td>2</td></tr></table>",
        Word.InsertLocation.end
    );
    return context.sync().then(function(){});
}).catch(function(error){});
Word.run(函数(上下文){
context.document.body.insertHtml(
“ab12”,
Word.InsertLocation.end
);
返回context.sync().then(函数(){});
}).catch(函数(错误){});

-Michael Saunders(Office加载项的PM)

也许Michael不知道这一点,但我们最近提供了一个(现在是GA)表对象,您可以在word中使用它。并为您提供了比插入HTML更多的功能

以下是表对象的文档:

顺便说一句,你的代码有一个错误。预期参数为2D数组。因此,您需要提供如下内容:

Word.run(函数(上下文){
//为文档正文创建代理对象。
var body=context.document.body;
body.insertTable(2,2,Word.InsertLocation.end,[[“a”,“b”],[“c”,“d”]);
//通过执行排队命令同步文档状态,并返回指示任务完成的承诺。
返回context.sync();
}).catch(函数(e){
控制台日志(e.message);
})

Michael我们在1.3 API中添加了一个表对象。检查开放规范:嗨,胡安,调用该方法时我遇到了无效的争论。请您解释一下要求“a)确保您使用的是支持表API的构建”好吗?如果我使用的是Office 2016的已安装版本(不是365),该怎么办?我是否应该安装Office 2016部署工具?Office.js的Beta CDN版本出现问题,我们正在更新,请稍候几天。该API作为2016年4月Office更新的一部分作为预览发布,请确保您的版本晚于16.6965。只要转到File->Account,就会有更新Office的选项。第一个链接已断开,表对象的文档现在在这里:链接已固定在我的答案上。还要注意的是,API现在是GA。