Office加载项开发:在Word 2016中插入表 [英] Office Add-in development: Insert table in Word 2016

查看:147
本文介绍了Office加载项开发:在Word 2016中插入表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Office.js在文档正文中插入一个表但无济于事。

I am trying to insert a table using Office.js inside the body of a document but to no avail.

我使用了以下代码:

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

}

但点击按钮后,它会给我以下错误:

But upon clicking the button, it gives me the error below:

Error: TypeError: Object doesn't support property or method 'insertTable'

任何帮助将不胜感激。我曾尝试检查Microsoft Office Dev网站,但他们没有这样的样本。

Any help will be appreciated. I have tried to check the Microsoft Office Dev site but they don't have any samples like this one.

谢谢!

推荐答案

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

Maybe Michael is not aware of this, but we recently shipped (its now GA) a table object that you can use in word. And gives you WAY more functionalities than just inserting the HTML.

以下是表对象的文档:
https://docs.microsoft.com/en-us/javascript/api/ word / word.table?view = office-js

Here is the documentation for the table object: https://docs.microsoft.com/en-us/javascript/api/word/word.table?view=office-js

btw您的代码有错误。期望的参数是2D数组。所以你需要提供这样的东西:

btw your code has an error. the expected argument is a 2D array. so you need to supply something like this:

   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","b"], ["c","d"]]);

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

            console.log(e.message);
        })
        

希望这有帮助! !!

hope this helps!!!

谢谢!!
Juan(Word JavaScript API的PM)

thanks!! Juan (PM for the Word JavaScript API)

这篇关于Office加载项开发:在Word 2016中插入表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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