Im trying to create an app for Word Office using the Office js API.
I have found how to get and set content into the word document but Im having a hard time finding how to change things like Styles (Headings, etc.)
Before getting into how to do it, is it even possible?
The answer to you question is yes and now, the word api is a bit new and missing some functionalities which may make a developer life easier. First, headers are not supported in the word api, a work around for this is to create a Content control and use it as a header. I am currently using this work around in a word app that I use.
Now for styles, styles are awesome to use. If a user's pc currently have those styles installed on their pc you can easily reference those styles. However, as a developer you may have found out that life is not easy. So I too ran int this issue and approached it by first inserting my desired content, then making a collection of paragraphs proxy object. I then load both both objects, and sync. After I iterate through the collection of paragraphs and add my custom styles which is an preset object which I made. the following function will show you what I tried to explain. I hope this help
function InsertHtml(content, styleSelection) {
Word.run(function (context) {
var range = context.document.body.insertHtml(content, "end");
var paragraphs = context.document.body.paragraphs;
return context.sync().then(function () {
var index = $.map(headerStyles, function (obj, index) {
if (obj.name == styleSelection) {
return index;
}
})
for (var x = 0; x < paragraphs.items.length; x++) {
var paragraph = paragraphs.items[x];
paragraph.font.name = headerStyles[index].fontName;
paragraph.font.color = headerStyles[index].color;
paragraph.font.size = headerStyles[index].size;
paragraph.leftIndent = headerStyles[index].indent;
//paragraph.lineSpacing = headerStyles[index].lineSpacing;
//paragraph.alignment = headerStyles[index].alignment;
}
return context.sync();
});
})
.catch(feedBackMessage);
};
来源:https://stackoverflow.com/questions/24215681/change-word-document-style-with-an-app-for-office