Skip to content

Commit 25ecdff

Browse files
author
rzymek
committed
Consolidate duplicated col-index-to-string logic. Fixes issue #567
1 parent 2b3b57c commit 25ecdff

File tree

9 files changed

+81
-36
lines changed

9 files changed

+81
-36
lines changed

fastexcel-reader/src/main/java/org/dhatim/fastexcel/reader/CellAddress.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,17 @@ public int hashCode() {
100100

101101
@Override
102102
public String toString() {
103-
StringBuilder sb = new StringBuilder();
104-
format(sb, row, col);
105-
return sb.toString();
103+
return format(row, col).toString();
106104
}
107105

108-
static void format(StringBuilder sb, int row, int col) {
106+
static StringBuilder format(int row, int col) {
107+
return format(new StringBuilder(), row, col);
108+
}
109+
110+
static StringBuilder format(StringBuilder sb, int row, int col) {
109111
sb.append(convertNumToColString(col));
110112
sb.append(row + 1);
113+
return sb;
111114
}
112115

113116
public static String convertNumToColString(int col) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
import java.io.IOException;
1919
import java.time.LocalDate;
2020
import java.time.LocalDateTime;
21-
import java.time.ZoneId;
2221
import java.time.ZonedDateTime;
2322
import java.util.Date;
2423

24+
import static org.dhatim.fastexcel.CellAddress.convertNumToColString;
25+
2526
/**
2627
* A cell contains a value and a cached style index.
2728
*/
@@ -47,7 +48,7 @@ class Cell implements Ref {
4748
*/
4849
void write(Writer w, int r, int c) throws IOException {
4950
if (value != null || style != 0) {
50-
w.append("<c r=\"").append(colToString(c)).append(r + 1).append('\"');
51+
w.append("<c r=\"").append(convertNumToColString(c)).append(r + 1).append('\"');
5152
if (style != 0) {
5253
w.append(" s=\"").append(style).append('\"');
5354
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2016 Dhatim.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.dhatim.fastexcel;
17+
18+
import java.nio.charset.StandardCharsets;
19+
20+
final class CellAddress {
21+
private static final int COL_RADIX = 'Z' - 'A' + 1;
22+
23+
static StringBuilder format(int row, int col) {
24+
return format(new StringBuilder(), row, col);
25+
}
26+
27+
static StringBuilder format(StringBuilder sb, int row, int col) {
28+
sb.append(convertNumToColString(col));
29+
sb.append(row + 1);
30+
return sb;
31+
}
32+
33+
static String convertNumToColString(int col) {
34+
// Excel counts column A as the 1st column, we
35+
// treat it as the 0th one
36+
int excelColNum = col + 1;
37+
38+
final int MAX_COL_CHARS = 3;
39+
final byte[] colRef = new byte[MAX_COL_CHARS];
40+
int colRemain = excelColNum;
41+
int pos = 2;
42+
while (colRemain > 0) {
43+
int thisPart = colRemain % COL_RADIX;
44+
if (thisPart == 0) {
45+
thisPart = COL_RADIX;
46+
}
47+
colRemain = (colRemain - thisPart) / COL_RADIX;
48+
49+
colRef[pos--] = (byte) (thisPart + (int) 'A' - 1);
50+
}
51+
pos++;
52+
return new String(colRef, pos, (MAX_COL_CHARS - pos), StandardCharsets.ISO_8859_1);
53+
}
54+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.Comparator;
44

5+
import static org.dhatim.fastexcel.CellAddress.convertNumToColString;
6+
57
class Location implements Comparable<Location>, Ref {
68
final int row;
79
final int col;
@@ -28,7 +30,7 @@ public int compareTo(Location o) {
2830

2931
@Override
3032
public String toString() {
31-
return colToString(col) + (row + 1);
33+
return convertNumToColString(col) + (row + 1);
3234
}
3335

3436
@Override

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.*;
1919
import java.util.stream.IntStream;
2020

21+
import static org.dhatim.fastexcel.CellAddress.convertNumToColString;
22+
2123
/**
2224
* Definition of a range of cells.
2325
*/
@@ -139,7 +141,7 @@ public boolean equals(Object obj) {
139141

140142
@Override
141143
public String toString() {
142-
return colToString(left) + (top + 1) + ':' + colToString(right) + (bottom + 1);
144+
return convertNumToColString(left) + (top + 1) + ':' + convertNumToColString(right) + (bottom + 1);
143145
}
144146

145147
/**
@@ -150,7 +152,7 @@ public String toString() {
150152
* @return absolute reference
151153
*/
152154
public String toAbsoluteString() {
153-
return '$' + colToString(left) + '$' + (top + 1) + ":$" + colToString(right) + '$' + (bottom + 1);
155+
return '$' + convertNumToColString(left) + '$' + (top + 1) + ":$" + convertNumToColString(right) + '$' + (bottom + 1);
154156
}
155157

156158
/**
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
package org.dhatim.fastexcel;
22

33
public interface Ref {
4-
default String colToString(int col){
5-
StringBuilder sb = new StringBuilder();
6-
while (col >= 0) {
7-
sb.append((char) ('A' + (col % 26)));
8-
col = (col / 26) - 1;
9-
}
10-
return sb.reverse().toString();
11-
}
124
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package org.dhatim.fastexcel;
1717

18-
/** This class represents a range of columns for the
18+
import static org.dhatim.fastexcel.CellAddress.convertNumToColString;
19+
20+
/** This class represents a range of columns for the
1921
* repeating columns feature in the print setup. */
2022
class RepeatColRange implements Ref {
2123

@@ -32,6 +34,6 @@ public RepeatColRange(int from, int to) {
3234
*/
3335
@Override
3436
public String toString() {
35-
return "$" + colToString(from) + ":$" + colToString(to);
37+
return "$" + convertNumToColString(from) + ":$" + convertNumToColString(to);
3638
}
3739
}

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ public class Worksheet implements Closeable {
5555
*/
5656
public static final double MAX_ROW_HEIGHT = 409.5;
5757

58-
/**
59-
* Helper for calculating column names
60-
*/
61-
private static final Ref REF_HELPER = new Ref() {};
62-
6358
private final Workbook workbook;
6459
private final String name;
6560
/**
@@ -860,12 +855,8 @@ private static void writeCol(Writer w, int columnIndex, double maxWidth, boolean
860855
* Helper method to get a cell name from (x, y) cell position.
861856
* e.g. "B3" from cell position (2, 1)
862857
*/
863-
static String getCellMark(int row, int coll) {
864-
if (0 <= coll && coll <= 'Z' - 'A') {
865-
char columnLetter = (char) ('A' + coll);
866-
return String.valueOf(columnLetter) + String.valueOf(row+1);
867-
}
868-
return REF_HELPER.colToString(coll) + String.valueOf(row+1);
858+
private static String getCellMark(int row, int col) {
859+
return CellAddress.format(row,col).toString();
869860
}
870861

871862
@Override

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.function.Consumer;
3131

3232
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.dhatim.fastexcel.CellAddress.convertNumToColString;
3334
import static org.dhatim.fastexcel.Color.BLACK;
3435
import static org.junit.jupiter.api.Assertions.assertEquals;
3536
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -54,12 +55,9 @@ static byte[] writeWorkbook(Consumer<Workbook> consumer, String writeToFile) thr
5455

5556
@Test
5657
void colToName() {
57-
assertThat(new Ref() {
58-
}.colToString(26)).isEqualTo("AA");
59-
assertThat(new Ref() {
60-
}.colToString(702)).isEqualTo("AAA");
61-
assertThat(new Ref() {
62-
}.colToString(Worksheet.MAX_COLS - 1)).isEqualTo("XFD");
58+
assertThat(convertNumToColString(26)).isEqualTo("AA");
59+
assertThat(convertNumToColString(702)).isEqualTo("AAA");
60+
assertThat(convertNumToColString(Worksheet.MAX_COLS - 1)).isEqualTo("XFD");
6361
}
6462

6563
@Test

0 commit comments

Comments
 (0)