0

我使用 Office js 和 word Api 1.3 创建了一个项目,它将存储在数据库中的内容控件的 Ooxml 数据加载到文档中。我在将具有内容控件的 Ooxml 数据加载到文档中时遇到问题

例如:如果我将 3 个内容控件加载到文档中

我的要求是在文档中依次加载内容控件

要求

但它加载第一个内容控件和第二个内容控件内部和第三个内容控件内部

结果

用于将内容控件的 Ooxml 数据存储到数据库中的代码:

        // Create a proxy object for the ConentControl body.
        var ConentControl = context.document.contentControls.getByTag('FirstControl').getFirst();

        // Queue a commmand to get the OOXML contents of the body.
        var OOXML = ConentControl.getOoxml(); 

我将此 OOXML 作为 xml 字段存储在数据库中

用于检索的代码

函数 GetTemplateData() {

    var apiurl = 'Service Url'


    $.ajax({
        url: apiurl,
        type: 'GET',
        dataType: 'json',
        //contentType: 'application/json',
        //data: JSON.stringify(searchDetails),
        async: false,
        success: function (data) {
            $.each(data, function (index, element) {
                var ControlName = element.ControlName;
                var ControlContent = element.ContentData;
                InsertOoxml(ControlName, ControlContent);

            });

        },
        error: function (d) {
            write("Error please try again");
        }
    });
}


function InsertOoxml(ControlName, ControlContent) {
    Word.run(function (context) {

        // Create a proxy object for the content controls collection that contains a specific tag.
        currentOOXML = ControlContent;
        if (currentOOXML != "" && currentOOXML != null) {
            // Create a proxy object for the document body.
            var DocBody = context.document.body;

            // Queue a commmand to insert OOXML in to the beginning of the body.
            DocBody.insertOoxml(currentOOXML, Word.InsertLocation.end);

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

                // Tell the user we succeeded and then clear the message after a 2 second delay
                report.innerText = "Section Loaded succeeded!";
                setTimeout(function () {
                    report.innerText = "";
                }, 2000);
            });
        }
        else {
            return context.sync().then(function () {

                // Tell the user we succeeded and then clear the message after a 2 second delay
                report.innerText = 'Data not available for the control in Database.';
                setTimeout(function () {
                    report.innerText = "";
                }, 2000);
            });
        }
    })
        .catch(function (error) {
            console.log('Error: ' + JSON.stringify(error));
            if (error instanceof OfficeExtension.Error) {
                console.log('Debug info: ' + JSON.stringify(error.debugInfo));
            }
        });
}
4

1 回答 1

2

与其直接插入内容控件,不如先尝试插入一段,再插入内容控件,

代码 var contentControls = context.document.body.insertParagraph("text",Word.InsertLocation.end); contentControls.insertOoxml(currentOOXML, Word.InsertLocation.replace);

希望这可以帮助!!!

于 2019-12-10T09:30:43.430 回答