Skip to content

Commit

Permalink
改进ImportModelToExportModel
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jan 2, 2025
1 parent e0623bc commit c65132f
Show file tree
Hide file tree
Showing 9 changed files with 832 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ public List<Object> getValues() {
return options.stream().map(DictOptionBean::getValue).collect(Collectors.toList());
}

@JsonIgnore
public List<String> getStringValues(){
return options.stream().map(DictOptionBean::getStringValue).collect(Collectors.toList());
}

/**
* 将下拉选项的label转换为value-label形式
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import io.nop.api.core.annotations.data.DataBean;
Expand All @@ -22,6 +23,7 @@
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

import static io.nop.api.core.util.FreezeHelper.checkNotFrozen;

Expand Down Expand Up @@ -85,6 +87,13 @@ public void setValue(Object value) {
this.value = value;
}

@JsonIgnore
public String getStringValue() {
if (value == null)
return "";
return Objects.toString(value, "");
}

@PropMeta(propId = 3)
@JsonInclude(Include.NON_EMPTY)
public String getDescription() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ void calcBbox(TreeCell cell) {
calcBbox(child);
}

switch (cell.getChildPos()) {
TreeCellChildPosition pos = cell.getChildPos();
if (pos == null)
pos = TreeCellChildPosition.ver;

switch (pos) {
case right_hor:
case left_hor: {
int w = sumBboxWidth(children);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ public static Map<String, ImportFieldModel> toNameMap(List<ImportFieldModel> fie
return fieldNameMap;
}

public String getDisplayNameOrName() {
String displayName = getDisplayName();
if (displayName == null)
return getName();
return displayName;
}

@Override
public String getPropOrName() {
String prop = getProp();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.nop.excel.model;

import io.nop.api.core.beans.DictBean;
import io.nop.commons.util.StringHelper;
import io.nop.core.model.table.CellRange;
import io.nop.excel.model._gen._ExcelDataValidation;
Expand All @@ -17,6 +18,12 @@ public ExcelDataValidation() {

}

public static ExcelDataValidation buildFromDict(DictBean dict) {
ExcelDataValidation obj = new ExcelDataValidation();
obj.setListOptions(dict.getLabels());
return obj;
}

public List<CellRange> getRanges() {
if (ranges == null) {
ranges = CellRange.parseRangeList(getSqref());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,57 @@
package io.nop.ooxml.xlsx.imp;

import io.nop.api.core.beans.DictBean;
import io.nop.commons.cache.ICache;
import io.nop.commons.cache.MapCache;
import io.nop.core.dict.DictProvider;
import io.nop.core.lang.eval.IEvalScope;
import io.nop.core.model.table.CellPosition;
import io.nop.core.model.table.ICellView;
import io.nop.core.model.table.tree.TreeCell;
import io.nop.excel.imp.model.ImportFieldModel;
import io.nop.excel.imp.model.ImportModel;
import io.nop.excel.imp.model.ImportSheetModel;
import io.nop.excel.model.ExcelCell;
import io.nop.excel.model.ExcelDataValidation;
import io.nop.excel.model.ExcelSheet;
import io.nop.excel.model.ExcelTable;
import io.nop.excel.model.ExcelWorkbook;
import io.nop.ooxml.xlsx.XlsxConstants;
import io.nop.ooxml.xlsx.parse.ExcelWorkbookParser;
import io.nop.xlang.api.XLang;

import java.util.List;

import static io.nop.ooxml.xlsx.imp.TreeObjectLayout.STYLE_ID_AUTO_SEQ;
import static io.nop.ooxml.xlsx.imp.TreeObjectLayout.STYLE_ID_COL;
import static io.nop.ooxml.xlsx.imp.TreeObjectLayout.STYLE_ID_HEADER;
import static io.nop.ooxml.xlsx.imp.TreeObjectLayout.STYLE_ID_LABEL;
import static io.nop.ooxml.xlsx.imp.TreeObjectLayout.STYLE_ID_SEQ;
import static io.nop.ooxml.xlsx.imp.TreeObjectLayout.STYLE_ID_TITLE;
import static io.nop.ooxml.xlsx.imp.TreeObjectLayout.STYLE_ID_VALUE;

public class ImportModelToExportModel {
private ExcelWorkbook wk;
private ExcelSheet dataSheet;
private String labelStyle;
private String valueStyle;
private String titleStyle;
private ICache<Object, Object> cache = new MapCache<>("import", false);
private IEvalScope scope = XLang.newEvalScope();

public ImportModelToExportModel() {
wk = new ExcelWorkbookParser().parseFromVirtualPath(XlsxConstants.SIMPLE_DATA_TEMPLATE_PATH);
dataSheet = wk.getSheet(XlsxConstants.SHEET_DATA);
wk.clearSheets();

labelStyle = this.getCellStyleId(0, 0);
valueStyle = this.getCellStyleId(1, 0);
titleStyle = this.getCellStyleId(0, 1);
}

String getCellStyleId(int row, int col) {
ICellView cell = dataSheet.getTable().getCell(row, col);
return cell != null ? cell.getStyleId() : null;
}

public ExcelWorkbook build(ImportModel model) {
Expand All @@ -33,23 +66,66 @@ ExcelSheet buildSheet(ImportSheetModel sheetModel) {
ExcelSheet sheet = new ExcelSheet();
sheet.setName(sheetModel.getName());
sheet.setLocation(sheetModel.getLocation());

TreeObjectLayout layout = new TreeObjectLayout();
TreeCell rootCell = layout.init(sheetModel);

assignToTable(sheet.getTable(), rootCell.getChildren());

addValidation(rootCell, sheetModel);
return sheet;
}

private void layout(List<TreeCell> cells, List<ImportFieldModel> fields, boolean list) {
if (list) {
boolean complex = hasSubFields(fields);

} else {
private void assignToTable(ExcelTable table, List<TreeCell> cells) {
for (TreeCell cell : cells) {
if (cell.isVirtual()) {
if (cell.getChildren() != null)
assignToTable(table, cell.getChildren());
continue;
}

ExcelCell ec = (ExcelCell) table.makeCell(cell.getRowIndex(), cell.getColIndex());
if (STYLE_ID_HEADER.equals(cell.getStyleId())) {
ec.setStyleId(labelStyle);
ImportFieldModel field = (ImportFieldModel) cell.getValue();
ec.setValue(field.getDisplayNameOrName());
} else if (STYLE_ID_LABEL.equals(cell.getStyleId())) {
ImportFieldModel field = (ImportFieldModel) cell.getValue();
ec.setStyleId(labelStyle);
ec.setValue(field.getDisplayNameOrName());
} else if (STYLE_ID_VALUE.equals(cell.getStyleId())) {
ec.setStyleId(valueStyle);
} else if (STYLE_ID_TITLE.equals(cell.getStyleId())) {
ec.setStyleId(titleStyle);
ImportFieldModel field = (ImportFieldModel) cell.getValue();
ec.setValue(field.getDisplayNameOrName());
}else if(STYLE_ID_AUTO_SEQ.equals(cell.getStyleId())){
ec.setStyleId(labelStyle);
ec.setValue("序号");
}else if(STYLE_ID_SEQ.equals(cell.getStyleId())){
ec.setStyleId(valueStyle);
ec.setValue("1");
}
}
}

private boolean hasSubFields(List<ImportFieldModel> fields) {
for (ImportFieldModel field : fields) {
if (field.hasFields())
return true;
void addValidation(TreeCell cell, ImportSheetModel sheetModel) {
for (TreeCell child : cell.getChildren()) {
if (!STYLE_ID_COL.equals(child.getStyleId()))
continue;

TreeCell fieldCell = child.getChildren().get(1);
ImportFieldModel field = (ImportFieldModel) fieldCell.getValue();
if (field != null && field.getSchema() != null && field.getSchema().getDict() != null) {
String dictName = field.getSchema().getDict();
DictBean dict = DictProvider.instance().getDict(null, dictName, cache, scope);

ExcelDataValidation validation = ExcelDataValidation.buildFromDict(dict);
String start = CellPosition.toABString(fieldCell.getRowIndex(), fieldCell.getColIndex());
String end = CellPosition.toABString(CellPosition.MAX_ROWS, fieldCell.getColIndex());
validation.setSqref(start + ":" + end);
}
}
return false;
}

}
Loading

0 comments on commit c65132f

Please sign in to comment.