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

Free Support Forum - aspose.com

Unlock specific cells in a protected worksheet

Hi,

I’m generating a spreadsheet where I have two sheets that I want to protect.
In one of them, I need to let the user edit only 4 cells. All the rest should be protected.
The simplest implementation should be:

  1. protect the sheet
  2. unlock each cell I want to let the user edit.

The problem is that I was searching to check if it’s possible to do this (protect the entire sheet and unlock only a few cells) and it seems to not to be possible. Please… tell me I’m wrong and there’s a way to do this, otherwise I’ll have to lock all the existing cells in the sheet and unlock only 4 of them… as long as I know, it seems to be very costly in terms of performance.

Hi,

Thanks for your posting and using Aspose.Cells for Java.

Please see the following documentation article for your help.

http://www.aspose.com/docs/display/cellsjava/Protecting+Worksheets

The following example exhibits how to protect a
few cells in the worksheet. It unlocks all the cells in the worksheet
first and then locks 3 cells (A1, B1, C1) in it. Finally, it protects
the worksheet. A row / column has a Style API that further contains a set Locked method . You can use this method to lock or unlock the row / column.

Java


// Create a new workbook.

Workbook wb = new Workbook();


// Create a worksheet object and obtain the first sheet.

Worksheet sheet = wb.getWorksheets().get(0);


// Define the style object.

Style style;


// Loop through all the columns in the worksheet and unlock them.

for(int i = 0; i <= 255; i ++)

{

style = sheet.getCells().getColumns().get(i).getStyle();

style.setLocked(false);

sheet.getCells().getColumns().get(i).setStyle(style);


}


// Lock the three cells…i.e. A1, B1, C1.

style = sheet.getCells().get(“A1”).getStyle();

style.setLocked(true);

sheet.getCells().get(“A1”).setStyle(style);

style = sheet.getCells().get(“B1”).getStyle();

style.setLocked(true);

sheet.getCells().get(“B1”).setStyle(style);

style = sheet.getCells().get(“C1”).getStyle();

style.setLocked(true);

sheet.getCells().get(“C1”).setStyle(style);


// Save the excel file.

wb.save(“d:\test\lockedcells.xls”, FileFormatType.EXCEL_97_TO_2003);