形状のロックまたはロック解除
場合によっては、特定のワークシート内のすべての形状を保護して、望ましくない状況によって破壊されるのを防ぐ必要があります。その場合は、指定されたワークシート内のすべての形状をロックする必要があります。
場合によっては、特定の保護されたワークシート内の特定の形状を変更する必要がある場合があります。その場合は、これらの形状をロック解除する必要があります。
この記事では、指定された形状をロックおよびロック解除する方法について詳しく説明します。
指定されたワークシート内のすべての形状を保護
指定されたワークシート内のすべての図形を保護するには、Worksheet.Protect(type)メソッドを使用します。以下は、これを示すサンプルコードです。
from aspose.cells import Workbook, FileFormatType,SaveFormat,ProtectionType | |
text = "This is a test"; | |
workbook = Workbook(); | |
worksheet = workbook.worksheets.get(0); | |
shape = worksheet.shapes.add_text_box(1, 0, 1, 0, 30, 100); | |
shape.text = text; | |
shape = worksheet.shapes.add_rectangle(5, 0, 1, 0, 30, 100); | |
shape.text = text; | |
shape = worksheet.shapes.add_button(9, 0, 1, 0, 30, 100); | |
shape.text = text; | |
shape = worksheet.shapes.add_oval(13, 0, 1, 0, 50, 100); | |
shape.text = text; | |
#Protect all shapes in a specified worksheet | |
shape.worksheet.protect(ProtectionType.OBJECTS);#Protects the entire worksheet. | |
#or shape.worksheet.protect(ProtectionType.All);//Protects all shapes in the specified worksheet. | |
#or worksheet.protect(ProtectionType.Objects);//Protects the entire worksheet. | |
#or worksheet.protect(ProtectionType.All);//Protects all shapes in the specified worksheet. | |
workbook.save(r"Locked.xlsx", SaveFormat.XLSX); |
保護されたワークシート内の指定された形状のロック解除
保護されたワークシート内の指定された図形をアンロックするには、shape.is_lockedを使用します。以下は、これを示すサンプルコードです。
ノート:shape.is_locked は、ワークシートが保護されている場合にのみ意味を持ちます。
from aspose.cells import Workbook, FileFormatType,SaveFormat,ProtectionType | |
workbook = Workbook(r"Locked.xlsx") | |
#Get protected worksheet | |
worksheet = workbook.worksheets[0] | |
#Get the specified shape to be unlocked | |
shape = worksheet.shapes[0] | |
#Unlock the specified shape | |
if (not worksheet.protection.allow_editing_object and shape.is_locked): | |
shape.is_locked = False; | |
workbook.save(r"UnLocked.xlsx", SaveFormat.XLSX); |