Office.Slice interface

表示文档文件的切片。 使用 File.getSliceAsync 方法访问 Slice 对象。

注解

示例

// This example demonstrates how to read file slices using the Office.Slice interface.
// Each slice represents a portion of the document file and includes the data, 
// index, and size properties.
Office.context.document.getFileAsync(Office.FileType.Compressed, { sliceSize: 65536 }, 
    function (result) {
        if (result.status === Office.AsyncResultStatus.Succeeded) {
            const file = result.value;
            const sliceCount = file.sliceCount;
            let slicesReceived = 0;
            let fileData = [];

            console.log(`File sliced into ${sliceCount} parts.`);

            // Get the first slice.
            getSliceAsync(file, 0, sliceCount);

            /**
             * Recursively retrieves slices from the file.
             * @param file - The Office.File object to retrieve slices from.
             * @param sliceIndex - The zero-based index of the slice to retrieve.
             * @param totalSlices - The total number of slices in the file.
             */
            function getSliceAsync(file, sliceIndex, totalSlices) {
                file.getSliceAsync(sliceIndex, function (sliceResult) {
                    if (sliceResult.status === Office.AsyncResultStatus.Succeeded) {
                        const slice: Office.Slice = sliceResult.value;
                        
                        // Access the properties of the Slice.
                        console.log(`Processing slice ${slice.index + 1} of ${totalSlices}`);
                        console.log(`Slice size: ${slice.size} bytes`);
                        
                        // Store the slice data.
                        fileData[slice.index] = slice.data;
                        slicesReceived++;

                        // Check if we've received all slices.
                        if (slicesReceived === totalSlices) {
                            file.closeAsync();
                            console.log("All slices received. File data complete.");
                            
                            // Process the complete file data.
                            processFileData(fileData);
                        } else {
                            // Get the next slice.
                            getSliceAsync(file, sliceIndex + 1, totalSlices);
                        }
                    } else {
                        file.closeAsync();
                        console.error(`Error getting slice: ${sliceResult.error.message}`);
                    }
                });
            }

            /**
             * Processes the complete file data by combining all slices.
             * @param data - An array of slice data arrays to combine.
             */
            function processFileData(data) {
                // Combine all slice data into a complete file.
                let combinedData = [];
                for (let i = 0; i < data.length; i++) {
                    combinedData.push(...data[i]);
                }
                
                console.log(`File processing complete. Total bytes: ${combinedData.length}`);
                // At this point, combinedData contains the complete file content.
                // You can now save it, send it to a server, or process it further.
            }
        } else {
            console.error(`Error getting file: ${result.error.message}`);
        }
    }
);

属性

data

获取由调用 Document.getFileAsync 方法的 参数指定的 fileTypeOffice.FileType.Compressed 格式的文件切片Office.FileType.Text的原始数据。

index

获取文件切片的从零开始的索引。

size

获取以字节为单位的切片大小。

属性详细信息

data

获取由调用 Document.getFileAsync 方法的 参数指定的 fileTypeOffice.FileType.Compressed 格式的文件切片Office.FileType.Text的原始数据。

data: any;

属性值

any

注解

采用“压缩”格式的文件将返回一个字节数组,如果需要,该数组可以转换为 Base64 编码的字符串。

index

获取文件切片的从零开始的索引。

index: number;

属性值

number

size

获取以字节为单位的切片大小。

size: number;

属性值

number