Lås eller lås upp figurer

Skydda alla figurer i ett angivet arbetsblad

För att skydda alla former i ett angivet kalkylblad, använd metoden Worksheet.protect(int type), som visas i följande exempelkod.

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

Lås upp angivna figurer i ett skyddat arbetsblad

För att låsa upp en angiven form i ett skyddat kalkylblad, använd shape.IsLocked och shape.setLocked, som visas i följande exempelkod.

Observera: shape.IsLocked och shape.setLocked är meningsfulla endast när kalkylbladet är skyddat.

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