Skip to content

Commit

Permalink
改进Excel SheetName过长时的修正处理,通过上下文环境中的sheetNameMapping来保存修正结果
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Aug 15, 2024
1 parent e621cfe commit 5dcf3c3
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@
/nop/report/demo/base/12-动态Sheet和动态列.xpt.xlsx
/nop/report/demo/base/13-复杂结构列展开.xpt.xlsx
/nop/report/demo/base/14-复杂结构行展开.xpt.xlsx
/nop/report/demo/base/15-兄弟节点同时展开.xpt.xlsx
/nop/report/demo/ext/report-with-params.xpt.xlsx
/nop/report/demo/pages/demo.page.yaml
/nop/report/demo/pages/report-with-params.page.yaml
Expand Down Expand Up @@ -618,7 +619,6 @@
/nop/report/xlib/xpt-rt.xlib
/nop/report/xlib/xpt.xlib
/nop/rpc/imp/api.imp.xml
/nop/rpc/imp/api.imp.xml.rej
/nop/rpc/imp/template.api.xlsx
/nop/rule/_module
/nop/rule/auth/_nop-rule-api.action-auth.xml
Expand Down Expand Up @@ -1051,7 +1051,6 @@
/nop/web/xlib/view-gen.xlib
/nop/web/xlib/view-gen/impl_GenFromMeta.xpl
/nop/web/xlib/web.xlib
/nop/web/xlib/web.xlib.rej
/nop/web/xlib/web/grid_crud.xpl
/nop/web/xlib/web/impl_GenForm.xpl
/nop/web/xlib/web/impl_GenGrid.xpl
Expand Down
2 changes: 2 additions & 0 deletions nop-excel/src/main/java/io/nop/excel/ExcelConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public interface ExcelConstants {

String VAR_FIELD_LABEL = "fieldLabel";

String VAR_SHEET_NAME_MAPPING = "sheetNameMapping";

/**
* By default, Microsoft Office Excel 2007 uses the Calibry font in font size 11
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void generateXml(IXNodeHandler out, IEvalContext context) {
genRows(out, sheet);
genMergeCells(out, sheet);

genLinks(out, sheet);
genLinks(out, sheet, context);

genPageMargins(out, sheet);

Expand Down Expand Up @@ -273,7 +273,7 @@ void genPageMargins(IXNodeHandler out, IExcelSheet sheet) {
* <hyperlink ref="B3" location="wf_status" display="dd" xr:uid="{1E0AC103-9F7F-465F-9C86-64335D36742B}"/>
* <hyperlink ref="B4" r:id="rId1" xr:uid="{600CE871-0B2C-47DB-A4AB-A39BD76FEFE9}"/>
*/
void genLinks(IXNodeHandler out, IExcelSheet sheet) {
void genLinks(IXNodeHandler out, IExcelSheet sheet, IEvalContext context) {
List<Link> links = new ArrayList<>();
sheet.getTable().forEachRealCell((cell, rowIndex, colIndex) -> {
String linkUrl = cell.getLinkUrl();
Expand All @@ -298,7 +298,8 @@ void genLinks(IXNodeHandler out, IExcelSheet sheet) {
links.forEach(link -> {
Map<String, ValueWithLocation> attrs = new LinkedHashMap<>();
attrs.put("ref", ValueWithLocation.of(null, CellPosition.toABString(link.rowIndex, link.colIndex)));
attrs.put("location", ValueWithLocation.of(null, normalizeLocation(link.location)));
attrs.put("location", ValueWithLocation.of(null, normalizeLocation(link.location,
(Map<String, String>) context.getEvalScope().getValue(ExcelConstants.VAR_SHEET_NAME_MAPPING))));
attrs.put("display", ValueWithLocation.of(null, link.text));
attrs.put("xr:id", ValueWithLocation.of(null, intToUUID(link.index)));
out.simpleNode(null, "hyperlink", attrs);
Expand All @@ -307,13 +308,16 @@ void genLinks(IXNodeHandler out, IExcelSheet sheet) {
}
}

private String normalizeLocation(String location) {
private String normalizeLocation(String location, Map<String, String> sheetNameMap) {
int pos = location.indexOf('!');
if (pos > 0) {
String abPos = location.substring(pos + 1);
try {
CellPosition.fromABString(abPos);
String sheetName = location.substring(0, pos);
String mappedName = sheetNameMap == null ? null : sheetNameMap.get(sheetName);
if (mappedName != null)
return mappedName + '!' + abPos;
return normalizeSheetName(sheetName, sheetIndex, workbook) + '!' + abPos;
} catch (Exception e) {
return location;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.nop.core.resource.IResource;
import io.nop.core.resource.impl.ByteArrayResource;
import io.nop.core.resource.impl.FileResource;
import io.nop.excel.ExcelConstants;
import io.nop.excel.model.ExcelImage;
import io.nop.excel.model.ExcelSheet;
import io.nop.excel.model.ExcelWorkbook;
Expand All @@ -35,7 +36,6 @@
import java.util.Map;

import static io.nop.ooxml.common.model.PackagingURIHelper.createPartName;
import static io.nop.ooxml.xlsx.output.XlsxGenHelper.normalizeSheetName;

public class ExcelTemplate extends AbstractOfficeTemplate {

Expand Down Expand Up @@ -79,9 +79,9 @@ public void generateToDir(File dir, IEvalContext context) {
GenState genState = new GenState();

if (sheetGenerator != null) {
sheetGenerator.generate(context, sheet -> {
sheetGenerator.generate(context,(sheet,ctx) -> {
int index = genState.nextSheetIndex++;
generateSheet(pkg, dir, index, sheet, context, genState);
generateSheet(pkg, dir, index, sheet, ctx, genState);
});
} else if (workbook != null) {
for (ExcelSheet sheet : workbook.getSheets()) {
Expand Down Expand Up @@ -110,7 +110,7 @@ private void generateSheet(ExcelOfficePackage pkg, File dir, int index, IExcelSh
OfficeRelsPart rels = pkg.makeRelsForPart(workbook);
String relPath = "worksheets/sheet" + sheetId + ".xml";
String relId = rels.addRelationship(XSSFRelation.WORKSHEET.getRelation(), relPath, null);
workbook.addSheet(relId, sheetId, normalizeSheetName(sheet.getName(), index, this.workbook));
workbook.addSheet(relId, sheetId, normalizeSheetName(sheet.getName(), index, context));

IResource resource = new FileResource(new File(dir, sheetPath));
ExcelSheetWriter writer = new ExcelSheetWriter(sheet, index == 0, index, this.workbook);
Expand All @@ -129,6 +129,16 @@ private void generateSheet(ExcelOfficePackage pkg, File dir, int index, IExcelSh
sheetRels.addRelationship(XSSFRelation.SHEET_COMMENTS.getRelation(), relCommentsPath, null);
}

private String normalizeSheetName(String sheetName, int index, IEvalContext context) {
Map<String, String> mapping = (Map<String, String>) context.getEvalScope().getValue(ExcelConstants.VAR_SHEET_NAME_MAPPING);
if (mapping != null) {
String mappedName = mapping.get(sheetName);
if (mappedName != null)
return mappedName;
}
return XlsxGenHelper.normalizeSheetName(sheetName, index, workbook);
}

private void generateDrawings(ExcelOfficePackage pkg, IExcelSheet sheet, String drawingRelId, IOfficePackagePart sheetPart, GenState genState) {
if (sheet.getImages() == null || sheet.getImages().isEmpty())
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import io.nop.core.context.IEvalContext;
import io.nop.excel.model.IExcelSheet;

import java.util.function.Consumer;
import java.util.function.BiConsumer;

public interface IExcelSheetGenerator {
void generate(IEvalContext context, Consumer<IExcelSheet> consumer);
void generate(IEvalContext context, BiConsumer<IExcelSheet, IEvalContext> consumer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@

public class XptConfigParseHelper {

public static void parseWorkbookModel(ExcelWorkbook workbook) {
public static XptWorkbookModel parseWorkbookModel(ExcelWorkbook workbook) {
ImportModel importModel = ImportModelHelper.getImportModel(XptConstants.XPT_IMP_MODEL_PATH);
parseWorkbookModel(workbook, importModel);
return parseWorkbookModel(workbook, importModel);
}

public static void parseWorkbookModel(ExcelWorkbook workbook, ImportModel importModel) {
public static XptWorkbookModel parseWorkbookModel(ExcelWorkbook workbook, ImportModel importModel) {
IEvalScope scope = XLang.newEvalScope();
scope.setLocalValue(ExcelConstants.VAR_WORKBOOK, workbook);

Expand All @@ -42,7 +42,7 @@ public static void parseWorkbookModel(ExcelWorkbook workbook, ImportModel import
if (namedStyles != null) {
workbookModel.prop_remove(XptConstants.PROP_NAMED_STYLES);
for (DynamicObject namedStyle : namedStyles) {
ExcelStyle style = getStyle(workbook,(String)namedStyle.prop_get(XptConstants.PROP_STYLE));
ExcelStyle style = getStyle(workbook, (String) namedStyle.prop_get(XptConstants.PROP_STYLE));
if (style != null) {
String id = (String) namedStyle.prop_get(XptConstants.PROP_ID);
if (!StringHelper.isEmpty(id)) {
Expand All @@ -53,8 +53,11 @@ public static void parseWorkbookModel(ExcelWorkbook workbook, ImportModel import
}
}
}
workbook.setModel(workbookModel);
}else {
workbookModel = new XptWorkbookModel();
}
workbook.setModel(workbookModel);
return workbookModel;
}

static ExcelStyle getStyle(ExcelWorkbook wk, String styleId){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class ExpandedSheetGenerator implements IExcelSheetGenerator {
Expand All @@ -54,7 +55,7 @@ public ExpandedSheetGenerator(ExcelWorkbook workbook) {
}

@Override
public void generate(IEvalContext ctx, Consumer<IExcelSheet> consumer) {
public void generate(IEvalContext ctx, BiConsumer<IExcelSheet, IEvalContext> consumer) {
XptRuntime xptRt = new XptRuntime(ctx.getEvalScope());
xptRt.setWorkbook(workbook);

Expand All @@ -79,7 +80,7 @@ public void generate(IEvalContext ctx, Consumer<IExcelSheet> consumer) {
});
}

void generateSheetLoop(ExcelSheet sheet, ExcelWorkbook workbook, IXptRuntime xptRt, Consumer<IExcelSheet> consumer) {
void generateSheetLoop(ExcelSheet sheet, ExcelWorkbook workbook, IXptRuntime xptRt, BiConsumer<IExcelSheet,IEvalContext> consumer) {
XptSheetModel sheetModel = sheet.getModel();
xptRt.getEvalScope().setLocalValue(null, XptConstants.VAR_SHEET_TPL, sheet);

Expand All @@ -106,7 +107,7 @@ void generateSheetLoop(ExcelSheet sheet, ExcelWorkbook workbook, IXptRuntime xpt
if (sheetModel != null)
runXpl(sheetModel.getAfterExpand(), xptRt);

consumer.accept(expandedSheet);
consumer.accept(expandedSheet,xptRt);
} finally {
xptRt.runSheetCleanup();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ITemplateOutput getRendererForExcel(ExcelWorkbook model, String renderTyp
throw new NopException(ERR_XPT_UNSUPPORTED_RENDER_TYPE)
.param(ARG_RENDER_TYPE, renderType);

return rendererFactory.buildRenderer(model, (ctx, action) -> model.getSheets().forEach(action));
return rendererFactory.buildRenderer(model, (ctx, action) -> model.getSheets().forEach(sheet-> action.accept(sheet,ctx)));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class HtmlRenderHelper {
public static String getHtml(ExcelWorkbook workbook, IExcelSheet sheet) {
return new HtmlReportRendererFactory.HtmlTemplate(workbook,
(ctx, action) -> action.accept(sheet)).generateText(DisabledEvalScope.INSTANCE);
(ctx, action) -> action.accept(sheet,ctx)).generateText(DisabledEvalScope.INSTANCE);
}

public static void dumpHtml(ExcelWorkbook workbook, IExcelSheet sheet, String fileName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ public void generateToWriter(Writer out, IEvalContext context) throws IOExceptio
MutableInt index = new MutableInt();

if (sheetGenerator != null) {
sheetGenerator.generate(context, sheet -> {
sheetGenerator.generate(context, (sheet,ctx) -> {
index.incrementAndGet();
String sheetId = reportId + "-sheet-" + index;
renderSheet(sheet, sheetId, scopeCssPrefix, out, context);
renderSheet(sheet, sheetId, scopeCssPrefix, out, ctx);
});
} else {
model.getSheets().forEach(sheet -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.nop.excel.model.ExcelWorkbook;
import io.nop.excel.model.XptCellModel;
import io.nop.excel.model.XptSheetModel;
import io.nop.excel.model.XptWorkbookModel;
import io.nop.excel.model.constants.XptExpandType;
import io.nop.report.core.XptConstants;
import io.nop.report.core.build.XptConfigParseHelper;
Expand All @@ -53,13 +54,26 @@
*/
public class ExcelTemplateToXptModelTransformer {
private final IEvalScope scope;
private final XLangCompileTool compileTool = XLang.newCompileTool().allowUnregisteredScopeVar(true);

public ExcelTemplateToXptModelTransformer(IEvalScope scope) {
this.scope = scope;
}

public void transform(ExcelWorkbook template, ImportModel model) {
XptConfigParseHelper.parseWorkbookModel(template);
XptWorkbookModel xptModel = XptConfigParseHelper.parseWorkbookModel(template);

XNode beforeExpandNode = XNode.fromValue(model.prop_get(XptConstants.EXT_PROP_XPT_BEFORE_EXPAND));
if (beforeExpandNode != null) {
IEvalAction action = compileTool.compileTagBodyWithSource(beforeExpandNode, XLangOutputMode.none);
xptModel.setBeforeExpand(action);
}

XNode afterExpandNode = XNode.fromValue(model.prop_get(XptConstants.EXT_PROP_XPT_AFTER_EXPAND));
if (afterExpandNode != null) {
IEvalAction action = compileTool.compileTagBodyWithSource(afterExpandNode, XLangOutputMode.none);
xptModel.setAfterExpand(action);
}

for (ExcelSheet sheet : template.getSheets()) {
ImportSheetModel sheetModel = getSheetModel(model, sheet);
Expand All @@ -76,7 +90,7 @@ public void transform(ExcelWorkbook template, ImportModel model) {

private void transformSheet(ExcelSheet sheet, ImportSheetModel sheetModel) {
new TreeTableDataParser(scope).parse(sheet.getName(), sheet.getTable(), sheetModel,
new BuildXptModelListener(sheet));
new BuildXptModelListener(sheet, compileTool));
}

private ImportSheetModel getSheetModel(ImportModel model, ExcelSheet sheet) {
Expand Down Expand Up @@ -104,7 +118,7 @@ static class BuildXptModelListener implements ITableDataEventListener {

private final List<ExcelCell> labelCells = new ArrayList<>();

private final XLangCompileTool compileTool = XLang.newCompileTool().allowUnregisteredScopeVar(true);
private final XLangCompileTool compileTool;

static class FieldRange {
int rowIndex;
Expand All @@ -131,8 +145,9 @@ public FieldRange(int rowIndex, int colIndex, int maxRowIndex,
}
}

public BuildXptModelListener(ExcelSheet sheet) {
public BuildXptModelListener(ExcelSheet sheet, XLangCompileTool compileTool) {
this.sheet = sheet;
this.compileTool = compileTool;
}

public ExcelTable getTable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ public void generateToStream(OutputStream os, IEvalContext context) throws IOExc


if (sheetGenerator != null) {
sheetGenerator.generate(context, sheet -> {
renderSheet(sheet, context);
});
sheetGenerator.generate(context, this::renderSheet);
} else {
model.getSheets().forEach(sheet -> {
renderSheet(sheet, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
templatePath="template.api.xlsx" xmlns:c="c" xmlns:xpt="xpt"
xdef="/nop/schema/api.xdef">

<xpt:beforeExpand><![CDATA[
import io.nop.ooxml.xlsx.output.XlsxGenHelper;
let index = 1;
const mapping = {}
for(let msg of entity.messages){
let name = XlsxGenHelper.normalizeSheetName(msg.name, index++, workbook);
mapping[msg.name] = name
}
assign("sheetNameMapping",mapping);
]]></xpt:beforeExpand>

<sheets>
<sheet name="目录" ignore="true"/>
<sheet name="配置">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public String getSource() {
return code;
}

public String toString() {
return code == null ? "" : code;
}

@Override
public Object invoke(IEvalContext ctx) {
return action.invoke(ctx);
Expand Down

0 comments on commit 5dcf3c3

Please sign in to comment.