Get or Set Value, Formula, or Formatted Text"

From Documentation
m
m
Line 25: Line 25:
 
First, we need to get the current cell's position. We can get row, column index from CellEvent, then use Spreadsheet.getRowtitle() and Spreadsheet.getColumntitle()  to get current cell's title.
 
First, we need to get the current cell's position. We can get row, column index from CellEvent, then use Spreadsheet.getRowtitle() and Spreadsheet.getColumntitle()  to get current cell's title.
  
<source lang="java" >
+
<source lang="java" high="4">
 
public void onCellFocused$spreadsheet(CellEvent event) {
 
public void onCellFocused$spreadsheet(CellEvent event) {
 
int row = event.getRow();
 
int row = event.getRow();
Line 36: Line 36:
 
After we get cell's position, we can get cell's value from Range.getEditText()
 
After we get cell's position, we can get cell's value from Range.getEditText()
  
<source lang="java" >
+
<source lang="java" high="6">
 
public void onCellFocused$spreadsheet(CellEvent event) {
 
public void onCellFocused$spreadsheet(CellEvent event) {
 
         ...
 
         ...
Line 70: Line 70:
  
 
[[File:ZKSsEss_Spreadsheet_SetValue_Input.png]]
 
[[File:ZKSsEss_Spreadsheet_SetValue_Input.png]]
 +
 +
=Version History=
 +
{{LastUpdated}}
 +
{| border='1px' | width="100%"
 +
! Version !! Date !! Content
 +
|-
 +
| &nbsp;
 +
| &nbsp;
 +
| &nbsp;
 +
|}
  
 
{{ZKSpreadsheetEssentialsPageFooter}}
 
{{ZKSpreadsheetEssentialsPageFooter}}

Revision as of 04:38, 18 November 2010


Get or Set Value, Formula, or Formatted Text



ZK Spreadsheet use Range.getEditText() to get cell's edit tet, and Range.setEditText(text) to set cell's text as input by user.

Scenario

Implement a formula editor to allow user input text, formula etc. For this function, we can use a Combobox to show the current cell, and use Textbox as a editor

<combobox id="focusCombobox" mold="rounded" style="text-align:center;"></combobox>
<textbox id="formulaEditor" cols="100"/>
<spreadsheet id="spreadsheet" src="/demo_sample.xls"	
			maxrows="200" 
			maxcolumns="40"
			width="100%"
			height="450px"></spreadsheet>

Current Cell

We can use onCellFocused event to get current focus cell.

Cell position

First, we need to get the current cell's position. We can get row, column index from CellEvent, then use Spreadsheet.getRowtitle() and Spreadsheet.getColumntitle() to get current cell's title.

public void onCellFocused$spreadsheet(CellEvent event) {
	int row = event.getRow();
	int col = event.getColumn();
	focusCombobox.setText(spreadsheet.getRowtitle(row) + spreadsheet.getColumntitle(col));
 ...

Cell value

After we get cell's position, we can get cell's value from Range.getEditText()

public void onCellFocused$spreadsheet(CellEvent event) {
        ...

	Sheet sheet = spreadsheet.getSelectedSheet();
	currentCell = Utils.getCell(sheet, row, col);
	currentRange = Ranges.range(sheet, row, col);
	formulaEditor.setText(currentRange.getEditText());
}

Editor

For editor, we can use onOK event, which menus when user click Enter keyboard, set cell's value and make spreadsheet's focus to next cell

public void onOK$formulaEditor() {
	currentRange.setEditText(formulaEditor.getText());
	spreadsheet.focusTo(currentCell.getRowIndex() + 1, currentCell.getColumnIndex());
}

Second, we can also use onChange and onChanging event to get current editor's value and set cell's value

public void onChanging$formulaEditor(InputEvent event) {
	if (currentCell.getCellType() != Cell.CELL_TYPE_FORMULA)
		currentRange.setEditText(event.getValue());
}
	
public void onChange$formulaEditor() {
	currentRange.setEditText(formulaEditor.getText());
}

ZKSsEss Spreadsheet SetValue Input.png

Version History

Last Update : 2010/11/18


Version Date Content
     


All source code listed in this book is at Github.


Last Update : 2010/11/18

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.