锁定或解锁形状
Contents
[
Hide
]
有时,您需要保护工作表中的所有形状,以防止它们被不需要的情况破坏。在这种情况下,您需要锁定指定工作表中的所有形状。
有时,您需要能够修改受保护工作表中的特定形状,在这种情况下,您需要解锁这些形状。
本文将详细介绍如何锁定和解锁指定的形状。
保护指定工作表中的所有形状
要保护指定工作表中的所有形状,请使用Worksheet.Protect(type)方法,如下所示的样本代码所示。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jpype | |
import asposecells | |
jpype.startJVM() | |
from asposecells.api import Workbook,SaveFormat,ProtectionType | |
text = "This is a test"; | |
workbook = Workbook(); | |
worksheet = workbook.getWorksheets().get(0); | |
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(r"Locked.xlsx", SaveFormat.XLSX); |
解锁受保护工作表中的指定形状
要解除保护受保护工作表中的特定形状,请使用shape.IsLocked,如下所示的样本代码所示。
注意:shape.IsLocked仅在工作表受保护时才有意义。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jpype | |
import asposecells | |
jpype.startJVM() | |
from asposecells.api import Workbook, SaveFormat,ProtectionType | |
workbook = Workbook(r"Locked.xlsx") | |
#Get protected worksheet | |
worksheet = workbook.getWorksheets().get(0) | |
#Get the specified shape to be unlocked | |
shape = worksheet.getShapes().get(r"TextBox 1") | |
#Unlock the specified shape | |
if (not worksheet.getProtection().getAllowEditingObject() and shape.isLocked()): | |
shape.setLocked(False); | |
workbook.save(r"UnLocked.xlsx", SaveFormat.XLSX); |