Skip to content

Commit 0b012c4

Browse files
authored
[Fix-17281][alert] Fix the content exceed 32767 characters will throw exception in excel (#17283)
1 parent e77dd2a commit 0b012c4

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

  • dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src

dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/main/java/org/apache/dolphinscheduler/plugin/alert/email/ExcelUtils.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.dolphinscheduler.plugin.alert.email.exception.AlertEmailException;
2222

2323
import org.apache.commons.collections4.CollectionUtils;
24+
import org.apache.poi.ss.SpreadsheetVersion;
2425
import org.apache.poi.ss.usermodel.Cell;
2526
import org.apache.poi.ss.usermodel.CellStyle;
2627
import org.apache.poi.ss.usermodel.HorizontalAlignment;
@@ -108,7 +109,12 @@ public static void genExcelFile(String content, String title, String xlsFilePath
108109
if (values[j] instanceof Number) {
109110
cell1.setCellValue(((Number) values[j]).doubleValue());
110111
} else {
111-
cell1.setCellValue(String.valueOf(values[j]));
112+
String cellValue = String.valueOf(values[j]);
113+
int maxLen = SpreadsheetVersion.EXCEL2007.getMaxTextLength();
114+
if (cellValue.length() > maxLen) {
115+
cellValue = cellValue.substring(0, maxLen - 67) + "...(truncated)";
116+
}
117+
cell1.setCellValue(cellValue);
112118
}
113119
}
114120
}

dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/test/java/org/apache/dolphinscheduler/plugin/alert/email/ExcelUtilsTest.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@
1717

1818
package org.apache.dolphinscheduler.plugin.alert.email;
1919

20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
22+
import org.apache.poi.ss.SpreadsheetVersion;
23+
import org.apache.poi.ss.usermodel.Row;
24+
import org.apache.poi.ss.usermodel.Sheet;
25+
import org.apache.poi.ss.usermodel.Workbook;
26+
import org.apache.poi.ss.usermodel.WorkbookFactory;
27+
2028
import java.io.File;
29+
import java.io.FileInputStream;
2130
import java.nio.file.Path;
2231

2332
import org.junit.jupiter.api.Assertions;
@@ -33,7 +42,7 @@ public class ExcelUtilsTest {
3342
private String xlsFilePath;
3443

3544
@BeforeEach
36-
public void setUp() {
45+
void setUp() {
3746
xlsFilePath = testFolder.toString();
3847
}
3948

@@ -56,8 +65,9 @@ public void testGenExcelFile() {
5665
Assertions.assertTrue(xlsFile.exists());
5766

5867
// Invoke genExcelFile with incorrectContent, will cause RuntimeException
59-
Assertions.assertThrows(IllegalArgumentException.class,
60-
() -> ExcelUtils.genExcelFile(incorrectContent1, title, xlsFilePath));
68+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
69+
ExcelUtils.genExcelFile(incorrectContent1, title, xlsFilePath);
70+
});
6171

6272
}
6373

@@ -74,4 +84,32 @@ public void testGenExcelFileByCheckDir() {
7484
file.delete();
7585
Assertions.assertFalse(file.exists());
7686
}
87+
88+
@Test
89+
void testGenExcelFile_TruncateLongString() throws Exception {
90+
String title = "truncate_test";
91+
String longStrKey = "longStr";
92+
int maxLen = SpreadsheetVersion.EXCEL2007.getMaxTextLength();
93+
StringBuilder sb = new StringBuilder();
94+
for (int i = 0; i < maxLen + 100; i++) {
95+
sb.append('X');
96+
}
97+
String longValue = sb.toString();
98+
String content = "[{\"" + longStrKey + "\":\"" + longValue + "\"}]";
99+
100+
ExcelUtils.genExcelFile(content, title, xlsFilePath);
101+
102+
try (
103+
FileInputStream fis = new FileInputStream(xlsFilePath + "/" + title + ".xlsx");
104+
Workbook wb = WorkbookFactory.create(fis)) {
105+
106+
Sheet sheet = wb.getSheetAt(0);
107+
Row headerRow = sheet.getRow(0);
108+
Row dataRow = sheet.getRow(1);
109+
110+
assertEquals(longStrKey, headerRow.getCell(0).getStringCellValue());
111+
String expected = longValue.substring(0, maxLen - 67) + "...(truncated)";
112+
assertEquals(expected, dataRow.getCell(0).getStringCellValue());
113+
}
114+
}
77115
}

0 commit comments

Comments
 (0)