使用Office应用程序更改word文档样式 [英] Change word document style with an app for Office
问题描述
我正在尝试使用Office js API为Word Office创建应用。
Im trying to create an app for Word Office using the Office js API.
我已经找到了如何获取和设置内容到word文档但我有一个很难找到如何更改样式(标题等)的内容
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?
推荐答案
你的回答是肯定的,现在,api这个词有点新,缺少一些可能让开发人员生活更轻松的功能。首先,api这个词不支持标题,解决这个问题的方法是创建一个Content控件并将其用作标题。我目前正在使用我使用的word应用程序中的这项工作。
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.
现在对于样式,样式很棒。如果用户的PC当前在他们的电脑上安装了这些样式,您可以轻松地引用这些样式。但是,作为开发人员,您可能已经发现生活并不容易。所以我也运行了这个问题并通过首先插入我想要的内容,然后制作段落代理对象的集合来接近它。然后我加载两个对象,并同步。在我遍历段落集合并添加我自定义样式之后,这是我制作的预设对象。以下功能将向您展示我试图解释的内容。我希望这个帮助
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);
};
这篇关于使用Office应用程序更改word文档样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!