次の方法で共有


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

Document.getFileAsync メソッドの呼び出しのfileType パラメーターで指定されたOffice.FileType.Text形式またはOffice.FileType.Compressed形式のファイル スライスの生データを取得します。

index

ファイル スライスの 0 から始まるインデックスを取得します。

size

スライスのサイズをバイト単位で取得します。

プロパティの詳細

data

Document.getFileAsync メソッドの呼び出しのfileType パラメーターで指定されたOffice.FileType.Text形式またはOffice.FileType.Compressed形式のファイル スライスの生データを取得します。

data: any;

プロパティ値

any

注釈

"圧縮" 形式のファイルは、必要に応じて Base64 でエンコードされた文字列に変換できるバイト配列を返します。

index

ファイル スライスの 0 から始まるインデックスを取得します。

index: number;

プロパティ値

number

size

スライスのサイズをバイト単位で取得します。

size: number;

プロパティ値

number