Skip to content

Commit 5f4fb5b

Browse files
authored
Merge pull request #356 from JakKowalCzyk/add-diagonal-properties
Added possibility to define diagonal border properties (up and down)
2 parents ee6e597 + 3c8fb09 commit 5f4fb5b

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed

fastexcel-writer/src/main/java/org/dhatim/fastexcel/Border.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
package org.dhatim.fastexcel;
1717

1818
import java.io.IOException;
19-
import java.util.EnumMap;
20-
import java.util.Map;
19+
import java.util.*;
2120

2221
/**
2322
* Border attributes.
@@ -33,6 +32,10 @@ class Border {
3332
* Border elements.
3433
*/
3534
final Map<BorderSide, BorderElement> elements = new EnumMap<>(BorderSide.class);
35+
/**
36+
* Diagonal properties
37+
*/
38+
private final Set<DiagonalProperty> diagonalProperties = new HashSet<>();
3639

3740
/**
3841
* Default constructor.
@@ -77,6 +80,15 @@ void setElement(BorderSide side, BorderElement element) {
7780
elements.put(side, element);
7881
}
7982

83+
/**
84+
* Set a diagonal property.
85+
*
86+
* @param diagonalProperty Diagonal property.
87+
*/
88+
void setDiagonalProperty(DiagonalProperty diagonalProperty) {
89+
diagonalProperties.add(diagonalProperty);
90+
}
91+
8092
/**
8193
* Create a border where all sides have the same style and color.
8294
*
@@ -92,15 +104,15 @@ static Border fromStyleAndColor(String style, String color) {
92104

93105
@Override
94106
public int hashCode() {
95-
return elements.hashCode();
107+
return Objects.hash(elements, diagonalProperties);
96108
}
97109

98110
@Override
99111
public boolean equals(Object obj) {
100112
boolean result;
101113
if (obj != null && obj.getClass() == this.getClass()) {
102114
Border other = (Border) obj;
103-
result = elements.equals(other.elements);
115+
result = elements.equals(other.elements) && diagonalProperties.equals(other.diagonalProperties);
104116
} else {
105117
result = false;
106118
}
@@ -114,7 +126,14 @@ public boolean equals(Object obj) {
114126
* @throws IOException If an I/O error occurs.
115127
*/
116128
void write(Writer w) throws IOException {
117-
w.append("<border>");
129+
w.append("<border");
130+
if (diagonalProperties.contains(DiagonalProperty.DIAGONAL_UP)) {
131+
w.append(" diagonalUp=\"1\"");
132+
}
133+
if (diagonalProperties.contains(DiagonalProperty.DIAGONAL_DOWN)) {
134+
w.append(" diagonalDown=\"1\"");
135+
}
136+
w.append(">");
118137
elements.get(BorderSide.LEFT).write("left", w);
119138
elements.get(BorderSide.RIGHT).write("right", w);
120139
elements.get(BorderSide.TOP).write("top", w);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.dhatim.fastexcel;
2+
3+
/**
4+
* Identifies diagonal border of a cell.
5+
*/
6+
public enum DiagonalProperty {
7+
DIAGONAL_UP, DIAGONAL_DOWN
8+
}

fastexcel-writer/src/main/java/org/dhatim/fastexcel/StyleSetter.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,20 @@ public StyleSetter borderColor(BorderSide side, String borderColor) {
404404
return borderElement(side, border.elements.get(side).updateColor(borderColor));
405405
}
406406

407+
/**
408+
* Set cell diagonal property.
409+
*
410+
* @param diagonalProperty Diagonal border property which should be aplied to a cell
411+
* @return This style setter.
412+
*/
413+
public StyleSetter diagonalProperty(DiagonalProperty diagonalProperty) {
414+
if (border == null) {
415+
border = new Border();
416+
}
417+
border.setDiagonalProperty(diagonalProperty);
418+
return this;
419+
}
420+
407421
/**
408422
* Sets the value for a protection option.
409423
*

fastexcel-writer/src/test/java/org/dhatim/fastexcel/CorrectnessTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,36 @@ void testForIssue285() throws Exception {
596596
// }
597597
}
598598

599+
@Test
600+
public void testDiagonalProperties() throws Exception {
601+
writeWorkbook(wb -> {
602+
Worksheet worksheet = wb.newWorksheet("Worksheet 1");
603+
worksheet.style(0, 0)
604+
.borderColor(BorderSide.RIGHT, Color.BLACK)
605+
.borderStyle(BorderSide.RIGHT, BorderStyle.THIN)
606+
.borderColor(BorderSide.DIAGONAL, Color.BLACK)
607+
.borderStyle(BorderSide.DIAGONAL, BorderStyle.THIN)
608+
.diagonalProperty(DiagonalProperty.DIAGONAL_UP)
609+
.set();
610+
worksheet.style(1, 0)
611+
.borderColor(BorderSide.TOP, Color.BLACK)
612+
.borderStyle(BorderSide.TOP, BorderStyle.THIN)
613+
.borderColor(BorderSide.DIAGONAL, Color.BLACK)
614+
.borderStyle(BorderSide.DIAGONAL, BorderStyle.MEDIUM)
615+
.diagonalProperty(DiagonalProperty.DIAGONAL_DOWN)
616+
.set();
617+
worksheet.style(2, 0)
618+
.borderColor(BorderSide.TOP, Color.BLACK)
619+
.borderStyle(BorderSide.TOP, BorderStyle.THIN)
620+
.borderColor(BorderSide.DIAGONAL, Color.BLACK)
621+
.borderStyle(BorderSide.DIAGONAL, BorderStyle.MEDIUM)
622+
.diagonalProperty(DiagonalProperty.DIAGONAL_DOWN)
623+
.diagonalProperty(DiagonalProperty.DIAGONAL_UP)
624+
.diagonalProperty(DiagonalProperty.DIAGONAL_UP)
625+
.set();
626+
});
627+
}
628+
599629
@Test
600630
void testForIndent() throws Exception {
601631
writeWorkbook(wb -> {
@@ -646,7 +676,6 @@ void testCustomValidation() throws Exception {
646676
.error("Wrong value")
647677
.showErrorMessage(true)
648678
.errorStyle(DataValidationErrorStyle.STOP);
649-
650679
});
651680
}
652681
}

0 commit comments

Comments
 (0)