内容控件嵌套在 word 中
Content Control getting nested in word
我使用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);
希望对您有所帮助!!!
我使用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);
希望对您有所帮助!!!