形状のロックまたはロック解除
場合によっては、特定のワークシート内のすべての形状を保護して、望ましくない状況によって破壊されるのを防ぐ必要があります。その場合は、指定されたワークシート内のすべての形状をロックする必要があります。
場合によっては、特定の保護されたワークシート内の特定の形状を変更する必要がある場合があります。その場合は、これらの形状をロック解除する必要があります。
この記事では、指定された形状をロックおよびロック解除する方法について詳しく説明します。
指定されたワークシート内のすべての形状を保護
指定されたワークシート内のすべての図形を保護するには、Worksheet.protect(int type) メソッドを使用します。次のサンプルコードに示すように。
String text = "This is a test"; | |
Workbook workbook = new Workbook(); | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
Shape shape = worksheet.getShapes().addTextBox(1, 0, 1, 0, 30, 100); | |
shape.setText(text); | |
shape = worksheet.getShapes().addRectangle(5, 0, 1, 0, 30, 100); | |
shape.setText(text); | |
shape = worksheet.getShapes().addButton(9, 0, 1, 0, 30, 100); | |
shape.setText(text); | |
shape = worksheet.getShapes().addOval(13, 0, 1, 0, 50, 100); | |
shape.setText(text); | |
//Protect all shapes in a specified worksheet | |
shape.getWorksheet().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("Locked.xlsx", SaveFormat.XLSX); |
保護されたワークシート内の指定された形状のロック解除
保護されたワークシート内の指定された図形をロック解除するには、shape.IsLocked と shape.setLocked を使用します。次のサンプルコードに示すように。
注意: shape.IsLocked と shape.setLocked は、ワークシートが保護されている場合にのみ意味があります。
Workbook workbook = new Workbook("Locked.xlsx"); | |
//Get protected worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
//Get the specified shape to be unlocked | |
Shape shape = worksheet.getShapes().get("TextBox 1"); | |
//Unlock the specified shape | |
if (!worksheet.getProtection().getAllowEditingObject() && shape.isLocked()) | |
{ | |
shape.setLocked(false); | |
} | |
workbook.save("UnLocked.xlsx", SaveFormat.XLSX); |