作者:cl云中皓 | 来源:互联网 | 2023-10-11 13:06
我使用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字段存储在数据库中
用于检索的代码
function 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));
}
});
}
首先尝试插入一个段落,然后再插入内容控件,而不是直接插入内容控件,
代码
var cOntentControls= context.document.body.insertParagraph(“ text”,Word.InsertLocation.end); contentControls.insertOoxml(currentOOXML,Word.InsertLocation.replace);
希望这会有所帮助!