ワークシートの保護

ワークシートを保護

紹介

Microsoft Excelの一般的な保護オプション:

  • コンテンツ
  • オブジェクト
  • シナリオ

保護されたワークシートは機密データを非表示または保護しないため、ファイルの暗号化とは異なります。一般的に、ワークシートの保護はプレゼンテーション目的に適しています。ユーザーはワークシート内のデータ、コンテンツ、および書式設定を変更できなくなります。

ワークシートを保護する

Aspose.Cellsは、Microsoft Excelファイルを表すWorkbookクラスを提供します。Workbookクラスには、Excelファイル内の各ワークシートにアクセスを許可するWorksheetsコレクションが含まれています。ワークシートは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-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook excel = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = excel.Worksheets[0];
// Protecting the worksheet with a password
worksheet.Protect(ProtectionType.All, "aspose", null);
// Saving the modified Excel file in default format
excel.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003);
// Closing the file stream to free all resources
fstream.Close();

上記のコードを使用してワークシートを保護した後、ワークシートの保護を開いて確認することができます。ファイルを開いてワークシートにデータを追加しようとすると、次のダイアログが表示されます:

ユーザーがワークシートを変更できないと警告するダイアログ
todo:image_alt_text

ワークシートで作業するには、ツールメニューの保護からワークシートの保護を解除を選択します。

ワークシートの保護を解除メニュー項目を選択すると、次のようなダイアログが開きます。パスワードを入力するように求められます。

|todo:image_alt_text|

Microsoft Excelを使用してワークシート内の一部のセルを保護する

ワークシート内の特定のセルをロックする必要がある場合があります。ワークシート内の特定のセルをロックするには、ワークシートの他のすべてのセルをロック解除する必要があります。ワークシートのすべてのセルは既にロックされています。MS Excelに任意のExcelファイルを開いて書式 | セル…をクリックしてセルの書式ダイアログボックスを表示し、保護タブをクリックし、「ロック」のチェックボックスがデフォルトでチェックされていることを確認できます。

次のポイントは、MS Excelを使用していくつかのセルをロックする方法を説明しています。この方法は、Microsoft Office Excel 97、2000、2002、2003およびそれ以降のバージョンに適用されます。

  1. 全選択ボタン(行1の行番号の直上および列文字Aの左直上の灰色の四角形)をクリックしてワークシート全体を選択します。
  2. 書式メニューのセルをクリックし、保護タブをクリックし、ロックのチェックボックスをクリアします。 これにより、ワークシートのすべてのセルがロック解除されます セルコマンドが利用できない場合、ワークシートの一部は既にロックされている可能性があります。 ツールメニューで、保護を指して、ワークシートの保護を解除をクリックします。
  3. ロックしたいセルだけを選択し、ステップ2を繰り返しますが、この時にロックのチェックボックスを選択します。
  4. ツールメニューで保護を指して、ワークシートの保護をクリックし、OKをクリックします。
  5. ワークシートの保護ダイアログボックスでは、ユーザーが変更できる要素を指定するオプションとパスワードを指定することができます。

Aspose Cellsを使用してワークシート内の一部のセルを保護する

この方法では、Aspose.Cells APIのみを使用してタスクを実行します。

例: 次の例は、ワークシート内の一部のセルを保護する手順を示しています。まずワークシートのすべてのセルをロック解除し、それから3つのセル(A1、B1、C1)をロックします。最後にワークシートを保護します。 Styleオブジェクトには、真偽値プロパティIsLockedが含まれています。 IsLockedプロパティをtrueまたはfalseに設定し、*Column/Row.ApplyStyle()*メソッドを使用して希望の属性で行または列をロックまたはロック解除できます。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Create a new workbook.
Workbook wb = new Workbook();
// Create a worksheet object and obtain the first sheet.
Worksheet sheet = wb.Worksheets[0];
// Define the style object.
Style style;
// Define the styleflag object
StyleFlag styleflag;
// Loop through all the columns in the worksheet and unlock them.
for (int i = 0; i <= 255; i++)
{
style = sheet.Cells.Columns[(byte)i].GetStyle();
style.IsLocked = false;
styleflag = new StyleFlag();
styleflag.Locked = true;
sheet.Cells.Columns[(byte)i].ApplyStyle(style, styleflag);
}
// Lock the three cells...i.e. A1, B1, C1.
style = sheet.Cells["A1"].GetStyle();
style.IsLocked = true;
sheet.Cells["A1"].SetStyle(style);
style = sheet.Cells["B1"].GetStyle();
style.IsLocked = true;
sheet.Cells["B1"].SetStyle(style);
style = sheet.Cells["C1"].GetStyle();
style.IsLocked = true;
sheet.Cells["C1"].SetStyle(style);
// Finally, Protect the sheet now.
sheet.Protect(ProtectionType.All);
// Save the excel file.
wb.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003);

ワークシート内の行を保護する

Aspose.Cellsを使用すると、ワークシート内の任意の行を簡単にロックできます。ここでは、ワークシート内の特定の行にApplyStyle()メソッドをAspose.Cells.Rowクラスを使用してStyleを適用できます。このメソッドは、StyleオブジェクトとStyleFlagオブジェクトを引数に取り、適用される書式に関連するすべてのメンバーを持っています。

次の例は、ワークシート内の行を保護する手順を示しています。まず、ワークシートのすべてのセルをロック解除してから、第1行をロックします。最後にワークシートを保護します。 Styleオブジェクトには、真偽値プロパティIsLockedが含まれています。 IsLockedプロパティをtrueまたはfalseに設定することで、StyleFlagオブジェクトを使用して行または列をロックまたはロック解除できます。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Create a new workbook.
Workbook wb = new Workbook();
// Create a worksheet object and obtain the first sheet.
Worksheet sheet = wb.Worksheets[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.Cells.Columns[(byte)i].GetStyle();
style.IsLocked = false;
flag = new StyleFlag();
flag.Locked = true;
sheet.Cells.Columns[(byte)i].ApplyStyle(style, flag);
}
// Get the first row style.
style = sheet.Cells.Rows[0].GetStyle();
// Lock it.
style.IsLocked = true;
// Instantiate the flag.
flag = new StyleFlag();
// Set the lock setting.
flag.Locked = true;
// Apply the style to the first row.
sheet.Cells.ApplyRowStyle(0, style, flag);
// Protect the sheet.
sheet.Protect(ProtectionType.All);
// Save the excel file.
wb.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003);

ワークシート内の列を保護する

Aspose.Cellsを使用すると、ワークシート内の任意の列を簡単にロックできます。ここでは、ワークシート内の特定の列にApplyStyle()メソッドをAspose.Cells.Columnクラスを使用してStyleを適用できます。このメソッドは、StyleオブジェクトとStyleFlagオブジェクトを引数に取り、適用される書式に関連するすべてのメンバーを持っています。

次の例は、ワークシート内の列を保護する手順を示しています。まず、ワークシートのすべてのセルをロック解除してから、第1列をロックします。最後にワークシートを保護します。 Styleオブジェクトには、真偽値プロパティIsLockedが含まれています。 IsLockedプロパティをtrueまたはfalseに設定することで、StyleFlagオブジェクトを使用して行または列をロックまたはロック解除できます。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Create a new workbook.
Workbook wb = new Workbook();
// Create a worksheet object and obtain the first sheet.
Worksheet sheet = wb.Worksheets[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.Cells.Columns[(byte)i].GetStyle();
style.IsLocked = false;
flag = new StyleFlag();
flag.Locked = true;
sheet.Cells.Columns[(byte)i].ApplyStyle(style, flag);
}
// Get the first column style.
style = sheet.Cells.Columns[0].GetStyle();
// Lock it.
style.IsLocked = true;
// Instantiate the flag.
flag = new StyleFlag();
// Set the lock setting.
flag.Locked = true;
// Apply the style to the first column.
sheet.Cells.Columns[0].ApplyStyle(style, flag);
// Protect the sheet.
sheet.Protect(ProtectionType.All);
// Save the excel file.
wb.Save(dataDir + "output.out.xls", SaveFormat.Excel97To2003);

ユーザーに範囲の編集を許可する

次の例は、保護されたワークシートで範囲の編集を許可する方法を示しています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiate a new Workbook
Workbook book = new Workbook();
// Get the first (default) worksheet
Worksheet sheet = book.Worksheets[0];
// Get the Allow Edit Ranges
ProtectedRangeCollection allowRanges = sheet.AllowEditRanges;
// Define ProtectedRange
ProtectedRange proteced_range;
// Create the range
int idx = allowRanges.Add("r2", 1, 1, 3, 3);
proteced_range = allowRanges[idx];
// Specify the passoword
proteced_range.Password = "123";
// Protect the sheet
sheet.Protect(ProtectionType.All);
// Save the Excel file
book.Save(dataDir + "protectedrange.out.xls");