ワークシートの保護と保護解除
ワークシートを保護
ワークシートが保護されている場合、ユーザーが行うことができる操作は制限されます。たとえば、データの入力、行または列の挿入や削除などはできません。Microsoft Excelの一般的な保護オプションは次のとおりです:
- コンテンツ
- オブジェクト
- シナリオ
保護されたワークシートは、機密データを非表示または保護しないため、ファイルの暗号化とは異なります。一般的に、ワークシートの保護はプレゼンテーションの目的に適しています。これにより、エンドユーザーがワークシート内のデータ、コンテンツ、および書式を変更できなくなります。
保護の追加または削除
Aspose.Cellsは、Microsoft Excelファイルを表すクラスであるWorkbookクラスを提供します。Workbookクラスには、Excelファイルの各ワークシートにアクセスできるWorksheetCollectionが含まれています。ワークシートはWorksheetクラスによって表されます。
Worksheetクラスには、ワークシートに保護を適用するために使用されるProtectメソッドが提供されています。Protectメソッドは次のパラメータを受け入れます。
- 保護の種類、ワークシートに適用する保護の種類。保護の種類はProtectionType列挙型のヘルプを使用して適用されます。
- 新しいパスワード、ワークシートを保護するために使用する新しいパスワード。
- 古いパスワード、既にワークシートがパスワードで保護されている場合は、古いパスワードを渡します。ワークシートがまだ保護されていない場合は、単にnullを渡します。
ProtectionType列挙型には、次の事前定義された保護タイプが含まれています。
保護タイプ | 説明 |
---|---|
ALL | ユーザーはこのワークシート上で何も変更できません |
CONTENTS | ユーザーはこのワークシートにデータを入力できません |
OBJECTS | ユーザーは描画オブジェクトを変更できません |
SCENARIOS | ユーザーは保存されたシナリオを変更できません |
STRUCTURE | ユーザーは保存された構造を変更できません |
WINDOWS | ユーザーは保存されたウィンドウを変更できません |
NONE | 保護なし |
以下の例は、ワークシートにパスワードを設定して保護する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ProtectingWorksheet.class) + "worksheets/"; | |
// Instantiating a Excel object by excel file path | |
Workbook excel = new Workbook(dataDir + "book1.xls"); | |
// Accessing the first worksheet in the Excel file | |
WorksheetCollection worksheets = excel.getWorksheets(); | |
Worksheet worksheet = worksheets.get(0); | |
Protection protection = worksheet.getProtection(); | |
// The following 3 methods are only for Excel 2000 and earlier formats | |
protection.setAllowEditingContent(false); | |
protection.setAllowEditingObject(false); | |
protection.setAllowEditingScenario(false); | |
// Protects the first worksheet with a password "1234" | |
protection.setPassword("1234"); | |
// Saving the modified Excel file in default format | |
excel.save(dataDir + "ProtectingWorksheet_out.xls"); | |
// Print Message | |
System.out.println("Sheet protected successfully."); |
上記のコードを使用してワークシートを保護した後、ワークシートの保護を確認するには、ファイルを開いてください。ファイルを開いてワークシートにデータを追加しようとすると、次のダイアログボックスが表示されます。
ユーザーがワークシートを変更できないことを警告するダイアログ
ワークシートで作業するには、以下に示すようにProtectionからシートの保護解除を選択してワークシートの保護を解除してください。
シートの保護解除メニューアイテムを選択する
パスワードを求めるダイアログが開きます。
ワークシートの保護を解除するためのパスワード入力
一部のセルを保護する
ワークシートで特定のセルのみをロックする必要がある場合があります。ワークシートで特定のセルのみをロックする場合は、ワークシートの他のすべてのセルのロックを解除する必要があります。ワークシートのすべてのセルは既にロックされるように初期化されています。これを確認するには、MS Excelで任意のExcelファイルを開き、書式 | セル…をクリックしてセルの書式ダイアログボックスを表示し、その後、保護タブをクリックして、デフォルトでチェックされた"ロック"と表示されるチェックボックスを確認できます。
次に、このタスクを実装するための2つのアプローチを示します。
方法1:
次のポイントは、MS Excelを使用していくつかのセルをロックする方法を説明しています。この方法は、Microsoft Office Excel 97、2000、2002、2003およびそれ以降のバージョンに適用されます。
-
シート全体を選択するには、Select Allボタンをクリックします(行1の行番号の直上および列Aの左にある灰色の四角形)。
-
フォーマットメニューからセルをクリックし、「保護」タブをクリックし、その後「ロックされている」チェックボックスをクリアします。
これにより、ワークシートのすべてのセルがロック解除されます
- ロックしたいセルを選択し、ステップ2を繰り返しますが、この時に「ロックされている」チェックボックスを選択します。
- ツールメニューで保護を選択し、シートの保護をクリックし、その後OKをクリックします。
メソッド2:
この方法では、Aspose.Cells APIのみを使用してタスクを実行します。
次の例は、ワークシート内の数個のセルを保護する方法を示しています。ますます、ワークシートのすべてのセルをアンロックし、それから(A1、B1、C1)の3つのセルをロックします。最後に、ワークシートを保護します。行/列にはStyle APIがあり、さらにセルロックを設定するメソッドが含まれています。このメソッドを使用して行/列をロックまたはアンロックできます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ProtectingSpecificCellsinaWorksheet.class) + "worksheets/"; | |
// Create a new workbook. | |
Workbook wb = new Workbook(); | |
// Create a worksheet object and obtain the first sheet. | |
Worksheet sheet = wb.getWorksheets().get(0); | |
// Define the style object. | |
Style style; | |
// Define the styleflag object. | |
StyleFlag flag; | |
flag = new StyleFlag(); | |
flag.setLocked(true); | |
// Loop through all the columns in the worksheet and unlock them. | |
for (int i = 0; i <= 255; i++) { | |
style = sheet.getCells().getColumns().get(i).getStyle(); | |
style.setLocked(false); | |
sheet.getCells().getColumns().get(i).applyStyle(style, flag); | |
} | |
// Lock the three cells...i.e. A1, B1, C1. | |
style = sheet.getCells().get("A1").getStyle(); | |
style.setLocked(true); | |
sheet.getCells().get("A1").setStyle(style); | |
style = sheet.getCells().get("B1").getStyle(); | |
style.setLocked(true); | |
sheet.getCells().get("B1").setStyle(style); | |
style = sheet.getCells().get("C1").getStyle(); | |
style.setLocked(true); | |
sheet.getCells().get("C1").setStyle(style); | |
// Save the excel file. | |
wb.save(dataDir + "PSpecificCellsinaWorksheet_out.xls", FileFormatType.EXCEL_97_TO_2003); | |
// Print Message | |
System.out.println("Cells protected successfully."); |
ワークシート内の行を保護する
Aspose.Cellsを使用すると、ワークシート内の任意の行を簡単にロックできます。ここでは、ワークシート内の特定の行にStyleを適用するためのapplyStyle()メソッドを{1}クラスのapplyStyle()メソッドを使用できます。このメソッドには2つの引数があります。Styleオブジェクトと適用された書式に関連するすべてのメンバーを持つStyleFlag構造体です。
次の例は、ワークシート内の行をロックする方法を示しています。ますます、ワークシートのすべてのセルをアンロックし、それから最初の行をロックします。最後に、ワークシートを保護します。行/列にはStyle APIがあり、セルをロックまたはアンロックするメソッドを含んでいます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ProtectRowWorksheet.class) + "worksheets/"; | |
// Create a new workbook. | |
Workbook wb = new Workbook(); | |
// Create a worksheet object and obtain the first sheet. | |
Worksheet sheet = wb.getWorksheets().get(0); | |
// Define the style object. | |
Style style; | |
// Define the styleflag object. | |
StyleFlag flag; | |
// Loop through all the columns in the worksheet and unlock them. | |
for (int i = 0; i <= 255; i++) { | |
style = sheet.getCells().getRows().get(i).getStyle(); | |
style.setLocked(false); | |
flag = new StyleFlag(); | |
flag.setLocked(true); | |
sheet.getCells().getRows().get(i).applyStyle(style, flag); | |
} | |
// Get the first Roww style. | |
style = sheet.getCells().getRows().get(1).getStyle(); | |
// Lock it. | |
style.setLocked(true); | |
// Instantiate the flag. | |
flag = new StyleFlag(); | |
// Set the lock setting. | |
flag.setLocked(true); | |
// Apply the style to the first row. | |
sheet.getCells().getRows().get(1).applyStyle(style, flag); | |
sheet.protect(ProtectionType.ALL); | |
// Save the excel file. | |
wb.save(dataDir + "ProtectRowWorksheet_out.xls", FileFormatType.EXCEL_97_TO_2003); | |
// Print Message | |
System.out.println("Row protected successfully."); |
ワークシート内の列を保護する
Aspose.Cellsを使用すると、ワークシート内の任意の列を簡単にロックできます。ここでは、ワークシート内の特定の列にStyleを適用するためのapplyStyle()メソッドを{1}クラスのapplyStyle()メソッドを使用できます。このメソッドには2つの引数があります。Styleオブジェクトと適用された書式に関連するすべてのメンバーを持つStyleFlag構造体です。
次の例は、ワークシート内の列をロックする方法を示しています。ますます、ワークシートのすべてのセルをアンロックし、それから最初の列をロックします。最後に、ワークシートを保護します。行/列にはStyle APIがあり、セルをロックまたはアンロックするメソッドを含んでいます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ProtectColumnWorksheet.class) + "worksheets/"; | |
// Create a new workbook. | |
Workbook wb = new Workbook(); | |
// Create a worksheet object and obtain the first sheet. | |
Worksheet sheet = wb.getWorksheets().get(0); | |
// Define the style object. | |
Style style; | |
// Define the styleflag object. | |
StyleFlag flag; | |
// Loop through all the columns in the worksheet and unlock them. | |
for (int i = 0; i <= 255; i++) { | |
style = sheet.getCells().getColumns().get(i).getStyle(); | |
style.setLocked(false); | |
flag = new StyleFlag(); | |
flag.setLocked(true); | |
sheet.getCells().getColumns().get(i).applyStyle(style, flag); | |
} | |
// Get the first column style. | |
style = sheet.getCells().getColumns().get(0).getStyle(); | |
// Lock it. | |
style.setLocked(true); | |
// Instantiate the flag. | |
flag = new StyleFlag(); | |
// Set the lock setting. | |
flag.setLocked(true); | |
// Apply the style to the first column. | |
sheet.getCells().getColumns().get(0).applyStyle(style, flag); | |
sheet.protect(ProtectionType.ALL); | |
// Save the excel file. | |
wb.save(dataDir + "PColumnWorksheet_out.xls", FileFormatType.EXCEL_97_TO_2003); | |
// Print Message | |
System.out.println("Column protected successfully."); |
ワークシートの保護を解除する
ワークシートの保護とExcel XP以降の高度な保護設定は、保護したワークシートから保護を解除してファイルを変更できるようにする必要が開発者にある場合に異なるアプローチについて説明しています。これはAspose.Cellsで簡単に実行できます。
Microsoft Excel の使用
ワークシートから保護を解除するには:
ツールメニューから保護を選択し、その後シートの保護を解除を選択します。
シートの保護を解除を選択
保護が解除されます(ワークシートがパスワードで保護されていない場合)。この場合、パスワードを入力するダイアログボックスが表示されます。
ワークシートの保護を解除するためのパスワード入力
Aspose.Cellsの使用
ワークシートをWorksheetクラスのUnprotectメソッドを呼び出すことで保護を解除できます。Unprotectメソッドは2つの方法で使用できます。以下で説明します。
単純に保護されたワークシートの保護解除
単純に保護されたワークシートはパスワードで保護されていないワークシートです。このようなワークシートはパラメータを渡さずに解除メソッドを呼び出すことで保護解除できます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(UnprotectingSimplyProtectedWorksheet.class) + "worksheets/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
Worksheet worksheet = worksheets.get(0); | |
Protection protection = worksheet.getProtection(); | |
// The following 3 methods are only for Excel 2000 and earlier formats | |
protection.setAllowEditingContent(false); | |
protection.setAllowEditingObject(false); | |
protection.setAllowEditingScenario(false); | |
// Unprotecting the worksheet | |
worksheet.unprotect(); | |
// Save the excel file. | |
workbook.save(dataDir + "USPWorksheet_out.xls", FileFormatType.EXCEL_97_TO_2003); | |
// Print Message | |
System.out.println("Worksheet unprotected successfully."); |
パスワードで保護されたワークシートの保護解除
パスワードで保護されたワークシートはパスワードで保護されたワークシートです。このようなワークシートはパラメータとしてパスワードを取るUnprotectメソッドのオーバーロードバージョンを呼び出すことで保護解除できます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(UnprotectProtectSheet.class) + "worksheets/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
Worksheet worksheet = worksheets.get(0); | |
// Unprotecting the worksheet | |
worksheet.unprotect("aspose"); | |
// Save the excel file. | |
workbook.save(dataDir + "UnprotectProtectSheet_out.xls", FileFormatType.EXCEL_97_TO_2003); | |
// Print Message | |
System.out.println("Worksheet unprotected successfully."); |
Excel XP以降の高度な保護設定
ワークシートの保護では、Microsoft Excel 97および2000のワークシートの保護について説明しました。しかし、Excel 2002またはXPのリリース以降、Microsoftは多くの高度な保護設定を追加しました。これらの保護設定により、ユーザーに制限をかけることができます。
- 行または列の削除。
- 内容、オブジェクト、またはシナリオの編集。
- セル、行、または列の書式設定。
- 行、列、またはハイパーリンクの挿入。
- ロックされたセルまたはロックされていないセルの選択。
- ピボットテーブルなどの使用。
Aspose.Cellsは、Excel XPおよびそれ以降のバージョンで提供されるすべての高度な保護設定をサポートしています。
Excel XPおよびそれ以降のバージョンを使用した高度な保護設定
Excel XPで利用可能な保護設定を表示するには:
-
ツール メニューから 保護 を選択し、続いて シートを保護 を選択します。 ダイアログが表示されます。
Excel XPで保護オプションを表示するためのダイアログ
- ワークシートの機能の許可または制限、またはパスワードを適用します。
Aspose.Cellsを使用した高度な保護設定
Aspose.Cellsはすべての高度な保護設定をサポートしています。
Aspose.Cellsは、Microsoft Excelファイルを表すWorkbookクラスを提供します。 Workbookクラスには、Excelファイル内の各ワークシートにアクセスを可能にするWorksheetCollectionコレクションが含まれています。ワークシートはWorksheet クラスで表されます。
Worksheetクラスには、これらの高度な保護設定を適用するために使用されるProtectionプロパティがあります。 Protectionプロパティは、実際にはProtectionクラスのオブジェクトで、制限の無効化または有効化のための複数のブールプロパティをカプセル化しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(AdvancedProtectionSettingsUsingAsposeCells.class) + "worksheets/"; | |
// Instantiating a Workbook object by excel file path | |
Workbook excel = new Workbook(dataDir + "book1.xlsx"); | |
WorksheetCollection worksheets = excel.getWorksheets(); | |
Worksheet worksheet = worksheets.get(0); | |
Protection protection = worksheet.getProtection(); | |
// Restricting users to delete columns of the worksheet | |
protection.setAllowDeletingColumn(false); | |
// Restricting users to delete row of the worksheet | |
protection.setAllowDeletingRow(false); | |
// Restricting users to edit contents of the worksheet | |
protection.setAllowEditingContent(false); | |
// Restricting users to edit objects of the worksheet | |
protection.setAllowEditingObject(false); | |
// Restricting users to edit scenarios of the worksheet | |
protection.setAllowEditingScenario(false); | |
// Restricting users to filter | |
protection.setAllowFiltering(false); | |
// Allowing users to format cells of the worksheet | |
protection.setAllowFormattingCell(true); | |
// Allowing users to format rows of the worksheet | |
protection.setAllowFormattingRow(true); | |
// Allowing users to insert columns in the worksheet | |
protection.setAllowInsertingColumn(true); | |
// Allowing users to insert hyperlinks in the worksheet | |
protection.setAllowInsertingHyperlink(true); | |
// Allowing users to insert rows in the worksheet | |
protection.setAllowInsertingRow(true); | |
// Allowing users to select locked cells of the worksheet | |
protection.setAllowSelectingLockedCell(true); | |
// Allowing users to select unlocked cells of the worksheet | |
protection.setAllowSelectingUnlockedCell(true); | |
// Allowing users to sort | |
protection.setAllowSorting(true); | |
// Allowing users to use pivot tables in the worksheet | |
protection.setAllowUsingPivotTable(true); | |
// Saving the modified Excel file Excel XP format | |
excel.save(dataDir + "APSettingsUsingAsposeCells_out.xls", FileFormatType.EXCEL_97_TO_2003); | |
// Print Message | |
System.out.println("Worksheet protected successfully."); |
以下は小さなサンプルアプリケーションです。それはExcelファイルを開いて、Excel XPおよびそれ以降のバージョンでサポートされる高度な保護設定のほとんどを使用します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(AdvancedProtection.class) + "worksheets/"; | |
// Instantiating a Workbook object by excel file path | |
Workbook excel = new Workbook(dataDir + "book1.xls"); | |
WorksheetCollection worksheets = excel.getWorksheets(); | |
Worksheet worksheet = worksheets.get(0); | |
Protection protection = worksheet.getProtection(); | |
// Restricting users to delete columns of the worksheet | |
protection.setAllowDeletingColumn(false); | |
// Restricting users to delete row of the worksheet | |
protection.setAllowDeletingRow(false); | |
// Restricting users to edit contents of the worksheet | |
protection.setAllowEditingContent(false); | |
// Restricting users to edit objects of the worksheet | |
protection.setAllowEditingObject(false); | |
// Restricting users to edit scenarios of the worksheet | |
protection.setAllowEditingScenario(false); | |
// Restricting users to filter | |
protection.setAllowFiltering(false); | |
// Allowing users to format cells of the worksheet | |
protection.setAllowFormattingCell(true); | |
// Allowing users to format rows of the worksheet | |
protection.setAllowFormattingRow(true); | |
// Allowing users to insert columns in the worksheet | |
protection.setAllowInsertingColumn(true); | |
// Allowing users to insert hyperlinks in the worksheet | |
protection.setAllowInsertingHyperlink(true); | |
// Allowing users to insert rows in the worksheet | |
protection.setAllowInsertingRow(true); | |
// Allowing users to select locked cells of the worksheet | |
protection.setAllowSelectingLockedCell(true); | |
// Allowing users to select unlocked cells of the worksheet | |
protection.setAllowSelectingUnlockedCell(true); | |
// Allowing users to sort | |
protection.setAllowSorting(true); | |
// Allowing users to use pivot tables in the worksheet | |
protection.setAllowUsingPivotTable(true); | |
// Saving the modified Excel file Excel XP format | |
excel.save(dataDir + "AdvancedProtection_out.xls", FileFormatType.EXCEL_97_TO_2003); | |
// Print Message | |
System.out.println("Worksheet protected successfully."); |
セルロックの問題
ワークシートが保護されている場合でもセルを編集できるようにするには、保護設定が適用される前にセルをロックする必要があります。Microsoft Excel XPでは、次のダイアログを使用してセルをロックできます:
Excel XPでセルをロックするためのダイアログ
Aspose.CellsのAPIを使用してセルをロックすることも可能です。各セルは、さらにsetLockedメソッドを含むStyle APIを持っており、これを使用してセルをロックまたはロック解除できます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(LockCell.class) + "worksheets/"; | |
// Instantiating a Workbook object by excel file path | |
Workbook excel = new Workbook(dataDir + "Book1.xlsx"); | |
WorksheetCollection worksheets = excel.getWorksheets(); | |
Worksheet worksheet = worksheets.get(0); | |
worksheet.getCells().get("A1").getStyle().setLocked(true); | |
// Saving the modified Excel file Excel XP format | |
excel.save(dataDir + "LockCell_out.xls", FileFormatType.EXCEL_97_TO_2003); | |
// Print Message | |
System.out.println("Cell Locked successfully."); |