首页 > 解决方案 > Word 桌面上 getFileAsync() 方法的错误

问题描述

我有一个 Word 加载项,它使用 Office Javascript API 中的getFileAsync()获取整个文档,将其转换为哈希,并将其保存到数据库中以供将来比较。

最近遇到一个相关的问题,在桌面平台调用函数会以某种方式修改文档。我已联系 Microsoft 的支持工程师,并将该问题确定为错误。支持工程师通过在 OfficeDev/office-js 存储库中打开一个问题提交了此错误。这是问题的链接:https ://github.com/OfficeDev/office-js/issues/298

在桌面应用程序上调用 getFileAsync 函数后,文档似乎发生了变化,而没有做任何其他事情。这是使用的代码:

verifyDocument(callback){
  let method = 'getDocumentByFile'
  
  var promiseGetFileAsync = new Promise((resolve, reject) => {

      Office.context.document.getFilePropertiesAsync(function (asyncResult) {
      let fileUrl = asyncResult.value.url;
      let fileType = localStorage.getItem("Host");

      if (!fileUrl) {
        reject("Please save the file first")
      } else {

        Office.context.document.getFileAsync('compressed',
          {},
          function (result) {

            if (result.status == Office.AsyncResultStatus.Succeeded) {

              var myFile = result.value;
        
              var state = {
                file: myFile,
                counter: 0,
                sliceCount: myFile.sliceCount
              };
              state.file.getSliceAsync(state.counter, function (result) {
                if (result.status == Office.AsyncResultStatus.Succeeded) {
  
                  var slice = result.value;
                  var data = slice.data;
                  var payload

                  if (data) {

                    payload = {
                      method : method,
                      data: data,
                      fileType: fileType
                    }
                    resolve(payload);
                  }

                  state.counter++;
                  if (state.counter > state.sliceCount) {

                  }
                  else {
                    state.file.closeAsync();
                  }

                }

              });
            }
            else {
              reject(result.error)
            }
          });
      }
    });
  })

  promiseGetFileAsync.then((data: any) => {

    let myHeaders = new Headers();
    myHeaders.append('Authorization', 'Bearer ' + localStorage.getItem('token'));
    let options = new RequestOptions({ headers: myHeaders });
      var file:any = new Blob([new Uint8Array(data.data)], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });
      let formData: FormData = new FormData();
      formData.append('method', data.method);
      formData.append('document', file);
  
      this.http.post('Link to our API receiver', formData, options)
        .map(res => res.json())
        .subscribe(response => {
          callback(response);
        }
      )
  });

此代码获取整个文档并将其作为 blob 发送到我们的 API 接收器以进行比较。

正在发送的文档与数据库上的比较方法是通过 sha256 哈希。API 将接收到的文档转换为 sha256 散列,并将其与数据库中的所有散列进行比较。该代码还用作对数据库中已经存在的哈希的“更新”,使用不同的 API 链接仍将文档转换为 sha256 哈希并将该特定哈希放在数据库中以供将来比较。这可以在 Word Online 上无缝运行;但是在 Word Office 365 上打开文档时,它会失败。调用该函数后,API 无法在数据库中找到文档的哈希值,将返回错误。这是因为该函数向接收者发送了不同的文档。退出 Word 应用程序后,系统会提示我们以下消息:

对话框

我们可以通过此对话框得出结论,文档上有一些更改。同意保存文档并退出桌面应用程序将再次更改文档,因为它已保存。在 Word Online 上打开文档并从那里调用函数将导致在将文档的哈希与数据库中的哈希进行比较时验证失败。

简而言之,getFileAsync 函数在桌面应用程序上的行为与 Word Online 不同,因为它返回的文档内容相同,但元数据不同。

有人知道为什么这只发生在桌面应用程序上而不是 Word Online 上吗?有没有解决这个问题的方法?

更新:

显然,这个问题现在已由分配给它的 PM 关闭,同时建议使用 Word API 中的getOoxml()方法作为解决方法。我尝试使用文档中的代码重做加载项,并发现 bodyOOXML 变量的值在调用函数时也会发生变化。

PM 声明要从 bodyOOXML 中获取文档元素,并将其与数据库中的现有元素进行比较;但是经过手动检查,我发现<w:p w:rsidR="00000000" w:rsidRDefault="******">在每次调用函数后,该元素的 w:rsidRDefault 值总是不同的。这总是会导致比较的负面结果。此外,我认为输出中的文档元素仅从文档中获取文本,不包括其上的图像。这是 bodyOOXML 值中的 w:document 元素:

<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14"><w:body><w:p w:rsidR="00000000" w:rsidRDefault="005D6ED5"><w:r><w:t>Test Word</w:t></w:r><w:r><w:rPr><w:noProof /></w:rPr><w:drawing><wp:inline distT="0" distB="0" distL="0" distR="0" wp14:anchorId="216A5B6F" wp14:editId="50917A19"><wp:extent cx="1096645" cy="1096645" /><wp:effectExtent l="0" t="0" r="8255" b="8255" /><wp:docPr id="1617038684" name="picture" /><wp:cNvGraphicFramePr><a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1" /></wp:cNvGraphicFramePr><a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"><pic:nvPicPr><pic:cNvPr id="0" name="picture" /><pic:cNvPicPr /></pic:nvPicPr><pic:blipFill><a:blip r:embed="rId4"><a:extLst><a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}"><a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0" /></a:ext></a:extLst></a:blip><a:stretch><a:fillRect /></a:stretch></pic:blipFill><pic:spPr><a:xfrm><a:off x="0" y="0" /><a:ext cx="1096645" cy="1096645" /></a:xfrm><a:prstGeom prst="rect"><a:avLst /></a:prstGeom></pic:spPr></pic:pic></a:graphicData></a:graphic></wp:inline></w:drawing></w:r></w:p><w:sectPr w:rsidR="00000000"><w:pgSz w:w="12240" w:h="15840" /><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0" /><w:cols w:space="720" /></w:sectPr></w:body></w:document>

这是用于直接比较的 XML 文件的下载链接

我仍然无法解决这个问题。该建议有一个更大的问题,因为它现在甚至影响除了桌面版本之外的 Word Online。我希望任何人都可以在这方面帮助我。

标签: office365ms-officeoffice-js

解决方案


推荐阅读