我正在为一个或我们的应用程序开发 Word 插件。使用此 Word 插件,将保存的 Word 模板从我们的应用程序发送到 Word。
如果我使用包含页眉和页脚的模板,这些内容不会显示在 Word 文档中。
代码如下:
function setDocumentDataBase64(data) {
Word.run(function (context) {
// Create a proxy object for the document body.
var body = context.document.body;
//cleaning old context
//body.clear();
body.insertFileFromBase64(data, Word.InsertLocation.replace);
return context.sync().then(function () {
alert.success("Document inserted");
});
})
.catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
if (error instanceof OfficeExtension.Error) {
console.log('Debug info: ' + JSON.stringify(error.debugInfo));
}
});
}
最佳答案
我不确定您在这个问题上所说的“绑定(bind)”是什么意思(因为“绑定(bind)”是 Office.js 中一个非常具体的概念)。我猜测您想要做的是通过 range.insertFileFromBase64 插入包含页眉/页脚的文档,并且在插入后您看不到页眉和页脚,如果是这种情况,那么它是设计问题,原因是,我们不想替换当前文档的页眉/页脚。该方法的目标是重用文档 block ,而不是替换整个文档。
如果您需要更改 header ,则需要手动执行此操作。
您可以探索预览版的 createDocument API(实际上会打开一个新文档窗口),这可能正是您所需要的。
希望这有帮助。 谢谢! 胡安。
这是获取当前文档的base64的示例代码:
function getFile(){
Office.context.document.getFileAsync(Office.FileType.Compressed, { sliceSize: 4194304 /*64 KB*/ },
function (result) {
if (result.status == "succeeded") {
// If the getFileAsync call succeeded, then
// result.value will return a valid File Object.
var myFile = result.value;
var sliceCount = myFile.sliceCount;
var slicesReceived = 0, gotAllSlices = true, docdataSlices = [];
console.log("File size:" + myFile.size + " #Slices: " + sliceCount);
// Get the file slices.
getSliceAsync(myFile, 0, sliceCount, gotAllSlices, docdataSlices, slicesReceived);
}
else {
app.showNotification("Error:", result.error.message);
}
});
}
function getSliceAsync(file, nextSlice, sliceCount, gotAllSlices, docdataSlices, slicesReceived) {
file.getSliceAsync(nextSlice, function (sliceResult) {
if (sliceResult.status == "succeeded") {
if (!gotAllSlices) { // Failed to get all slices, no need to continue.
return;
}
// Got one slice, store it in a temporary array.
// (Or you can do something else, such as
// send it to a third-party server.)
docdataSlices[sliceResult.value.index] = sliceResult.value.data;
if (++slicesReceived == sliceCount) {
// All slices have been received.
file.closeAsync();
onGotAllSlices(docdataSlices);
}
else {
getSliceAsync(file, ++nextSlice, sliceCount, gotAllSlices, docdataSlices, slicesReceived);
}
}
else {
gotAllSlices = false;
file.closeAsync();
console.log("getSliceAsync Error:", sliceResult.error.message);
}
});
}
function onGotAllSlices(docdataSlices) {
var docdata = [];
for (var i = 0; i < docdataSlices.length; i++) {
docdata = docdata.concat(docdataSlices[i]);
}
var fileContent = new String();
for (var j = 0; j < docdata.length; j++) {
fileContent += String.fromCharCode(docdata[j]);
}
var mybase64 = window.btoa(fileContent);
console.log("here is the base 64", mybase64);
// Now all the file content is stored in 'fileContent' variable,
// you can do something with it, such as print, fax...
}
关于ms-word - 页眉和页脚未在 Word javascript api 中绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45372866/