Reputation: 321
Is there a way to get the table of contents of a Word document including the styles used in an add-in?
I'm able to get the full text of the document (including the headings), using console.log(context.document.body.text)
but it just gives me the entire document as a string, stripping away all styles and formatting. I need to get the heading level of each heading so my add-in can check that whatever document I have open is structured correctly.
Upvotes: 0
Views: 716
Reputation: 131
Hope this work for you
I just console headers array
[{
key: keyValue
level: 1 // headers level
text: "Header 1 testing" // headers string
}]
Full Code :
Word.run(async function (context) {
const paragraphs = context.document.body.paragraphs.load('items');
const headers = [];
const headersString = [];
context
.sync()
.then(async function () {
await context.sync();
for (let i = 0; i < paragraphs.items.length; i++) {
const item = paragraphs.items[i];
context.load(item, ['text', 'style']);
// we can use outlineLevel 1,2,3
if (item.style === 'Heading 1' || item.style === 'Heading 2' || item.style === 'Heading 3') {
headers.push(paragraphs.items[i]);
}
}
console.log(headers);
headers.map((item) => {
headersString.push({
key: item._ReferenceId,
text: item.text.toString(),
level: item.outlineLevel,
});
});
console.log(headersString);
})
.then(context.sync);
}).catch(function(error) {
console.log('Error: ' + error);
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
console.log('Trace info: ' + JSON.stringify(error.traceMessages));
}
});
Upvotes: 0
Reputation: 25703
Any time the content of a Word document is queried using a Text
property only the string content will be returned, stripped of all formatting information.
Formatting information is available from Range
objects. In this case, get the Paragraphs
collection, iterate over it and check the Style
properties of the Paragraph.Range
that are relevant.
Alternatively, getting the OpenXML of the body will return a string with the content as Word Open XML in the OPC flat-file format. That can be "parsed" for all kinds of information.
Upvotes: 1