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}`);
}
});
});
}