We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How can I check if a cell is protected or not?

I have a workbook in which some of the cells are read-only.
In my little design I need to verify if the cell is or isn’t protected to put or not a value in it.

If the cell A1 is protected and in my code I write

worksheet.Cells[1, 1].Value = “Something”;

the value of my PROTECTED cell will be changed.


Any help regarding this?

Hi,

Thanks for your posting and using Aspose.Cells.

You can check Style.IsLocked property of any cell. If the cell is protected and read only, this property will be true and if the cell is not read only, this property will be false.

Please check the following code. It writes the data inside the cell if the cell is not locked. Please check the source Excel file attached. The cell A1 is not read only while the cell B1 is read only. If you see the output Excel file, you will see, data is written in cell A1 but no in cell B1.

C#


string filePath = @“F:\Shak-Data-RW\Downloads\sample.xlsx”;


Workbook workbook = new Workbook(filePath);


Worksheet worksheet = workbook.Worksheets[0];


//Access cell A1, A1 is not read only

Cell cell = worksheet.Cells[“A1”];


//Put value in a cell if it is not locked

Style style = cell.GetStyle();


if (style.IsLocked != true)

cell.PutValue(“Test”);


//Access cell B1, B1 is read only

cell = worksheet.Cells[“B1”];


//Put value in a cell if it is not locked

style = cell.GetStyle();


if (style.IsLocked != true)

cell.PutValue(“Test”);


workbook.Save(filePath + “.out.xlsx”, SaveFormat.Xlsx);