Officejs:最大范围呼叫堆栈尺寸错误

发布于 2025-02-03 14:09:37 字数 2567 浏览 1 评论 0原文

我在单词加载项中使用officejs,并看到有关错误显示在控制台中的一些奇怪行为,该错误显示了日志“最大范围调用堆栈尺寸错误”。

下面的代码几乎适用于所有文档。但是对于特定文档,看起来像file.getSliceasync失败并导致错误。但是,在测试时,我们的一些开发人员可以重现错误,有些则不能。单个开发人员的时间有100%的时间,或者时间为0%。

我们尝试清除卡车,但没有成功。在这里失败的文档中没有任何内容。

似乎这是OfficeJS库中的错误,但我不确定是否有任何解决方法。

有什么想法吗?

下面的代码:

static async getFileContent(): Promise<number[]> {
    console.log('Starting file content retrieval');
    return new Promise((resolve, reject) => {
      return Office.context.document.getFileAsync(
        Office.FileType.Compressed,
        async (result) => {
          if (result.status === Office.AsyncResultStatus.Succeeded) {
            const file: Office.File = result.value;
            console.log('Slice count: ' + file.sliceCount);

            // Get all file slices
            const fileSlicePromises: Promise<number[]>[] = [
              ...Array(file.sliceCount).keys(),
            ].map((counter) => WordDocumentService.getFileSlice(file, counter));
            console.log('fileSlices length: ' + fileSlicePromises.length);
            // Combine file slices into a single file and then close the file
            const dataSlices: number[][] = await Promise.all(fileSlicePromises).finally(
              () => WordDocumentService.closeOfficeFile(file)
            );
            console.log('Concatenating data');
            return resolve([].concat(...dataSlices));
          } else {
            return reject(`Failed to get file content: ${result?.error?.message}`);
          }
        }
      );
    });
  }

  static getFileSlice(file: Office.File, sliceIndex: number): Promise<number[]> {
    console.log('Getting file slice: ' + sliceIndex);
    return new Promise((resolve, reject) => {
      return file.getSliceAsync(sliceIndex, (result) => {
        if (result.status === Office.AsyncResultStatus.Succeeded) {
          return resolve(result.value.data);
        } else {
          return reject(
            `Failed to get file slice ${sliceIndex} from file: ${result?.error?.message}`
          );
        }
      });
    });
  }

  static closeOfficeFile(file: Office.File): Promise<void> {
    console.log('Closing office file');
    return new Promise((resolve, reject) => {
      return file.closeAsync((result) => {
        if (result.status === Office.AsyncResultStatus.Succeeded) {
          return resolve();
        } else {
          return reject(`Failed to close file: ${result?.error?.message}`);
        }
      });
    });
  }

I'm using OfficeJS in a Word add-in and seeing some strange behavior regarding an error that shows up in the console with the log "Maximum Range Call Stack Size Error".

The code below works for almost all documents. But for specific documents, it looks like the file.getSliceAsync fails and causes the error. However, when testing, some of our developers can reproduce the error and some cannot. It either works 100% of the time or 0% of the time for a single developer.

We have tried clearing caches, but without success. There's nothing that sticks out in the documents that fail here.

It seems like this is a bug in the OfficeJS library, but I'm not sure if there is any workaround.

Any ideas?

Code below:

static async getFileContent(): Promise<number[]> {
    console.log('Starting file content retrieval');
    return new Promise((resolve, reject) => {
      return Office.context.document.getFileAsync(
        Office.FileType.Compressed,
        async (result) => {
          if (result.status === Office.AsyncResultStatus.Succeeded) {
            const file: Office.File = result.value;
            console.log('Slice count: ' + file.sliceCount);

            // Get all file slices
            const fileSlicePromises: Promise<number[]>[] = [
              ...Array(file.sliceCount).keys(),
            ].map((counter) => WordDocumentService.getFileSlice(file, counter));
            console.log('fileSlices length: ' + fileSlicePromises.length);
            // Combine file slices into a single file and then close the file
            const dataSlices: number[][] = await Promise.all(fileSlicePromises).finally(
              () => WordDocumentService.closeOfficeFile(file)
            );
            console.log('Concatenating data');
            return resolve([].concat(...dataSlices));
          } else {
            return reject(`Failed to get file content: ${result?.error?.message}`);
          }
        }
      );
    });
  }

  static getFileSlice(file: Office.File, sliceIndex: number): Promise<number[]> {
    console.log('Getting file slice: ' + sliceIndex);
    return new Promise((resolve, reject) => {
      return file.getSliceAsync(sliceIndex, (result) => {
        if (result.status === Office.AsyncResultStatus.Succeeded) {
          return resolve(result.value.data);
        } else {
          return reject(
            `Failed to get file slice ${sliceIndex} from file: ${result?.error?.message}`
          );
        }
      });
    });
  }

  static closeOfficeFile(file: Office.File): Promise<void> {
    console.log('Closing office file');
    return new Promise((resolve, reject) => {
      return file.closeAsync((result) => {
        if (result.status === Office.AsyncResultStatus.Succeeded) {
          return resolve();
        } else {
          return reject(`Failed to close file: ${result?.error?.message}`);
        }
      });
    });
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文