Skip to content

Commit 57ccddc

Browse files
AlexJerabekCopilot
andauthored
[all hosts] (Common APIs) Add an Office.Slice sample (#2437)
* Add an Office.Slice sample * Refactor recursion and add process functionality * Update docs/code-snippets/office-snippets.yaml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update docs/code-snippets/office-snippets.yaml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 03bb730 commit 57ccddc

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

docs/code-snippets/office-snippets.yaml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4353,6 +4353,81 @@ Office.Settings#saveAsync:member(2):
43534353
function write(message){
43544354
document.getElementById('message').innerText += message;
43554355
}
4356+
Office.Slice:interface:
4357+
- |-
4358+
// This example demonstrates how to read file slices using the Office.Slice interface.
4359+
// Each slice represents a portion of the document file and includes the data,
4360+
// index, and size properties.
4361+
Office.context.document.getFileAsync(Office.FileType.Compressed, { sliceSize: 65536 },
4362+
function (result) {
4363+
if (result.status === Office.AsyncResultStatus.Succeeded) {
4364+
const file = result.value;
4365+
const sliceCount = file.sliceCount;
4366+
let slicesReceived = 0;
4367+
let fileData = [];
4368+
4369+
console.log(`File sliced into ${sliceCount} parts.`);
4370+
4371+
// Get the first slice.
4372+
getSliceAsync(file, 0, sliceCount);
4373+
4374+
/**
4375+
* Recursively retrieves slices from the file.
4376+
* @param file - The Office.File object to retrieve slices from.
4377+
* @param sliceIndex - The zero-based index of the slice to retrieve.
4378+
* @param totalSlices - The total number of slices in the file.
4379+
*/
4380+
function getSliceAsync(file, sliceIndex, totalSlices) {
4381+
file.getSliceAsync(sliceIndex, function (sliceResult) {
4382+
if (sliceResult.status === Office.AsyncResultStatus.Succeeded) {
4383+
const slice: Office.Slice = sliceResult.value;
4384+
4385+
// Access the properties of the Slice.
4386+
console.log(`Processing slice ${slice.index + 1} of ${totalSlices}`);
4387+
console.log(`Slice size: ${slice.size} bytes`);
4388+
4389+
// Store the slice data.
4390+
fileData[slice.index] = slice.data;
4391+
slicesReceived++;
4392+
4393+
// Check if we've received all slices.
4394+
if (slicesReceived === totalSlices) {
4395+
file.closeAsync();
4396+
console.log("All slices received. File data complete.");
4397+
4398+
// Process the complete file data.
4399+
processFileData(fileData);
4400+
} else {
4401+
// Get the next slice.
4402+
getSliceAsync(file, sliceIndex + 1, totalSlices);
4403+
}
4404+
} else {
4405+
file.closeAsync();
4406+
console.error(`Error getting slice: ${sliceResult.error.message}`);
4407+
}
4408+
});
4409+
}
4410+
4411+
/**
4412+
* Processes the complete file data by combining all slices.
4413+
* @param data - An array of slice data arrays to combine.
4414+
*/
4415+
function processFileData(data) {
4416+
// Combine all slice data into a complete file.
4417+
let combinedData = [];
4418+
for (let i = 0; i < data.length; i++) {
4419+
combinedData.push(...data[i]);
4420+
}
4421+
4422+
console.log(`File processing complete. Total bytes: ${combinedData.length}`);
4423+
// At this point, combinedData contains the complete file content.
4424+
// You can now save it, send it to a server, or process it further.
4425+
}
4426+
} else {
4427+
console.error(`Error getting file: ${result.error.message}`);
4428+
}
4429+
}
4430+
);
43564431
Office.StartupBehavior:enum:
43574432
- |-
43584433
// Configure your add-in to load and start running when the document is opened.

0 commit comments

Comments
 (0)