Word Office.js:使用getOoxml()和insertOoxml()更新ContentControls中的表的问题 [英] Word Office.js: issues with updating tables in ContentControls using getOoxml() and insertOoxml()

查看:157
本文介绍了Word Office.js:使用getOoxml()和insertOoxml()更新ContentControls中的表的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新Word表单元格(位于Content Controls [CCs]中)的文本.我已经成功地使用Table Objects做到了这一点;但是对于拥有大量表且包含大量文档的客户而言,这太慢了.因此,我希望能够使用getOoxml和insertOoxml更新表单元格的值(希望更快,尽管我看到它在Word Online中可能很慢). 因此,我将使用getOoxml在CC中获取表Ooxml,然后以编程方式修改xml,然后将Ooxml重新插入CC中.

我能够成功获取Ooxml,并使用C#OpenXMLPowerTools以编程方式成功修改表Ooxml.但是,我无法将Ooxml成功插入到CC中.

如果在选定的表周围添加了CC,则在代码运行时,它始终会失败,并显示"InvalidArgument".请注意,以这种方式完成操作后,CC中表格上方或下方没有换行符(CC适合表格).这就是我大多数客户的桌子的样子.

如果将表添加到CC内(导致在表的上方/下方出现不必要的换行符),则代码将运行,但会导致添加额外的CC和换行符.因此,每次代码运行时,CC计数都会增加1.CC似乎相互嵌套.多余的CC不可接受,因为它们会使后续代码混乱.每次运行代码时,它还会添加一个额外的换行符.

我已经尝试过contentControls.items [0] .clear(). -这会删除文本,但不会删除表格或抄送表,因此无济于事.

以下是脚本实验室中的简化代码:

$("#getooxml").click(getooxml);
function getooxml() {
Word.run(function (context) {
    var contentControls = context.document.contentControls;
    context.load(contentControls);
    return context.sync().then(function () {
        var ooxml = contentControls.items[0].getOoxml(); //contains a table
        return context.sync().then(function () {
            contentControls.items[0].insertOoxml(ooxml.value, Word.InsertLocation.replace);
            return context.sync().then(function () {
                console.log('inserted OOXM.  CC count:' + contentControls.items.length);
            });
        });
    });
})
    .catch(function (error) {
        console.log('Error: ' + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log('Debug info: ' + JSON.stringify(error.debugInfo));
        }
    });
}

有没有可能使它起作用的方法? 例如,有没有一种方法可以修改Ooxml以使其正常工作?我怀疑它添加CC的原因是因为getOoxml()包括父CC本身,并且在插入到父CC中时,它添加了CC(以及任何嵌套的CC).如果是这样,如何从Ooxml中删除父CC?

我有版本1704(内部版本8067.2115)

这里有一条帖子,指示添加的段落问题将在下个月修复:

这是设计使然,因为对于表上的内容控件,内容控件的开始标记位于第一个单元格的内部,结束标记位于最后一个单元格的内部.因此,从开始标签到结束标签的范围不是整个表格.

当与文本获取/设置功能进行比较时, Ooxml的获取和设置功能也较慢.因此,我建议您这样做:

由于您可能有很多表,因此可以在正在使用的表上添加内容控件,并为此内容控件添加标签.此内容控件可以在整个表上,也可以仅在单元格中.

然后,您可以使用内容控件直接获取表.之后,您可以操作表格的文本.

代码可能是这样的:

Word.run(function (context) {
    var contentControl = context.document.contentControls.getByTag("forMyTable").getFirst();
    var table = contentControl.tables.getFirst();
    context.load(table);
    return context.sync().then(function () {
        var values = table.values;
        // add some text to the second cell in the second row.
        table.getCell(1, 1).value = values[1][1] + " extra";
        return context.sync().then(function () {
            console.log("inserted values.");
        });
    });
}).catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

I am trying to update the text in Word table cells (that are within Content Controls [CCs]). I have been doing this successfully using the Table Objects; however it is too slow for customers with large documents with many tables. So, I want to be able to use getOoxml and insertOoxml to update the table cell values (hopefully much more quickly, although I see it may be very slow in Word Online). So I would use getOoxml to get the table Ooxml within the CCs, then modify the xml programmatically, then insertOoxml back into the CCs.

I am able to get the Ooxml successfully, and programmatically modify the table Ooxml successfully using C# OpenXMLPowerTools. However, I have not been able to insertOoxml back into the CCs successfully.

If the CC was added around a selected table, then when the code runs, it always fails with "InvalidArgument". Note that when it is done this way, there is no line break above or below the table in the CC (the CC fits the table). This is how most of my customer's tables are.

If the table was added inside a CC (results in unwanted line breaks above/below the table), then the code runs, but results in adding an extra CC and a line break. So, every time the code runs the CC count increases by 1. The CCs appear to be nested inside each other. The extra CCs are not acceptable because they mess up subsequent code. It also adds an additional line break each time the code is run.

I've tried contentControls.items[0].clear(). - this removes text, but not tables or CCs, so it does not help.

Here is simplified code in Script Lab:

$("#getooxml").click(getooxml);
function getooxml() {
Word.run(function (context) {
    var contentControls = context.document.contentControls;
    context.load(contentControls);
    return context.sync().then(function () {
        var ooxml = contentControls.items[0].getOoxml(); //contains a table
        return context.sync().then(function () {
            contentControls.items[0].insertOoxml(ooxml.value, Word.InsertLocation.replace);
            return context.sync().then(function () {
                console.log('inserted OOXM.  CC count:' + contentControls.items.length);
            });
        });
    });
})
    .catch(function (error) {
        console.log('Error: ' + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log('Debug info: ' + JSON.stringify(error.debugInfo));
        }
    });
}

Are there any potential ways to make it work? For example, Is there a way I can modify the Ooxml to make it work? I suspect the reason it adds CCs is because the getOoxml() includes the parent CC itself and when inserted inside the parent CC, it adds the CC (and any nested CCs). If so, how can I remove the parent CC from the Ooxml?

I have Version 1704 (Build 8067.2115)

There is a post here indicating the added paragraph issue will be fixed next month: `context.document.body.insertOoxml` breaks documents, crashes word It also indicates there's an error where the "replace" option doesn't actually replace (but I don't think that's the case)

解决方案

This is by design, because for the content control on a table, the content control's start tag is INSIDE the first cell and the end tag is INSIDE the last cell. Therefore, the range from the start tag to the end tag is not the whole table.

Ooxml's getting and setting functions are also slower when you compare them with text getting/setting functions. Therefore, I suggest you do this way:

Since you may have many tables, you can add a content control on the table you are working on, and give a tag to this content control. This content control can be on the whole table or simply just in a cell.

Then you can use the content control to grab the table directly. After that, you can manipulate the table's text.

The code could be something like this:

Word.run(function (context) {
    var contentControl = context.document.contentControls.getByTag("forMyTable").getFirst();
    var table = contentControl.tables.getFirst();
    context.load(table);
    return context.sync().then(function () {
        var values = table.values;
        // add some text to the second cell in the second row.
        table.getCell(1, 1).value = values[1][1] + " extra";
        return context.sync().then(function () {
            console.log("inserted values.");
        });
    });
}).catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

这篇关于Word Office.js:使用getOoxml()和insertOoxml()更新ContentControls中的表的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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