Skip to content

Commit

Permalink
Merge pull request #385 from liaochong/Hotfix/4.3.0.RC2
Browse files Browse the repository at this point in the history
Hotfix/4.3.0.rc2
  • Loading branch information
liaochong authored Dec 21, 2022
2 parents cb2ba3c + 0f1a8e8 commit 26c9b98
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.github.liaochong</groupId>
<artifactId>myexcel</artifactId>
<version>4.3.0.RC2</version>
<version>4.3.0.RC3</version>
<packaging>jar</packaging>

<name>myexcel</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.github.liaochong.myexcel.core;

import com.github.liaochong.myexcel.core.parser.ContentTypeEnum;
import com.github.liaochong.myexcel.core.parser.DropDownLists;
import com.github.liaochong.myexcel.core.parser.HtmlTableParser;
import com.github.liaochong.myexcel.core.parser.Td;
import com.github.liaochong.myexcel.core.parser.Tr;
Expand Down Expand Up @@ -564,29 +565,38 @@ private Cell setLink(Td td, Row currentRow, HyperlinkType hyperlinkType) {
}

private String setDropDownList(Td td, Sheet sheet, String content) {
if (content.length() > 250) {
throw new IllegalArgumentException("The total number of words in the drop-down list should not exceed 250.");
}
CellRangeAddressList addressList = new CellRangeAddressList(
if (content != null && !content.isEmpty()) {
CellRangeAddressList addressList = new CellRangeAddressList(
td.row, td.getRowBound(), td.col, td.getColBound());
DataValidationHelper dvHelper = sheet.getDataValidationHelper();
String[] list = content.split(",");
DataValidationConstraint dvConstraint = dvHelper.createExplicitListConstraint(list);
DataValidation validation = dvHelper.createValidation(
dvConstraint, addressList);
if (td.promptContainer != null) {
validation.createPromptBox(td.promptContainer.title, td.promptContainer.text);
validation.setShowPromptBox(true);
}
if (validation instanceof XSSFDataValidation) {
validation.setSuppressDropDownArrow(true);
validation.setShowErrorBox(true);
} else {
validation.setSuppressDropDownArrow(false);
}
sheet.addValidationData(validation);
if (list.length > 0) {
return list[0];
DataValidationHelper dvHelper = sheet.getDataValidationHelper();
String[] list;
DataValidation validation;
if (content.length() <= 250) {
list = content.split(",");
DataValidationConstraint dvConstraint = dvHelper.createExplicitListConstraint(list);
validation = dvHelper.createValidation(
dvConstraint, addressList);

} else {
DropDownLists.Index index = DropDownLists.getHiddenSheetIndex(content, workbook);
list = new String[]{index.firstLine};
validation = dvHelper.createValidation(dvHelper.createFormulaListConstraint(index.path), addressList);
}

if (td.promptContainer != null) {
validation.createPromptBox(td.promptContainer.title, td.promptContainer.text);
validation.setShowPromptBox(true);
}
if (validation instanceof XSSFDataValidation) {
validation.setSuppressDropDownArrow(true);
validation.setShowErrorBox(true);
} else {
validation.setSuppressDropDownArrow(false);
}
sheet.addValidationData(validation);
if (list.length > 0) {
return list[0];
}
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.github.liaochong.myexcel.core.parser;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public final class DropDownLists {

private static final Map<String, Index> INDEX = new ConcurrentHashMap<>();

private DropDownLists() {
}

public static Index getHiddenSheetIndex(String input, Workbook workbook) {
return INDEX.computeIfAbsent(input, hash -> createAndWriteHiddenSheet(input, workbook));
}

private static Index createAndWriteHiddenSheet(String input, Workbook workbook) {
String sheetName = "MyExcel_HiddenDat@List-0";
Sheet sheet = workbook.getSheet(sheetName);
if (sheet == null) {
sheet = workbook.createSheet(sheetName);
int index = workbook.getSheetIndex(sheet);
workbook.setSheetHidden(index, true);
}
int rowNum = sheet.getLastRowNum() + 1;
Row row = sheet.createRow(rowNum);
String[] list = input.split(",");
for (int i = 0; i < list.length; i++) {
Cell cell = row.createCell(i);
cell.setCellValue(list[i]);
}
int displayRowNum = rowNum + 1;
return new Index(list[0], rowNum, "'" + sheetName + "'!$" + displayRowNum + ":$" + displayRowNum);
}

public static class Index {

public String firstLine;

public int rowNum;

public String path;

public Index(String firstLine, int rowNum, String path) {
this.firstLine = firstLine;
this.rowNum = rowNum;
this.path = path;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Index)) return false;
Index index = (Index) o;
return rowNum == index.rowNum && Objects.equals(firstLine, index.firstLine) && Objects.equals(path, index.path);
}

@Override
public int hashCode() {
return Objects.hash(firstLine, rowNum, path);
}
}
}

0 comments on commit 26c9b98

Please sign in to comment.