Header 插入文档时未显示
Header not showing up when I'm inserting a document
我正在开发一个 Word 插件,用户可以在其中 select 从 SharePoint 加载的一些预定义模板 (docx) 文档。通过向导,用户可以在文档中设置内容控件。迄今为止非常好的体验。
但是,我在使用 headers 加载 docx 文件时遇到问题。
我正在使用此函数加载 docx 文件:(下面的完整代码)
body.insertFileFromBase64(templateDoc.base64String, Word.InsertLocation.end);
可行,但有时源文档的 headers 不存在。或者其他时候它们在所有页面上,而第一页应该不同。
问题:
插入带有 headers 和页脚的文档是否可行,我做错了什么吗?
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
和我做的有什么区别?
这是一种设计行为,当您使用 insertFileFromBase64 方法插入文件时,我们不会替换文档的 header/footers 或 customXMLParts(那里可能已经有 headers 和页脚, 与 XMLParts 相同)。
因此,如果您需要更新 headers 和页脚,则必须在插入文件后进行更新。 (使用 api 您可以为每个部分插入 word 支持的 3 种类型 headers,第一页,偶数页,奇数页)
希望这对您有所帮助。
谢谢!
娟。
我正在开发一个 Word 插件,用户可以在其中 select 从 SharePoint 加载的一些预定义模板 (docx) 文档。通过向导,用户可以在文档中设置内容控件。迄今为止非常好的体验。
但是,我在使用 headers 加载 docx 文件时遇到问题。
我正在使用此函数加载 docx 文件:(下面的完整代码)
body.insertFileFromBase64(templateDoc.base64String, Word.InsertLocation.end);
可行,但有时源文档的 headers 不存在。或者其他时候它们在所有页面上,而第一页应该不同。
问题: 插入带有 headers 和页脚的文档是否可行,我做错了什么吗?
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
和我做的有什么区别?
这是一种设计行为,当您使用 insertFileFromBase64 方法插入文件时,我们不会替换文档的 header/footers 或 customXMLParts(那里可能已经有 headers 和页脚, 与 XMLParts 相同)。
因此,如果您需要更新 headers 和页脚,则必须在插入文件后进行更新。 (使用 api 您可以为每个部分插入 word 支持的 3 种类型 headers,第一页,偶数页,奇数页)
希望这对您有所帮助。 谢谢! 娟。