在标题文本可用之前,您需要
load
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);
});
}
从性能角度来看,这将产生一系列不必要的
电话。更好的选择是利用队列,这样您可以请求头标头,但可以在一个队列中获取所有头标头
同步
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);
}
});
}
});
})