Spreadsheet non-editable cells

Is it possibly to programmatically disable editing for certain cells within the Spreadsheet component?

I have tried the following to no effect:

        CellStyle nonEditableStyle = workbook.createCellStyle();
        nonEditableStyle.setLocked( true );  

        Sheet sheet = spreadsheet.getActiveSheet();
        Cell cell = sheet.getRow( row ).getCell( col );
        cell.setCellStyle( nonEditableStyle );
        spreadsheet.setActiveSheetProtected( "abc" );

However, the cell is still editable. Is there a method to do this?

Hi, the following works for me at least:

var cell = spreadsheet.createCell(0, 0, "locked");
spreadsheet.setSheetProtected(0, "password");
var lockedCellStyle = spreadsheet.getActiveSheet().getWorkbook()
1 Like

Thanks, Tom, that did indeed work.

However, I am importing an existing XSLX to which I am applying the above logic.

The code you provided created a cell (with the String “locked”) which is now non-editable.

I tried, unsuccessfully, to apply this to existing cells using getCell().

Do you have to create a new cell in order to make it locked, and in turn copy all value/style/formula across? I would have thought there is a way to do this to an existing cell.

If the spreadsheet component is based on apache poi (which it looks like it is), then functions like getCell(…) and getRow(…) can return null if that cell / row is empty. You’ll need to check for null returns and if so then create that cell / row.

See this stack overflow answer and apache poi docs for reference.

1 Like