我想使用office.js
制作所有部分的标题表。我无法从一个部分获取标题文本。 section对象中有一个返回body对象的方法。如何从该主体对象中获取标题文本?
sectionObject.getHeader(type);
这是我的代码:
Word.run(function (context) {
//Create table here
var sections = context.document.sections;
context.load(sections);
return context.sync()
.then(function () {
if (sections != null) {
var items = sections.items;
var itemsCount = items.length;
for (var i = 0; i < itemsCount; i++) {
var currentSection = items[i];
var headerBody =currentSection.getHeader('primary');
//Get headerText here
//??
//Write the text into table
}
}
}).catch(function (myError) {
showNotification("Error", myError.message);
});
}).catch(errorHandler);
请忽略表创建/写入注释。
我尝试过一种方法:从body对象获取`段落集合。然后得到第一段文字。这实际上是标题文本。在这种情况下,我必须加载和同步段落集合来获取项目。现在,如果我将集合加载并同步到循环中,循环只进行一次,这样我只得到一个节的标题。但我需要所有部分的标题。这是代码:
Word.run(function (context) {
//Create table here
var sections = context.document.sections;
context.load(sections);
return context.sync()
.then(function () {
if (sections != null) {
var items = sections.items;
var itemsCount = items.length;
for (var i = 0; i < itemsCount; i++) {
var currentSection = items[i];
var headerBody = currentSection.getHeader('primary');
var headerBodyParagraps = headerBody.paragraphs;
context.load(headerBodyParagraps);
return context.sync()
.then(function () {
if (headerBodyParagraps != null) {
var headerBodyParaItems = headerBodyParagraps.items;
var headerText = headerBodyParagraps.items[0].text;
//Write the headerText into table
//
}
});
}
}
}).catch(function (myError) {
//otherwise we handle the exception here!
showNotification("Error", myError.message);
});
}).catch(errorHandler);
我尝试了另一种方式。而不是为每个部分加载“headerBodyParagraps”一次,在数组中添加“headerBodyParagraps”并一次加载所有这些。但是加载集合数组会引发错误:无法在不同的请求上下文中使用该对象。这是代码:
Word.run(function (context) {
//Add table here
var sections = context.document.sections;
context.load(sections);
return context.sync()
.then(function () {
if (sections != null) {
var items = sections.items;
var itemsCount = items.length;
var paraList = [];
for (var i = 0; i < itemsCount; i++) {
var currentSection = items[i];
var headerBody = currentSection.getHeader('primary');
var headerBodyParagraps = headerBody.paragraphs;
paraList[i] = headerBodyParagraps;
}
context.load(paraList); // Throws exception("Cannot use the object across different request contexts ") here.
context.sync()
.then(function () {
if (paraList != null) {
for (var j = 0; j < itemsCount; j++) {
var headerBodyParaItems = paraList[j].items;
var headerText = paraList[j].items[0].text;
//Add headertext to table here
}
}
});
}
}).catch(function (myError) {
//otherwise we handle the exception here!
showNotification("Error", myError.message);
});
}).catch(errorHandler);
感谢阅读。欢迎任何类型的小费/帮助!
答案 0 :(得分:1)
在标题文字可用之前,您需要load
&amp; sync
要检索它。例如:
for (var i = 0; i < itemsCount; i++) {
var currentSection = items[i];
var headerBody = currentSection.getHeader('primary');
context.load(headerBody);
context.sync().then(function () {
console.log(headerBody.text);
});
}
从性能角度来看,这会产生一堆不必要的sync
次调用。一个更好的选择是利用队列,以便您请求头标题,但在一次sync
调用中获取所有这些:
Word.run(function (context) {
var sections = context.document.sections;
context.load(sections);
return context.sync().then(function () {
if (sections != null) {
// Collection of headers we'll populate later
var headers = [];
// Iterate through the sections
for (var i = 0; i < sections.items.length; i++) {
// Grab the header from the current section
var header = sections.items[i].getHeader('primary');
// Add loading this header to the queue
context.load(header);
// Push this header into the headers collection
headers.push(header);
}
// Sync/Exectute the queued actions
context.sync().then(function () {
// Iterate through the headers collection
for (var i = 0; i < headers.length; i++) {
// Output each header's text to the console
console.log(headers[i].text);
}
});
}
});
})