Change Cell's Style and Text Format"
m (→Composer) |
m |
||
Line 347: | Line 347: | ||
short fontHeight = getFontHeight(Integer.parseInt(fontSize.getText())); | short fontHeight = getFontHeight(Integer.parseInt(fontSize.getText())); | ||
setFontSize(fontHeight); | setFontSize(fontHeight); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ====Select font color==== | ||
+ | |||
+ | <source lang="java" high="4"> | ||
+ | Colorbox fontColor; | ||
+ | public void onChange$fontColor() { | ||
+ | String color = fontColor.getColor(); | ||
+ | setFontColor(color); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ====Set font bold==== | ||
+ | <source lang="java" high="5"> | ||
+ | boolean isBold; | ||
+ | Toolbarbutton boldBtn; | ||
+ | public void onClick$boldBtn() { | ||
+ | isBold = !isBold; | ||
+ | setFontBold(isBold); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ====Set font italic==== | ||
+ | <source lang="java" high="5"> | ||
+ | boolean isItalic; | ||
+ | Toolbarbutton italicBtn; | ||
+ | public void onClick$italicBtn() { | ||
+ | isItalic = !isItalic; | ||
+ | setItalic(isItalic); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ====Set font underline==== | ||
+ | <source lang="java" high="5"> | ||
+ | boolean isUnderline; | ||
+ | Toolbarbutton underlineBtn; | ||
+ | public void onClick$underlineBtn() { | ||
+ | isUnderline = !isUnderline; | ||
+ | setUnderline(isUnderline); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ====Set font strikethrough==== | ||
+ | <source lang="java" high="5"> | ||
+ | boolean isStrikethrough; | ||
+ | Toolbarbutton strikethroughBtn; | ||
+ | public void onClick$strikethroughBtn() { | ||
+ | isStrikethrough = !isStrikethrough; | ||
+ | setStrikethrough(isStrikethrough); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ====Select cell color=== | ||
+ | <source lang="java" high="4"> | ||
+ | Colorbox cellColor; | ||
+ | public void onChange$cellColor() { | ||
+ | String color = cellColor.getColor(); | ||
+ | setCellColor(color); | ||
} | } | ||
</source> | </source> |
Revision as of 03:01, 23 November 2010
Purpose
ZK Spreadsheet support various cell style and font style.
Style Supported | Notes |
---|---|
Font / Font Color / Fill Color | Cause of browser limitation, font also depends on installed font on client side |
Border / Border Color | Cause of browser limitation, only solid/dashed/dotted border are supported now. |
Horizontal Alignment | Vertical Alignment has not implemented yet |
Text Wrap & Overflow | |
Horizontal Merged Cell | Vertical Merged Cell has not implemented yet |
Clone cell style
To keep original cell style and only modify part of style, we can use the CellStyle.cloneStyleFrom(CellStyle) to clone a cell style.
CellStyle cloneStyle(CellStyle srcStyle, Book book) {
CellStyle newStyle = book.createCellStyle();
newStyle.cloneStyleFrom(srcStyle);
return newStyle;
}
Cell Style
Color
We can get color from CellStyle.getFillForegroundColor(), it return a color index. However, we usually use color in #RRGGBB format. We can translate string to index by BookHelper.rgbToIndex(book, color)
After we clone cell style, we can modify color by CellStyle.setFillForegroundColor(), and use Range.setStyle() to set new style
void setCellColor(String color) {
Rect rect = getSelection();
Sheet sheet = spreadsheet.getSelectedSheet();
Book book = spreadsheet.getBook();
short colorIndex = BookHelper.rgbToIndex(book, color);
for (int row = rect.getTop(); row <= rect.getBottom(); row++) {
for (int col = rect.getLeft(); col <= rect.getRight(); col++) {
Cell cell = Utils.getOrCreateCell(sheet, row, col);
CellStyle cellStyle = cell.getCellStyle();
final short srcColor = cellStyle.getFillForegroundColor();
if (srcColor != colorIndex) {
CellStyle newStyle = cloneStyle(cellStyle, book);
newStyle.cloneStyleFrom(cellStyle);
newStyle.setFillForegroundColor(colorIndex);
Ranges.range(sheet, row, col).setStyle(newStyle);
}
}
}
}
Alignment
We can get cell's alignment information by CellStyle.getAlignment(), and use CellStyle.setAlignment() to set alignment.
void setAlignment(short alignment) {
Rect rect = getSelection();
Sheet sheet = spreadsheet.getSelectedSheet();
Book book = spreadsheet.getBook();
for (int row = rect.getTop(); row <= rect.getBottom(); row++) {
for (int col = rect.getLeft(); col <= rect.getRight(); col++) {
Cell cell = Utils.getOrCreateCell(sheet, row, col);
short srcAlign = cell.getCellStyle().getAlignment();
if (srcAlign != alignment) {
CellStyle newStyle = cloneStyle(cell.getCellStyle(), book);
newStyle.setAlignment(alignment);
Ranges.range(sheet, row, col).setStyle(newStyle);
}
}
}
}
Border
We can set border by Range.setBorders(), specify
1. Border position
Border position can be top (BookHelper.BORDER_EDGE_TOP) , left (BookHelper.BORDER_EDGE_LEFT) etc...
2. Border Style Style can be BorderStyle.MEDIUM or set BorderStyle.NONE to remove border.
3. Border color Color in #RRGGBB format
void setBorder(String border) {
//Border color
String color = "#000000";
//Border style
BorderStyle style = "none".equals(border) ? BorderStyle.NONE : BorderStyle.MEDIUM;
Rect rect = getSelection();
Sheet sheet = spreadsheet.getSelectedSheet();
int lCol = rect.getLeft();
int rCol = rect.getRight();
int tRow = rect.getTop();
int bRow = rect.getBottom();
if ("bottom".equals(border)) {
Ranges.range(sheet, tRow, lCol, bRow, rCol).
setBorders(BookHelper.BORDER_EDGE_BOTTOM, style, color);
} else if ("top".equals(border)) {
Ranges.range(sheet, tRow, lCol, tRow, rCol).
setBorders(BookHelper.BORDER_EDGE_TOP, style, color);
} else if ("left".equals(border)) {
Ranges.range(sheet, tRow, lCol, bRow, lCol).
setBorders(BookHelper.BORDER_EDGE_LEFT, style, color);
} else if ("right".equals(border)) {
Ranges.range(sheet, tRow, rCol, bRow, rCol).
setBorders(BookHelper.BORDER_EDGE_RIGHT, style, color);
} else if ("none".equals(border)) {
Ranges.range(sheet, tRow, lCol, bRow, rCol).
setBorders(BookHelper.BORDER_FULL, style, color);
} else if ("full".equals(border)) {
Ranges.range(sheet, tRow, lCol, bRow, rCol).
setBorders(BookHelper.BORDER_FULL, style, color);
}
}
Font Style
From CellStyle.getFontIndex(), we can font index in the book, then we can get Font from Book.getFontAt()
We can create Font by BookHelper.getOrCreateFont() and specify attributes, for example font family, font size, color, ext...
Font family
void setFontFamily(String fontName) {
Rect rect = getSelection();
Sheet sheet = spreadsheet.getSelectedSheet();
Book book = spreadsheet.getBook();
for (int row = rect.getTop(); row <= rect.getBottom(); row++) {
for (int col = rect.getLeft(); col <= rect.getRight(); col++) {
Cell cell = Utils.getOrCreateCell(sheet, row, col);
Font srcFont = book.getFontAt(cell.getCellStyle().getFontIndex());
if (srcFont.getFontName() != fontName) {
Font newFont = BookHelper.getOrCreateFont(book,
srcFont.getBoldweight(), BookHelper.getFontColor(book, srcFont),
Font.getFontHeight(), fontName, srcFont.getItalic(), srcFont.getStrikeout(),
srcFont.getTypeOffset(), srcFont.getUnderline());
CellStyle newStyle = cloneStyle(cell.getCellStyle(), book);
newStyle.setFont(newFont);
Ranges.range(sheet, row, col).setStyle(newStyle);
}
}
}
}
Font size
We can get font height by Font.getFontHeight(), however font size is different from font height. Font height in unit's of 1/20th of a point, so we can transform font size to font height by
short getFontHeight(int fontSize) {
return (short) (fontSize * 20);
}
After we get the font height to set, we can use BookHelper.getOrCreateFont() to create font.
void setFontSize(short fontHeight) {
Rect rect = getSelection();
Sheet sheet = spreadsheet.getSelectedSheet();
Book book = spreadsheet.getBook();
for (int row = rect.getTop(); row <= rect.getBottom(); row++) {
for (int col = rect.getLeft(); col <= rect.getRight(); col++) {
Cell cell = Utils.getOrCreateCell(sheet, row, col);
Font srcFont = book.getFontAt(cell.getCellStyle().getFontIndex());
if (srcFont.getFontHeight() != fontHeight) {
Font newFont = BookHelper.getOrCreateFont(book, srcFont.getBoldweight(),
BookHelper.getFontColor(book, srcFont), fontHeight, srcFont.getFontName(),
srcFont.getItalic(), srcFont.getStrikeout(), srcFont.getTypeOffset(), srcFont.getUnderline());
CellStyle newStyle = cloneStyle(cell.getCellStyle(), book);
newStyle.setFont(newFont);
Ranges.range(sheet, row, col).setStyle(newStyle);
}
}
}
}
Bold
We can get font bold weight by Font.getBoldweight()
void setFontBold(boolean isBold) {
Rect rect = getSelection();
Sheet sheet = spreadsheet.getSelectedSheet();
Book book = spreadsheet.getBook();
for (int row = rect.getTop(); row <= rect.getBottom(); row++) {
for (int col = rect.getLeft(); col <= rect.getRight(); col++) {
Cell cell = Utils.getOrCreateCell(sheet, row, col);
Font srcFont = book.getFontAt(
cell.getCellStyle().getFontIndex());
boolean srcBold = srcFont.getBoldweight() == Font.BOLDWEIGHT_BOLD;
if (srcBold != isBold) {
Font newFont = BookHelper.getOrCreateFont(book,
isBold ? Font.BOLDWEIGHT_BOLD : Font.BOLDWEIGHT_NORMAL,
BookHelper.getFontColor(book, srcFont), srcFont.getFontHeight(),
srcFont.getFontName(), srcFont.getItalic(), srcFont.getStrikeout(),
srcFont.getTypeOffset(), srcFont.getUnderline());
CellStyle newStyle = cloneStyle(cell.getCellStyle(), book);
newStyle.setFont(newFont);
Ranges.range(sheet, row, col).setStyle(newStyle);
}
}
}
}
Italic
We can know whether font use italic or not by Font.getItalic()
void setItalic(boolean isItalic) {
Rect rect = getSelection();
Sheet sheet = spreadsheet.getSelectedSheet();
Book book = spreadsheet.getBook();
for (int row = rect.getTop(); row <= rect.getBottom(); row++) {
for (int col = rect.getLeft(); col <= rect.getRight(); col++) {
Cell cell = Utils.getOrCreateCell(sheet, row, col);
Font srcFont = book.getFontAt(
cell.getCellStyle().getFontIndex());
boolean srcItalic = srcFont.getItalic();
if (srcItalic != isItalic) {
Font newFont = BookHelper.getOrCreateFont(book,
srcFont.getBoldweight(), BookHelper.getFontColor(book, srcFont),
srcFont.getFontHeight(), srcFont.getFontName(), isItalic,
srcFont.getStrikeout(), srcFont.getTypeOffset(), srcFont.getUnderline());
CellStyle newStyle = cloneStyle(cell.getCellStyle(), book);
newStyle.setFont(newFont);
Ranges.range(sheet, row, col).setStyle(newStyle);
}
}
}
}
Underline
We can get font underline information by Font.getUnderline()
void setUnderline(boolean isUnderline){
Rect rect = getSelection();
Sheet sheet = spreadsheet.getSelectedSheet();
Book book = spreadsheet.getBook();
for (int row = rect.getTop(); row <= rect.getBottom(); row++) {
for (int col = rect.getLeft(); col <= rect.getRight(); col++) {
Cell cell = Utils.getOrCreateCell(sheet, row, col);
Font srcFont = book.getFontAt(
cell.getCellStyle().getFontIndex());
boolean srcUnderline = srcFont.getUnderline() == Font.U_SINGLE;
if (srcUnderline != isUnderline) {
Font newFont = BookHelper.getOrCreateFont(book, srcFont.getBoldweight(),
BookHelper.getFontColor(book, srcFont), srcFont.getFontHeight(),
srcFont.getFontName(), srcFont.getItalic(), srcFont.getStrikeout(),
srcFont.getTypeOffset(), isUnderline ? Font.U_SINGLE : Font.U_NONE);
CellStyle newStyle = cloneStyle(cell.getCellStyle(), book);
newStyle.setFont(newFont);
Ranges.range(sheet, row, col).setStyle(newStyle);
}
}
}
}
Strikethrough
We can know whether font use strikethrough or not by Font.getStrikeout()
void setStrikethrough(boolean isStrikethrough) {
Rect rect = getSelection();
Sheet sheet = spreadsheet.getSelectedSheet();
Book book = spreadsheet.getBook();
for (int row = rect.getTop(); row <= rect.getBottom(); row++) {
for (int col = rect.getLeft(); col <= rect.getRight(); col++) {
Cell cell = Utils.getOrCreateCell(sheet, row, col);
Font srcFont = book.getFontAt(
cell.getCellStyle().getFontIndex());
boolean srcStrikethrough = srcFont.getStrikeout();
if (srcStrikethrough != isStrikethrough) {
Font newFont = BookHelper.getOrCreateFont(book, srcFont.getBoldweight(),
BookHelper.getFontColor(book, srcFont), srcFont.getFontHeight(),
srcFont.getFontName(), srcFont.getItalic(), isStrikethrough,
srcFont.getTypeOffset(), srcFont.getUnderline());
CellStyle newStyle = cloneStyle(cell.getCellStyle(), book);
newStyle.setFont(newFont);
Ranges.range(sheet, row, col).setStyle(newStyle);
}
}
}
}
Composer
Current selection
Rect selection;
public void onCellSelection$spreadsheet(CellSelectionEvent event) {
selection = spreadsheet.getSelection();
}
Select font family
Combobox fontFamily;
public void onSelect$fontFamily() {
String fontName = fontFamily.getText();
setFontFamily(fontName);
}
Select font size
Combobox fontSize
public void onSelect$fontSize() {
short fontHeight = getFontHeight(Integer.parseInt(fontSize.getText()));
setFontSize(fontHeight);
}
Select font color
Colorbox fontColor;
public void onChange$fontColor() {
String color = fontColor.getColor();
setFontColor(color);
}
Set font bold
boolean isBold;
Toolbarbutton boldBtn;
public void onClick$boldBtn() {
isBold = !isBold;
setFontBold(isBold);
}
Set font italic
boolean isItalic;
Toolbarbutton italicBtn;
public void onClick$italicBtn() {
isItalic = !isItalic;
setItalic(isItalic);
}
Set font underline
boolean isUnderline;
Toolbarbutton underlineBtn;
public void onClick$underlineBtn() {
isUnderline = !isUnderline;
setUnderline(isUnderline);
}
Set font strikethrough
boolean isStrikethrough;
Toolbarbutton strikethroughBtn;
public void onClick$strikethroughBtn() {
isStrikethrough = !isStrikethrough;
setStrikethrough(isStrikethrough);
}
=Select cell color
Colorbox cellColor;
public void onChange$cellColor() {
String color = cellColor.getColor();
setCellColor(color);
}
Version History
Version | Date | Content |
---|---|---|
All source code listed in this book is at Github.