使用 Javascript API 在 Word 文档中的光标处插入 table

Inserting table at cursor in Word document with Javascript API

问题

目前我正在开发一个为客户端提供模板功能的Word插件。我想在光标附近放置一个包含单个单元格 table 的 ContentControl。 Javascript API 提供了此功能,如 here (ContentControl class) 所示。遗憾的是,可以将 table 放在控件内容之前或之后,但不能用 table 替换内容。每当我尝试实现后者时,我都会收到 InvalidArgument 异常。

同样,我无法在选择的文档中直接添加 table(没有 ContentControl)。抛出相同的异常。然而,在这种情况下,指定哪个位置选项并不重要(之前、之后、替换内容等)。

使用的技术是React和typescript。


内容控制Table

private placeTableControl() {
    Word.run(function (context) {
        var values = [["Apple"]];

        let selectionRange = context.document.getSelection();
        let contentControl = selectionRange.insertContentControl();

        // This works
        // let table = contentControl.insertTable(1, 1, 'Start', values);

        // This doesn't
        let table = contentControl.insertTable(1, 1, 'Replace', values);

        return context.sync()
            .catch(function (error) {
                console.log(error);
            });
    });
}

添加 Table 不带 ContentControl

private placeTable() {
    Word.run(function (context) {
        var values = [["Apple"]];

        let selectionRange = context.document.getSelection();

        // This doesn't work.
        let table = selectionRange.insertTable(1, 1, 'Replace', values);

        // Neither does this.
        //let table = selectionRange.insertTable(1, 1, 'Start', values);

        // Or this.
        //let table = selectionRange.insertTable(1, 1, 'End', values);

        return context.sync()
            .catch(function (error) {
                console.log(error);
            });
    });
}

部分解决 - 建议 Cindy Meister

Cindy Meister 建议这可能与段落有关。 Tables 应位于段落中。由于我错误地指定了一个简单的数组而不是二维数组,所以我无法让它工作。这似乎现在有效。我现在将尝试使其与内容控件一起使用。

private placeTable() {
    Word.run(function (context) {
        var values = [["Apple"]];

        var selectionRange = context.document.getSelection();

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

        return context.sync()
            .then(function () {
                paragraph.insertTable(1, 1, "Before", values);
            })
            .then(context.sync)
            .catch(function (error) {
                console.log(error);
            });
    });
}

类似问题

找到了一个类似的问题here

Cindy Meister 建议先插入一个段落,因为 table 不能插入包含其他内容的段落。事实证明这是解决方案,请参见下面的代码。所有学分都属于 Cindy Meister。

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

作为第二个选项,您可以选择插入 table "after" 选区,它会为您创建段落。检查一下:(然后你可以 table.insertContentControl 用内容控件包装它)

async function insertTable() {
    await Word.run(async (context) => {
        // We need a 2D array to hold the initial table values
        let data = [["Apple", "Orange", "Pineapple"], ["Tokyo", "Beijing", "Seattle"]];
        let table = context.document.getSelection().insertTable(3, 3, "after", data);
        table.styleBuiltIn = Word.Style.gridTable5Dark_Accent2;

        await context.sync();
    });
}