Bloccare o sbloccare forme

Proteggere tutte le forme in un foglio di lavoro specificato

Per proteggere tutte le forme in un foglio di lavoro specificato, utilizzare il metodo Worksheet.Protect(type), come mostrato nel seguente esempio di codice.

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);

Sbloccare forme specificate in un foglio di lavoro protetto

Per sbloccare una forma specifica in un foglio di lavoro protetto, utilizzare shape.IsLocked, come mostrato nel seguente esempio di codice.

Nota: shape.IsLocked ha significato solo quando il foglio di lavoro è protetto.

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);