Skip to content

Commit 9b17091

Browse files
authored
feat: Introduced Workbook constant for maximum sheet name length validation (#658)
* feat: Introduced Workbook constant for maximum sheet name length validation * test: Added a longer sheet name to the existing sheet name list * refactor: reorganize import statements in ExcelWriterSheetBuilder * test: add sheet name length validation test case
1 parent 36615f1 commit 9b17091

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

fesod/src/main/java/org/apache/fesod/excel/write/builder/ExcelWriterSheetBuilder.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,19 @@
2222
import java.util.Collection;
2323
import java.util.Objects;
2424
import java.util.function.Supplier;
25+
import lombok.extern.slf4j.Slf4j;
2526
import org.apache.fesod.excel.ExcelWriter;
2627
import org.apache.fesod.excel.exception.ExcelGenerateException;
2728
import org.apache.fesod.excel.write.metadata.WriteSheet;
2829
import org.apache.fesod.excel.write.metadata.fill.FillConfig;
30+
import org.apache.poi.ss.usermodel.Workbook;
2931

3032
/**
3133
* Build sheet
3234
*
3335
*
3436
*/
37+
@Slf4j
3538
public class ExcelWriterSheetBuilder extends AbstractExcelWriterParameterBuilder<ExcelWriterSheetBuilder, WriteSheet> {
3639
private ExcelWriter excelWriter;
3740
/**
@@ -73,13 +76,13 @@ public ExcelWriterSheetBuilder sheetNoIfNotNull(Integer sheetNo) {
7376
* @return
7477
*/
7578
public ExcelWriterSheetBuilder sheetName(String sheetName) {
76-
writeSheet.setSheetName(sheetName);
79+
writeSheet.setSheetName(sensitiveSheetName(sheetName));
7780
return this;
7881
}
7982

8083
public ExcelWriterSheetBuilder sheetNameIfNotNull(String sheetName) {
8184
if (Objects.nonNull(sheetName)) {
82-
writeSheet.setSheetName(sheetName);
85+
writeSheet.setSheetName(sensitiveSheetName(sheetName));
8386
}
8487
return this;
8588
}
@@ -136,4 +139,31 @@ public ExcelWriterTableBuilder table(Integer tableNo) {
136139
protected WriteSheet parameter() {
137140
return writeSheet;
138141
}
142+
143+
/**
144+
* Processes worksheet names to ensure they comply with MS Excel's length limitations.
145+
* <p>
146+
* If the provided sheet name exceeds the maximum allowed length defined by
147+
* {@link Workbook#MAX_SENSITIVE_SHEET_NAME_LEN}, it will be truncated to fit within
148+
* the limit. A warning message is logged when truncation occurs, as the original
149+
* sheet name will not be available in scenarios such as formula references.
150+
* </p>
151+
*
152+
* @param sheetName the original worksheet name to process
153+
* @return the processed sheet name that complies with Excel's length restrictions
154+
*/
155+
private String sensitiveSheetName(String sheetName) {
156+
if (sheetName.length() > Workbook.MAX_SENSITIVE_SHEET_NAME_LEN) {
157+
String trimmedSheetName = sheetName.substring(0, Workbook.MAX_SENSITIVE_SHEET_NAME_LEN);
158+
159+
// we still need to warn about the trimming as the original sheet name won't be available
160+
// e.g. when referenced by formulas
161+
log.warn(
162+
"Sheet '{}' will be added with a trimmed name '{}' for MS Excel compliance.",
163+
sheetName,
164+
trimmedSheetName);
165+
return trimmedSheetName;
166+
}
167+
return sheetName;
168+
}
139169
}

fesod/src/test/java/org/apache/fesod/excel/writesheet/WriteSheetTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.fesod.excel.support.ExcelTypeEnum;
3232
import org.apache.fesod.excel.util.TestFileUtil;
3333
import org.apache.fesod.excel.write.metadata.WriteSheet;
34+
import org.apache.poi.ss.usermodel.Workbook;
3435
import org.junit.jupiter.api.Assertions;
3536
import org.junit.jupiter.api.Test;
3637

@@ -106,8 +107,8 @@ private Map<Integer, Integer> initSheetDataSizeList(List<Integer> sheetNoList) {
106107
}
107108

108109
private void testSheetOrderWithSheetName(ExcelTypeEnum excelTypeEnum) {
109-
List<String> sheetNameList = Arrays.asList("Sheet1", "Sheet2", "Sheet3");
110-
List<Integer> sheetNoList = Arrays.asList(0, 1, 2);
110+
List<String> sheetNameList = Arrays.asList("Sheet1", "Sheet2", "Sheet3", "Sheet111112222233333444445555566666");
111+
List<Integer> sheetNoList = Arrays.asList(0, 1, 2, 3);
111112

112113
Map<Integer, Integer> dataMap = initSheetDataSizeList(sheetNoList);
113114
File testFile = TestFileUtil.createNewFile("writesheet/write-sheet-order-name" + excelTypeEnum.getValue());
@@ -136,6 +137,16 @@ private void testSheetOrderWithSheetName(ExcelTypeEnum excelTypeEnum) {
136137
excelWriter.write(dataList(dataMap.get(sheetNo)), writeSheet);
137138
Assertions.assertEquals(
138139
sheetNo, excelWriter.writeContext().writeSheetHolder().getSheetNo());
140+
141+
sheetNo = 3;
142+
writeSheet =
143+
FastExcel.writerSheet(sheetNo, sheetNameList.get(sheetNo)).build();
144+
excelWriter.write(dataList(dataMap.get(sheetNo)), writeSheet);
145+
Assertions.assertEquals(
146+
sheetNameList.get(sheetNo).substring(0, Workbook.MAX_SENSITIVE_SHEET_NAME_LEN),
147+
excelWriter.writeContext().writeSheetHolder().getSheetName());
148+
Assertions.assertEquals(
149+
sheetNo, excelWriter.writeContext().writeSheetHolder().getSheetNo());
139150
}
140151

141152
for (int i = 0; i < sheetNoList.size(); i++) {

0 commit comments

Comments
 (0)