页眉和页脚在单词javascript api中没有约束力

时间:2017-07-28 11:54:11

标签: ms-word office-js

我正在为一个或我们的应用程序开发一个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));
            }
        });
}

1 个答案:

答案 0 :(得分:2)

我不确定你对这个问题的“绑定”是什么意思(因为“绑定”是Office.js中一个非常具体的概念)。我猜你要做的是通过range.insertFileFromBase64插入一个包含页眉/页脚的文档,你在插入后没有看到页眉和页脚,如果这是一个设计问题,原因,我们不想替换当前文档的页眉/页脚。该方法的目标是重用文档块,而不是替换整个文档。

如果您需要更改标题,则需要手动更改标题。

您可以浏览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...
    }