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

Free Support Forum - aspose.com

How to set one cell to be readonly?

Hi,

I tried to set some cell to be readonly, but failed. Here is the code:

GridTableItemStyle colHeaderStyle = gridCells.get(“A1”).getStyle();
colHeaderStyle.setLocked(true);

GridCell gridCell = gridCells.get(“A1”);
gridCell.setValue(“TEST”);
gridCell.setStyle(colHeaderStyle);

Can anyone tell me what’s wrong? Thanks!

Hi,


I am afraid, this is not the right way to do it. This is a three-steps based procedure. If you need to lock a few cells, you got to first mark all the cells unlocked, then you may pick your desired cells to set them locked, mind you by default all the cells are marked as locked (you may confirm this in MS Excel by checking to see the Protection tab in Format Cells dialog box), now protect the sheet, it will work for your needs. I have written a sample code for your reference which locks only the B10 in the sheet.
e.g
Sample code:

//1) Set all cells not locked (by default these are locked same as per MS Excel) in the sheet.
for(int i = 0; i<gridweb.getWorkSheets().get(0).getCells().getMaxDataRow(); i++ )
{
for(int j = 0; j< gridweb.getWorkSheets().get(0).getCells().getMaxDataColumn(); j++)
{
GridTableItemStyle stl = gridweb.getWorkSheets().get(0).getCells().get(i,j).getStyle();
stl.setLocked(false);
gridweb.getWorkSheets().get(0).getCells().get(i,j).setStyle(stl);
}
}
//2) Now set locked for your favorite cells only, it will lock for your favorite cells only.
GridTableItemStyle style = gridweb.getWorkSheets().get(0).getCells().get(“B10”).getStyle();
style.setLocked(true);
gridweb.getWorkSheets().get(0).getCells().get(“B10”).setStyle(style);
//3) Protect the sheet.
gridweb.getWorkSheets().get(0).setProtect();

Hope, this helps you a bit.

Thank you.

Hi Amjad Sahi,

Thank you for replying. And I tried and it worked!
Thanks again!

Hi,


Good to know that your issue is figured out now. Feel free to write back in case you need further assistance or have some other issue or queries, we will be happy to assist you soon.

Thank you.