C++経由のJavaScriptでブックをプレビュー

印刷プレビュー

数百ページのExcelファイルをPDFや画像に変換する必要がある場合があります。これらのファイルを処理すると、多大な時間とリソースを消費します。そんな時、ワークブックとワークシートの印刷プレビュー機能が役立ちます。変換前に総ページ数を確認し、変換の可否を判断できます。この記事では、WorkbookPrintingPreviewSheetPrintingPreviewクラスを使って総ページ数を調べる方法に焦点を当てます。

Aspose.Cellsには印刷プレビュー機能があり、そのためにWorkbookPrintingPreviewSheetPrintingPreviewクラスを提供します。全ワークブックの印刷プレビューを作成するには、WorkbookPrintingPreviewクラスのインスタンスを作成し、WorkbookImageOrPrintOptionsオブジェクトを引数に渡します。WorkbookPrintingPreviewクラスには、このプレビューのページ数を返すevaluatedPageCountメソッドがあります。WorkbookPrintingPreviewクラスと同様に、SheetPrintingPreviewクラスは特定のワークシートの印刷プレビューを生成するために使われます。ワークシートのプレビューを作成するには、SheetPrintingPreviewクラスのインスタンスを作成し、WorksheetImageOrPrintOptionsオブジェクトを渡します。SheetPrintingPreviewクラスも、生成されたプレビューのページ数を返すevaluatedPageCountメソッドを提供します。

次のコードスニペットは、サンプルエクセルファイルを使ったWorkbookPrintingPreviewSheetPrintingPreviewクラスの両方の使用例を示しています。

サンプルコード

<!DOCTYPE html>
<html>
    <head>
        <title>Aspose.Cells Example - Printing Preview</title>
    </head>
    <body>
        <h1>Printing Preview Example</h1>
        <input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
        <button id="runExample">Run Example</button>
        <a id="downloadLink" style="display: none;">Download Result</a>
        <div id="result"></div>
    </body>

    <script src="aspose.cells.js.min.js"></script>
    <script type="text/javascript">
        const { Workbook, ImageOrPrintOptions, WorkbookPrintingPreview, SheetPrintingPreview } = AsposeCells;

        AsposeCells.onReady({
            license: "/lic/aspose.cells.enc",
            fontPath: "/fonts/",
            fontList: [
                "arial.ttf",
                "NotoSansSC-Regular.ttf"
            ]
        }).then(() => {
            console.log("Aspose.Cells initialized");
        });

        document.getElementById('runExample').addEventListener('click', async () => {
            const fileInput = document.getElementById('fileInput');
            if (!fileInput.files.length) {
                document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
                return;
            }

            const file = fileInput.files[0];
            const arrayBuffer = await file.arrayBuffer();

            // Instantiating a Workbook object from the uploaded file
            const workbook = new Workbook(new Uint8Array(arrayBuffer));

            // Creating image/print options
            const imgOptions = new ImageOrPrintOptions();

            // Workbook printing preview
            const preview = new WorkbookPrintingPreview(workbook, imgOptions);
            const workbookPageCount = preview.evaluatedPageCount;
            console.log("Workbook page count: " + workbookPageCount);

            // Worksheet printing preview for first worksheet
            const preview2 = new SheetPrintingPreview(workbook.worksheets.get(0), imgOptions);
            const worksheetPageCount = preview2.evaluatedPageCount;
            console.log("Worksheet page count: " + worksheetPageCount);

            document.getElementById('result').innerHTML = `<p>Workbook page count: ${workbookPageCount}</p><p>Worksheet page count: ${worksheetPageCount}</p>`;
        });
    </script>
</html>

上記のコードを実行した結果生成された出力は次のとおりです。

コンソール出力

  
Workbook page count: 1  
Worksheet page count: 1  

高度なトピック