JavaScriptを通じてC++でExcelファイルの印刷を阻止する方法

可能な使用シナリオ

日常の仕事の中で、重要な情報を含むExcelファイルがあります。内部データの拡散を防ぐために、会社は印刷を許可しません。このドキュメントは、他者がExcelファイルを印刷できないようにする方法を説明します。

MS-Excelでファイルの印刷を防ぐ方法

次のVBAコードを適用して、特定のファイルの印刷を防ぐことができます。

  1. 印刷を許可しないブックを開きます。
  2. Excelリボンの「開発」タブを選択し、「コントロール」セクションの「コードの表示」ボタンをクリックします。あるいは、ALT + F11キーを押してMicrosoft Visual Basic for Applicationsウィンドウを開きます。


  3. 次に、左側のProject ExplorerでThisWorkbookをダブルクリックしてモジュールを開き、VBAコードを追加します。


  4. その後、コードを保存して閉じ、ブックに戻ります。次にサンプルファイルを印刷しようとすると、印刷が許可されず、次の警告ボックスが表示されます。


Aspose.Cells for JavaScriptを使用してExcelファイルの印刷を防止する方法

次のサンプルコードは、Excelファイルの印刷を防ぐ方法を示しています。

  1. サンプルファイルをロードする。
  2. WorkbookのVbaProjectプロパティからVbaModuleCollectionオブジェクトを取得します。
  3. “ThisWorkbook"名を通じてVbaModuleオブジェクトを取得します。
  4. VbaModuleのcodesプロパティを設定します。
  5. サンプルファイルをxlsm形式で保存します。
<!DOCTYPE html>
<html>
    <head>
        <title>Aspose.Cells Example</title>
    </head>
    <body>
        <h1>Update VBA Module Example</h1>
        <input type="file" id="fileInput" accept=".xls,.xlsx,.xlsm" />
        <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, SaveFormat } = 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));

            // Accessing VBA project and its modules
            const modules = workbook.vbaProject.modules;
            const module = modules.get("ThisWorkbook");

            // Setting module codes (converted from setCodes -> codes assignment)
            module.codes = "Private Sub Workbook_BeforePrint(Cancel As Boolean)\r\n  Cancel = True\r\n  MsgBox \"Refusing to print in paperless office\"\r\nEnd Sub\r\n";

            // Saving the modified workbook as macro-enabled workbook
            const outputData = workbook.save(SaveFormat.Xlsm);
            const blob = new Blob([outputData]);
            const downloadLink = document.getElementById('downloadLink');
            downloadLink.href = URL.createObjectURL(blob);
            downloadLink.download = 'out.xlsm';
            downloadLink.style.display = 'block';
            downloadLink.textContent = 'Download Modified Excel File';

            document.getElementById('result').innerHTML = '<p style="color: green;">VBA module updated successfully! Click the download link to get the modified file.</p>';
        });
    </script>
</html>