锁定或解锁形状

保护指定工作表中的所有形状

要保护指定工作表中的所有形状,请使用Worksheet.Protect(ProtectionType)方法,如以下示例代码所示。

Aspose::Cells::Startup();
U16String text = u"This is a test";
Workbook 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(u"Locked.xlsx", SaveFormat::Xlsx);
Aspose::Cells::Cleanup();

解锁受保护工作表中的指定形状

要在受保护的工作表中解锁指定形状,请使用shape.IsLockedshape.SetIsLocked,如以下示例代码所示。

注意:shape.IsLockedshape.SetIsLocked仅在工作表受保护时才有意义。

Aspose::Cells::Startup();
Workbook workbook(u"Locked.xlsx");
//Get protected worksheet
Worksheet worksheet = workbook.GetWorksheets().Get(0);
//Get the specified shape to be unlocked
Shape shape = worksheet.GetShapes().Get(u"TextBox 1");
//Unlock the specified shape
if (!worksheet.GetProtection().GetAllowEditingObject() && shape.IsLocked())
{
shape.SetIsLocked(false);
}
workbook.Save(u"UnLocked.xlsx", SaveFormat::Xlsx);
Aspose::Cells::Cleanup();