Поделиться через


Office.Slice interface

Представляет срез файла документа. Доступ к объекту Slice осуществляется с помощью File.getSliceAsync метода .

Комментарии

Примеры

// 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

Возвращает необработанные данные среза файла в Office.FileType.Text формате или Office.FileType.Compressed в соответствии с параметром fileType вызова Document.getFileAsync метода .

index

Возвращает отсчитываемый от нуля индекс среза файла.

size

Получает размер среза в байтах.

Сведения о свойстве

data

Возвращает необработанные данные среза файла в Office.FileType.Text формате или Office.FileType.Compressed в соответствии с параметром fileType вызова Document.getFileAsync метода .

data: any;

Значение свойства

any

Комментарии

Файлы в сжатом формате возвращают массив байтов, который при необходимости можно преобразовать в строку в кодировке Base64.

index

Возвращает отсчитываемый от нуля индекс среза файла.

index: number;

Значение свойства

number

size

Получает размер среза в байтах.

size: number;

Значение свойства

number