Excel XP以降の高度な保護設定
紹介
これらの保護設定により、ユーザーは次の操作を制限または許可できます:
- 行または列の削除。
- 内容、オブジェクト、またはシナリオの編集。
- セル、行、または列の書式設定。
- 行、列、またはハイパーリンクの挿入。
- ロックされたセルまたはロックされていないセルの選択。
- ピボットテーブルなどの使用。
Aspose.Cells for Python via .NETは、Excel XP以降のすべての高度な保護設定をサポートしています。
Excel XPおよびそれ以降のバージョンを使用した高度な保護設定
Excel XPで利用可能な保護設定を表示するには:
- ツールメニューから、保護の後にシートを保護を選択します。ダイアログが表示されます。
Excel 2016で利用可能な保護設定を表示するには
- ファイルメニューから、ワークブックを保護, その後現在のシートを保護を選択します。
- レビューメニューでシートを保護を選択します。
上記の手順に従うと、ワークシートの機能を許可または制限したり、ワークシートにパスワードを適用したりするダイアログが表示されます。
Aspose.Cells for Python via .NETを使用した高度な保護設定
Aspose.Cells for Python via .NETは、すべての高度な保護設定をサポートしています。
Aspose.Cells for Python via .NET は、Microsoft Excelファイルを表すクラス Workbook を提供します。Workbook クラスは、Excelファイル内の各ワークシートにアクセスできる worksheets コレクションを含みます。ワークシートは Worksheet クラスで表されます。
Worksheetクラスは、これらの高度な保護設定を適用するために使用されるprotectionプロパティを提供します。Protectionプロパティは実際にはProtectionクラスのオブジェクトであり、無効化または有効化するための複数のブール値プロパティをカプセル化しています。
以下は小さなサンプルアプリケーションです。それはExcelファイルを開いて、Excel XPおよびそれ以降のバージョンでサポートされる高度な保護設定のほとんどを使用します。
from aspose.cells import SaveFormat, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(dataDir + "book1.xls", "rb") | |
# Instantiating a Workbook object | |
# Opening the Excel file through the file stream | |
excel = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = excel.worksheets[0] | |
# Restricting users to delete columns of the worksheet | |
worksheet.protection.allow_deleting_column = False | |
# Restricting users to delete row of the worksheet | |
worksheet.protection.allow_deleting_row = False | |
# Restricting users to edit contents of the worksheet | |
worksheet.protection.allow_editing_content = False | |
# Restricting users to edit objects of the worksheet | |
worksheet.protection.allow_editing_object = False | |
# Restricting users to edit scenarios of the worksheet | |
worksheet.protection.allow_editing_scenario = False | |
# Restricting users to filter | |
worksheet.protection.allow_filtering = False | |
# Allowing users to format cells of the worksheet | |
worksheet.protection.allow_formatting_cell = True | |
# Allowing users to format rows of the worksheet | |
worksheet.protection.allow_formatting_row = True | |
# Allowing users to insert columns in the worksheet | |
worksheet.protection.allow_formatting_column = True | |
# Allowing users to insert hyperlinks in the worksheet | |
worksheet.protection.allow_inserting_hyperlink = True | |
# Allowing users to insert rows in the worksheet | |
worksheet.protection.allow_inserting_row = True | |
# Allowing users to select locked cells of the worksheet | |
worksheet.protection.allow_selecting_locked_cell = True | |
# Allowing users to select unlocked cells of the worksheet | |
worksheet.protection.allow_selecting_unlocked_cell = True | |
# Allowing users to sort | |
worksheet.protection.allow_sorting = True | |
# Allowing users to use pivot tables in the worksheet | |
worksheet.protection.allow_using_pivot_table = True | |
# Saving the modified Excel file | |
excel.save(dataDir + "output.xls", SaveFormat.EXCEL_97_TO_2003) | |
# Closing the file stream to free all resources | |
fstream.close() |
セルロックの問題
セルの編集を制限したい場合は、保護設定を適用する前にセルをロックする必要があります。そうでない場合、ワークシートが保護されていてもセルが編集できます。Microsoft Excel XPでは、次のダイアログでセルをロックできます:
Excel XPでセルをロックするダイアログ |
---|
![]() |
Aspose.Cells for Python via .NET APIを使えばセルをロックすることも可能です。各セルは Style 形式を持ち、その中にブーリアンの属性 is_locked があります。セルをロックまたはアンロックするには is_locked 属性をtrueまたはfalseに設定します。
from aspose.cells import ProtectionType, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
workbook = Workbook(dataDir + "Book1.xlsx") | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
worksheet.cells.get("A1").get_style().is_locked = True | |
# Finally, Protect the sheet now. | |
worksheet.protect(ProtectionType.ALL) | |
workbook.save(dataDir + "output.xlsx") |