ワークシートの保護と保護解除
ワークシートを保護する
ワークシートが保護されている場合、ユーザーが実行できるアクションは制限されます。たとえば、データを入力したり、行や列を挿入または削除したりすることはできません。Microsoft Excel の一般的な保護オプションは次のとおりです。
- コンテンツ
- オブジェクト
- シナリオ
保護されたワークシートは機密データを隠したり保護したりしないため、ファイルの暗号化とは異なります。一般に、ワークシートの保護はプレゼンテーションの目的に適しています。これにより、エンド ユーザーはワークシートのデータ、コンテンツ、および書式を変更できなくなります。
保護の追加または削除
Aspose.Cells はクラスを提供し、ワークブック、Microsoft Excel ファイルを表します。 Workbook クラスには、Excel ファイル内の各ワークシートにアクセスできるようにする WorksheetCollection が含まれています。ワークシートは、ワークシートクラス。
Worksheet クラスは、[守る](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#protect(int)ワークシートに保護を適用するために使用されるメソッド。 Protect メソッドは、次のパラメーターを受け入れます。
- 保護タイプ、ワークシートに適用する保護のタイプ。保護タイプは、保護タイプ列挙。
- 新しいパスワード。ワークシートを保護するために使用される新しいパスワード。
- 古いパスワード、ワークシートがすでにパスワードで保護されている場合の古いパスワード。ワークシートがまだ保護されていない場合は、null を渡します。
ProtectionType 列挙には、次の事前定義された保護の種類が含まれています。
保護の種類 | 説明 |
---|---|
全て | ユーザーはこのワークシートで何も変更できません |
コンテンツ | ユーザーはこのワークシートにデータを入力できません |
オブジェクト | ユーザーは描画オブジェクトを変更できません |
シナリオ | ユーザーは保存されたシナリオを変更できません |
構造 | ユーザーは保存された構造を変更できません |
ウィンドウズ | ユーザーは保存されたウィンドウを変更できません |
なし | 保護なし |
次の例は、ワークシートをパスワードで保護する方法を示しています。
// 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."); |
上記のコードを使用してワークシートを保護した後、ワークシートを開いて保護を確認します。ファイルを開いてワークシートにデータを追加しようとすると、次のダイアログが表示されます。
ユーザーがワークシートを変更できないことを警告するダイアログ
ワークシートで作業するには、保護、 それからシートの保護を解除からツール下図のようなメニュー項目。
シート保護解除メニュー項目の選択
パスワードの入力を求めるダイアログが開きます。
パスワードを入力してワークシートの保護を解除しています
少数を保護する Cells
ワークシート内のいくつかのセルのみをロックする必要がある特定のシナリオがあるかもしれません。ワークシート内の特定のセルをロックする場合は、ワークシート内の他のすべてのセルのロックを解除する必要があります。ワークシート内のすべてのセルは、すでにロック用に初期化されています。これを確認して、任意の Excel ファイルを MS Excel で開き、フォーマット | Cells…見せるフォーマット Cellsダイアログ ボックスを開き、[保護] タブをクリックすると、デフォルトで [ロック] というラベルの付いたチェック ボックスがオンになっていることがわかります。
以下は、タスクを実装するための 2 つのアプローチです。
方法1:
次のポイントでは、MS Excel を使用していくつかのセルをロックする方法について説明します。この方法は、Microsoft Office Excel 97、2000、2002、2003 以降のバージョンに適用されます。
- [すべて選択] ボタン (行 1 の行番号のすぐ上、列文字 A の左側にある灰色の四角形) をクリックして、ワークシート全体を選択します。
- [フォーマット] メニューの [Cells] をクリックし、[保護] タブをクリックして、[ロック] チェック ボックスをオフにします。
これにより、ワークシートのすべてのセルのロックが解除されます
- ロックするセルだけを選択して手順 2 を繰り返しますが、今回は [ロック] チェック ボックスをオンにします。
- 上でツールメニュー、選択保護 、 クリックプロテクトシートをクリックし、わかった.
方法 2:
この方法では、タスクを実行するためだけに Aspose.Cells API を使用します。
次の例は、ワークシート内のいくつかのセルを保護する方法を示しています。最初にワークシート内のすべてのセルのロックを解除し、次に 3 つのセル (A1、B1、C1) をロックします。最後に、ワークシートを保護します。行/列には、セット Locked メソッドをさらに含む 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 を使用すると、ワークシートの任意の行を簡単にロックできます。ここで、私たちは利用することができます[applyStyle()](https://reference.aspose.com/cells/java/com.aspose.cells/row#applyStyle(com.aspose.cells.Style,%20com.aspose.cells.StyleFlag) )の方法行ワークシートの特定の行に Style を適用するクラス。このメソッドは 2 つの引数を取ります。スタイルオブジェクトとスタイルフラグ適用された書式設定に関連するすべてのメンバーを持つ構造体。
次の例は、ワークシートの行を保護する方法を示しています。最初にワークシート内のすべてのセルのロックを解除してから、その最初の行をロックします。最後に、ワークシートを保護します。行/列には、setCellLocked メソッドをさらに含む Style API があります。 StyleFlag 構造体を使用して、行/列をロックまたはロック解除できます。
// 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 を使用すると、ワークシート内の任意の列を簡単にロックできます。ここで、私たちは利用することができます[applyStyle()](https://reference.aspose.com/cells/java/com.aspose.cells/column#applyStyle(com.aspose.cells.Style,%20com.aspose.cells.StyleFlag) )の方法桁ワークシートの特定の列に Style を適用するクラス。このメソッドは 2 つの引数を取ります。スタイルオブジェクトとスタイルフラグ適用された書式設定に関連するすべてのメンバーを持つ構造体。
次の例は、ワークシートの列を保護する方法を示しています。最初にワークシート内のすべてのセルのロックを解除してから、その最初の列をロックします。最後に、ワークシートを保護します。行/列には、セット Locked メソッドをさらに含む Style API があります。 StyleFlag 構造体を使用して、行/列をロックまたはロック解除できます。
// 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 エクセルを使う
ワークシートから保護を解除するには:
からツールメニュー、選択保護に続くシートの保護を解除.
保護解除シートの選択
ワークシートがパスワードで保護されていない限り、保護は解除されます。この場合、パスワードの入力を求めるダイアログが表示されます。
パスワードを入力してワークシートの保護を解除しています
Aspose.Cells を使用
を呼び出すことにより、ワークシートの保護を解除できます。ワークシートクラス'[保護解除](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#unprotect()) 方法。の[保護解除](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#unprotect()メソッドは、以下で説明する 2 つの方法で使用できます。
単純に保護されたワークシートの保護を解除する
単純に保護されたワークシートは、パスワードで保護されていないワークシートです。このようなワークシートは、パラメーターを渡さずに 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(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 クラスには、Excel ファイル内の各ワークシートへのアクセスを可能にする WorksheetCollection コレクションが含まれています。ワークシートは、ワークシートクラス。
Worksheet クラスは、これらの高度な保護設定を適用するために使用される 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."); |
Cell ロックの問題
ユーザーによるセルの編集を制限する場合は、保護設定を適用する前にセルをロックする必要があります。そうしないと、ワークシートが保護されていてもセルを編集できます。 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."); |