Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/strikethrough #461

Merged
merged 5 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions fastexcel-writer/src/main/java/org/dhatim/fastexcel/Font.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Font {
/**
* Default font.
*/
public static Font DEFAULT = build(false, false, false, "Calibri", BigDecimal.valueOf(11.0), "FF000000");
public static Font DEFAULT = build(false, false, false, "Calibri", BigDecimal.valueOf(11.0), "FF000000", false);

/**
* Bold flag.
Expand All @@ -56,6 +56,10 @@ class Font {
* RGB font color.
*/
private final String rgbColor;
/**
* Strikethrough flag.
*/
private final Boolean strikethrough;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use primitive type boolean to be consistent with the other flags.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to primitive type boolean.


/**
* Constructor.
Expand All @@ -66,8 +70,9 @@ class Font {
* @param name Font name.
* @param size Font size, in points.
* @param rgbColor RGB font color.
* @param strikethrough Strikethrough flag.
*/
Font(boolean bold, boolean italic, boolean underlined, String name, BigDecimal size, String rgbColor) {
Font(boolean bold, boolean italic, boolean underlined, String name, BigDecimal size, String rgbColor, Boolean strikethrough) {
if (size.compareTo(BigDecimal.valueOf(409)) > 0 || size.compareTo(BigDecimal.valueOf(1)) < 0) {
throw new IllegalStateException("Font size must be between 1 and 409 points: " + size);
}
Expand All @@ -77,6 +82,7 @@ class Font {
this.name = name;
this.size = size.setScale(2, RoundingMode.HALF_UP);
this.rgbColor = rgbColor;
this.strikethrough = strikethrough;
}

/**
Expand All @@ -88,31 +94,32 @@ class Font {
* @param name Font name. Defaults to "Calibri".
* @param size Font size, in points. Defaults to 11.0.
* @param rgbColor RGB font color. Defaults to "FF000000".
* @param strikethrough Strikethrough flag.
* @return New font object.
*/
public static Font build(Boolean bold, Boolean italic, Boolean underlined, String name, BigDecimal size, String rgbColor) {
return new Font(bold != null? bold : DEFAULT.bold, italic != null ? italic : DEFAULT.italic , underlined != null ? underlined : DEFAULT.underlined, name != null ? name : DEFAULT.name, size != null ? size:DEFAULT.size, rgbColor != null ? rgbColor: DEFAULT.rgbColor);
public static Font build(Boolean bold, Boolean italic, Boolean underlined, String name, BigDecimal size, String rgbColor, Boolean strikethrough) {
return new Font(bold != null? bold : DEFAULT.bold, italic != null ? italic : DEFAULT.italic , underlined != null ? underlined : DEFAULT.underlined, name != null ? name : DEFAULT.name, size != null ? size:DEFAULT.size, rgbColor != null ? rgbColor: DEFAULT.rgbColor, strikethrough != null ? strikethrough : DEFAULT.strikethrough);
}

@Override
public int hashCode() {
return Objects.hash(bold, italic, underlined, name, size, rgbColor);
return Objects.hash(bold, italic, underlined, name, size, rgbColor, strikethrough);
}

@Override
public boolean equals(Object obj) {
boolean result;
if (obj != null && obj.getClass() == this.getClass()) {
Font other = (Font) obj;
result = Objects.equals(bold, other.bold) && Objects.equals(italic, other.italic) && Objects.equals(underlined, other.underlined) && Objects.equals(name, other.name) && Objects.equals(size, other.size) && Objects.equals(rgbColor, other.rgbColor);
result = Objects.equals(bold, other.bold) && Objects.equals(italic, other.italic) && Objects.equals(underlined, other.underlined) && Objects.equals(name, other.name) && Objects.equals(size, other.size) && Objects.equals(rgbColor, other.rgbColor) && Objects.equals(strikethrough, other.strikethrough);
} else {
result = false;
}
return result;
}

public static boolean equalsDefault(Boolean bold, Boolean italic, Boolean underlined, String fontName, BigDecimal fontSize, String fontColor) {
return Objects.equals(bold, DEFAULT.bold) && Objects.equals(italic, DEFAULT.italic) && Objects.equals(underlined, DEFAULT.underlined) && Objects.equals(fontName, DEFAULT.name) && Objects.equals(fontSize, DEFAULT.size) && Objects.equals(fontColor, DEFAULT.rgbColor);
public static boolean equalsDefault(Boolean bold, Boolean italic, Boolean underlined, String fontName, BigDecimal fontSize, String fontColor, Boolean strikethrough) {
return Objects.equals(bold, DEFAULT.bold) && Objects.equals(italic, DEFAULT.italic) && Objects.equals(underlined, DEFAULT.underlined) && Objects.equals(fontName, DEFAULT.name) && Objects.equals(fontSize, DEFAULT.size) && Objects.equals(fontColor, DEFAULT.rgbColor) && Objects.equals(strikethrough, DEFAULT.strikethrough);
}

/**
Expand All @@ -123,6 +130,7 @@ public static boolean equalsDefault(Boolean bold, Boolean italic, Boolean underl
*/
void write(Writer w) throws IOException {
w.append("<font>").append(bold ? "<b/>" : "").append(italic ? "<i/>" : "").append(underlined ? "<u/>" : "").append("<sz val=\"").append(size.toString()).append("\"/>");
w.append(strikethrough ? "<strike/>" : "");
if (rgbColor != null) {
w.append("<color rgb=\"").append(rgbColor).append("\"/>");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ abstract class GenericStyleSetter<STYLE_SETTER extends GenericStyleSetter<STYLE_
* RGB font color.
*/
private String fontColor;
/**
* Strikethrough flag.
*/
private Boolean strikethrough;
/**
* Horizontal alignment.
*/
Expand Down Expand Up @@ -250,6 +254,15 @@ public STYLE_SETTER underlined() {
return getThis();
}

/**
* Use strikethrough text.
* @return This style setter.
*/
public STYLE_SETTER strikethrough() {
this.strikethrough = true;
return getThis();
}

/**
* Define horizontal alignment.
*
Expand Down Expand Up @@ -463,8 +476,8 @@ protected void setStyle(boolean shadingEnabled, Set<Integer> currentStyles,
alignment = null;
}
Font font;
if (!Font.equalsDefault(bold,italic,underlined,fontName,fontSize,fontColor)) {
font = Font.build(bold, italic, underlined, fontName, fontSize, fontColor);
if (!Font.equalsDefault(bold,italic,underlined,fontName,fontSize,fontColor, strikethrough)) {
font = Font.build(bold, italic, underlined, fontName, fontSize, fontColor, strikethrough);
} else {
font = Font.DEFAULT;
}
Expand Down Expand Up @@ -513,8 +526,8 @@ public void set(ConditionalFormattingRule conditionalFormattingRule) {
alignment = new Alignment(horizontalAlignment, verticalAlignment, wrapText, rotation, indent);
}
Font font = null;
if (bold != null && bold || italic != null && italic || underlined != null && underlined || fontColor != null || fontName != null || fontSize != null) {
font = Font.build(bold, italic, underlined, fontName, fontSize, fontColor);
if (bold != null && bold || italic != null && italic || underlined != null && underlined || fontColor != null || fontName != null || fontSize != null || strikethrough != null && strikethrough) {
font = Font.build(bold, italic, underlined, fontName, fontSize, fontColor, strikethrough);
}
Fill fill = null;
if (fillColor != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void setActiveTab(int tabIndex) {
}

public void setGlobalDefaultFont(String fontName, double fontSize) {
this.setGlobalDefaultFont(Font.build(null, null, null, fontName, BigDecimal.valueOf(fontSize), null));
this.setGlobalDefaultFont(Font.build(null, null, null, fontName, BigDecimal.valueOf(fontSize), null, null));
}

public void setGlobalDefaultFont(Font font) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ void testForAllFeatures() throws Exception {
.fontName("Cooper Black")
.borderColor(Color.SEA_BLUE)
.underlined()
.strikethrough()
.rotation(90)
.set();
//merge cells
Expand All @@ -371,6 +372,7 @@ void testForAllFeatures() throws Exception {
.fontName("Cooper Black")
.borderColor(Color.SEA_BLUE)
.underlined()
.strikethrough()
.shadeAlternateRows(Color.SEA_BLUE)
.shadeRows(Color.RED, 1)
.set();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,14 @@ void font() throws Exception {
String bold = "Bold";
String italic = "Italic";
String underlined = "Underlined";
String strikethrough = "Strikethrough";
String bold_italic = "Bold_italic";
String bold_underlined = "Bold_underlined";
String bold_strikethrough = "Bold_strikethrough";
String italic_underlinded = "Italic_underlined";
String all_three = "All_three";
String italic_strikethrough = "Italic_strikethrough";
String underlined_strikethrough = "Underlined_strikethrough";
String all_four = "All_four";
byte[] data = writeWorkbook(wb -> {
Worksheet ws = wb.newWorksheet(sheetName);
ws.value(0, 0, bold);
Expand All @@ -223,14 +227,22 @@ void font() throws Exception {
ws.style(0, 1).italic().set();
ws.value(0, 2, underlined);
ws.style(0, 2).underlined().set();
ws.value(0, 3, bold_italic);
ws.style(0, 3).bold().italic().set();
ws.value(0, 4, bold_underlined);
ws.style(0, 4).bold().underlined().set();
ws.value(0, 5, italic_underlinded);
ws.style(0, 5).italic().underlined().set();
ws.value(0, 6, all_three);
ws.style(0, 6).bold().italic().underlined().set();
ws.value(0, 3, strikethrough);
ws.style(0, 3).strikethrough().set();
ws.value(0, 4, bold_italic);
ws.style(0, 4).bold().italic().set();
ws.value(0, 5, bold_underlined);
ws.style(0, 5).bold().underlined().set();
ws.value(0, 6, bold_strikethrough);
ws.style(0, 6).bold().strikethrough().set();
ws.value(0, 7, italic_underlinded);
ws.style(0, 7).italic().underlined().set();
ws.value(0, 8, italic_strikethrough);
ws.style(0, 8).italic().strikethrough().set();
ws.value(0, 9, underlined_strikethrough);
ws.style(0, 9).underlined().strikethrough().set();
ws.value(0, 10, all_four);
ws.style(0, 10).bold().italic().underlined().strikethrough().set();
try {
ws.close();
} catch (IOException ex) {
Expand All @@ -244,15 +256,26 @@ void font() throws Exception {
assertTrue(xws.getRow(0).getCell(0).getCellStyle().getFont().getBold());
assertTrue(xws.getRow(0).getCell(1).getCellStyle().getFont().getItalic());
assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(2).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE);
assertTrue(xws.getRow(0).getCell(3).getCellStyle().getFont().getBold());
assertTrue(xws.getRow(0).getCell(3).getCellStyle().getFont().getItalic());
assertTrue(xws.getRow(0).getCell(3).getCellStyle().getFont().getStrikeout());

assertTrue(xws.getRow(0).getCell(4).getCellStyle().getFont().getBold());
assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(4).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE);
assertTrue(xws.getRow(0).getCell(5).getCellStyle().getFont().getItalic());
assertTrue(xws.getRow(0).getCell(4).getCellStyle().getFont().getItalic());
assertTrue(xws.getRow(0).getCell(5).getCellStyle().getFont().getBold());
assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(5).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE);
assertTrue(xws.getRow(0).getCell(6).getCellStyle().getFont().getBold());
assertTrue(xws.getRow(0).getCell(6).getCellStyle().getFont().getItalic());
assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(6).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE);
assertTrue(xws.getRow(0).getCell(6).getCellStyle().getFont().getStrikeout());
assertTrue(xws.getRow(0).getCell(7).getCellStyle().getFont().getItalic());
assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(7).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE);
assertTrue(xws.getRow(0).getCell(8).getCellStyle().getFont().getItalic());
assertTrue(xws.getRow(0).getCell(8).getCellStyle().getFont().getStrikeout());
assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(9).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE);
assertTrue(xws.getRow(0).getCell(9).getCellStyle().getFont().getStrikeout());

assertTrue(xws.getRow(0).getCell(10).getCellStyle().getFont().getBold());
assertTrue(xws.getRow(0).getCell(10).getCellStyle().getFont().getItalic());
assertEquals(FontUnderline.valueOf(xws.getRow(0).getCell(10).getCellStyle().getFont().getUnderline()), FontUnderline.SINGLE);
assertTrue(xws.getRow(0).getCell(10).getCellStyle().getFont().getStrikeout());

}

@Test
Expand Down Expand Up @@ -664,7 +687,7 @@ void hasValidNamedRange() throws Exception {
// Fetch the XSSF Name object
XSSFName name = xwb.getName("col names");
String formula = name.getRefersToFormula();

assertTrue(name != null);
assertTrue(name.getNameName().equals("col names"));
assertTrue(formula.equals("'Worksheet 1'!$A$1:$D$2"));
Expand All @@ -688,7 +711,7 @@ void hasValidCellConditionalFormatting() throws Exception {
XSSFConditionalFormattingRule condFmtRule = condFmt.getRule(0);
int numRanges = condFmt.getFormattingRanges().length;
CellRangeAddress cellRange = condFmt.getFormattingRanges()[0];

assertTrue(numCondFmts == 1);
assertTrue(numRules == 1);
assertTrue(numRanges == 1);
Expand All @@ -715,7 +738,7 @@ void hasValidRangeConditionalFormatting() throws Exception {
XSSFConditionalFormattingRule condFmtRule = condFmt.getRule(0);
int numRanges = condFmt.getFormattingRanges().length;
CellRangeAddress cellRange = condFmt.getFormattingRanges()[0];

assertTrue(numCondFmts == 1);
assertTrue(numRules == 1);
assertTrue(numRanges == 1);
Expand Down