【发布时间】:2016-08-17 13:19:29
【问题描述】:
我正在开发一个 Word 插件,用户可以在其中选择一些从 SharePoint 加载的预定义模板 (docx) 文档。用户通过向导设置文档中的内容控件。到目前为止,体验非常好。
但是,我在加载带有标题的 docx 文件时遇到了问题。
我正在使用这个函数来加载 docx 文件:(下面的完整代码)
body.insertFileFromBase64(templateDoc.base64String, Word.InsertLocation.end);
这可行,但有时源文档的标题不存在。或者其他时候它们在所有页面上,而第一页应该不同。
问题: 插入带有页眉和页脚的文档应该有效吗?我做错了什么吗?
private applyTemplate(template: Template): void {
if (!Office.context.requirements.isSetSupported("WordApi", 1.2)) {
this.errorMessage = 'Deze versie van Word wordt niet ondersteund';
this.showError = true;
return;
}
let calls: [
ng.IPromise<TemplateFile>
] = [
this.appService.getTemplateDocument(template.templateId)
];
this.q.all(calls)
.then((results: any[]) => {
let templateDoc: TemplateFile = results[0].data;
Word.run((context) => {
let body = context.document.body;
let sections = context.document.sections;
context.load(sections, 'body/style');
body.clear();
return context.sync()
.then(() => {
sections.items[0].getHeader(Word.HeaderFooterType.primary).clear();
sections.items[0].getFooter(Word.HeaderFooterType.primary).clear();
return context.sync()
.then(() => {
body.insertFileFromBase64(templateDoc.base64String, Word.InsertLocation.end);
this.appService.setTemplateSelected(template);
return context.sync()
.then(() => {
this.go('/customers');
this.scope.$apply();
}, ((result: OfficeErrorMessage) => {
this.setErrorState(result);
}));
}, ((result: OfficeErrorMessage) => {
this.setErrorState(result);
}));
}, ((result: OfficeErrorMessage) => {
this.setErrorState(result);
}));
});
}, ((result: ng.IHttpPromiseCallbackArg<ErrorMessage>) => {
this.errorMessage = result.data.exceptionMessage;
this.showError = true;
this.scope.$apply();
}));
}
***编辑 我看到这是一个新版本: https://github.com/OfficeDev/office-js-docs/blob/WordJs_1.3_Openspec/word/resources/application.md
和我做的有什么区别?
【问题讨论】:
-
如果切换到 Word.InsertLocation.replace,页眉/页脚是否正确插入?我怀疑存在现有内容时 Word 没有插入页眉/页脚。
-
如果我的问题不清楚,我很抱歉,但我在文档中插入了一个 docx 文件。源文件第一页没有页眉,但插入后,第一页有页眉。所以它与来源不一样。我确实在清除标题时遇到了一些问题,但在我的情况下,这些问题现在有一个解决方法。