0

[ZSS] How set an Cell no-editable ? (Extended : how make work CellStyle ?)

asked 2014-07-16 13:07:09 +0800

kolodz gravatar image kolodz
1

Hi, I want block the edition of one specific cell. So the value can't be change by the final user.

I did use this code :

EditableCellStyle style = range.getCellStyleHelper().createCellStyle(range.getCellStyle()); style.setLocked(true); range.setCellStyle(style);

But it's not working ... And I don't know WHY ! No warning, no exception...

In fact any change on CellStyle don't apply... WHY ? My setCellValue that is make just after take effect...

And need a manual that is up to date... (Not some "this one is not updated, but you can look the new one that have nothing to say...")

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2014-07-17 05:09:08 +0800

RaymondChao gravatar image RaymondChao
386 1 4
ZK Team

Hello kolodz,

Did you try to protect sheet after set locked?

Here is a protect demo from zss essentials in ZSS 3.5.0 version:

protect.zul

<window hflex="1" vflex="1"
    apply="org.zkoss.zss.essential.ProtectionComposer">
    <hlayout hflex="1" vflex="1">
        <spreadsheet id="ss" hflex="1" vflex="1" showFormulabar="true" showContextMenu="true"
            showSheetbar="true" maxVisibleRows="100" maxVisibleColumns="40" 
            src="/WEB-INF/books/protection.xlsx" />
        <vlayout width="300px" vflex="1">
            <groupbox hflex="1" >
                <caption label="Protection" />
                <grid vflex="1" hflex="1">
                    <columns>
                        <column width="200px"/>
                        <column/>
                    </columns>
                    <rows>
                        <row>
                            Current Sheet Protection Status: 
                            <label id="status" style="font-weight:bold"/>
                        </row>
                        <row spans="2" align="right">
                            <button id="toggleProtection" label="Toggle Protection"/>
                        </row>
                        <row>
                            Current Cell Locked Status: 
                            <label id="lockStatus" style="font-weight:bold"/>
                        </row>
                        <row spans="2" align="right">
                            <button id="toggleLock" label="Toggle Lock"/>
                        </row>
                    </rows>
                </grid>
            </groupbox>
        </vlayout>
    </hlayout>
</window>

ProtectionComposer.java

package org.zkoss.zss.essential;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zss.api.Range;
import org.zkoss.zss.api.Ranges;
import org.zkoss.zss.api.model.CellStyle;
import org.zkoss.zss.api.model.EditableCellStyle;
import org.zkoss.zss.api.model.Sheet;
import org.zkoss.zss.ui.Spreadsheet;
import org.zkoss.zss.ui.event.CellSelectionEvent;
import org.zkoss.zss.ui.event.SheetSelectEvent;
import org.zkoss.zul.Label;

@SuppressWarnings("serial")
public class ProtectionComposer extends SelectorComposer<Component>{

    @Wire
    private Spreadsheet ss;
    @Wire
    private Label status;
    @Wire
    private Label lockStatus;


    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        updateSheetProtectionStatus(ss.getSelectedSheet());
    }

    @Listen("onClick = #toggleProtection")
    public void toggleProtection(){
        Sheet selectedSheet = ss.getSelectedSheet();
        if (selectedSheet.isProtected()){
            Ranges.range(selectedSheet).unprotectSheet("mypass");
        }else{
            Ranges.range(selectedSheet).protectSheet("mypass",
                    true, //allowSelectingLockedCells
                    true, //allowSelectingUnlockedCells,  
                    false, //allowFormattingCells
                    false, //allowFormattingColumns
                    false, //allowFormattingRows 
                    false, //allowInsertColumns 
                    false, //allowInsertRows
                    false, //allowInsertingHyperlinks
                    false, //allowDeletingColumns
                    false, //boolean allowDeletingRows
                    false, //allowSorting
                    false, //allowFiltering 
                    false, //allowUsingPivotTables
                    false, //drawingObjects
                    false  //boolean scenarios
            );
        }
        updateSheetProtectionStatus(selectedSheet);
    }

    @Listen("onSheetSelect = #ss")
    public void selectSheet(SheetSelectEvent event) {
        updateSheetProtectionStatus(event.getSheet());
    }

    private void updateSheetProtectionStatus(Sheet sheet){
        status.setValue(Boolean.toString(sheet.isProtected()));
    }

    @Listen("onClick = #toggleLock")
    public void toggleLock(){
        Range selection = Ranges.range(ss.getSelectedSheet(), ss.getSelection());
        CellStyle oldStyle = selection.getCellStyle();
        EditableCellStyle newStyle = selection.getCellStyleHelper().createCellStyle(oldStyle);
        newStyle.setLocked(!oldStyle.isLocked());
        selection.setCellStyle(newStyle);
        updateCellLockedStatus(newStyle.isLocked());
    }

    @Listen("onCellSelection = #ss")
    public void selectCells(CellSelectionEvent event) {
        CellStyle style = Ranges.range(ss.getSelectedSheet(), ss.getSelection()).getCellStyle();
        updateCellLockedStatus(style.isLocked());
    }

    private void updateCellLockedStatus(Boolean status){
        lockStatus.setValue(status.toString());
    }
}
link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2014-07-16 13:07:09 +0800

Seen: 23 times

Last updated: Jul 17 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More