diff --git a/imixs-adapters-poi/src/main/java/org/imixs/workflow/poi/POIFindReplaceAdapter.java b/imixs-adapters-poi/src/main/java/org/imixs/workflow/poi/POIFindReplaceAdapter.java index 1e4578a..21994c7 100644 --- a/imixs-adapters-poi/src/main/java/org/imixs/workflow/poi/POIFindReplaceAdapter.java +++ b/imixs-adapters-poi/src/main/java/org/imixs/workflow/poi/POIFindReplaceAdapter.java @@ -177,12 +177,47 @@ public ItemCollection execute(ItemCollection document, ItemCollection event) } catch (IOException e) { throw new PluginException(POIFindReplaceAdapter.class.getSimpleName(), DOCUMENT_ERROR, - "doucment '" + fileName + "' not readable: " + e.getMessage()); + "document '" + fileName + "' not readable: " + e.getMessage()); } return document; } + /** + * This helper method processes all poi-update instructions for a given file + * attachment (.docx | .xlsx) + * + * @param workitem + * @param event + * @param targetName + * @throws PluginException + */ + public void processPOIUpdate(ItemCollection workitem, ItemCollection event, String targetName) + throws PluginException { + logger.fine("... loading poi configuration.."); + // Process POI instructions + // read the config + ItemCollection poiConfig; + + poiConfig = workflowService.evalWorkflowResult(event, "poi-update", workitem, + false); + + if (poiConfig == null || !poiConfig.hasItem("findreplace")) { + throw new PluginException(POIFindReplaceAdapter.class.getSimpleName(), CONFIG_ERROR, + "missing poi configuration"); + } + List replaceDevList = poiConfig.getItemValue("findreplace"); + String eval = poiConfig.getItemValueString("eval"); + + logger.info("... update template with normal poi information.."); + try { + this.updateFileData(workitem.getFileData(targetName), workitem, replaceDevList, eval); + } catch (IOException e) { + throw new PluginException(POIFindReplaceAdapter.class.getSimpleName(), DOCUMENT_ERROR, + "document '" + targetName + "' not readable: " + e.getMessage()); + } + } + /** * This helper method updates the content of a given FileData object with a * replaceDevList diff --git a/imixs-adapters-poi/src/main/java/org/imixs/workflow/poi/POIUtil.java b/imixs-adapters-poi/src/main/java/org/imixs/workflow/poi/POIUtil.java index 503d211..ab8ee6c 100644 --- a/imixs-adapters-poi/src/main/java/org/imixs/workflow/poi/POIUtil.java +++ b/imixs-adapters-poi/src/main/java/org/imixs/workflow/poi/POIUtil.java @@ -1,5 +1,7 @@ package org.imixs.workflow.poi; +import java.util.HashMap; +import java.util.Map; import java.util.logging.Logger; import org.apache.poi.ss.usermodel.CellCopyPolicy; @@ -49,20 +51,81 @@ public static void insertRows(XSSFSheet sheet, String cellReference, int numberO } /** - * Insert n columns in a XSSFSheet from a given cell reference. + * Inserts one column in a XSSFSheet before a given start column (beginning + * by 0). + * The end column defines the last column in the sheet to be shifted (which is + * the last column in the sheet). + * + * @param sheet + * @param startColumn - column before the new column should be inserted + * @param endColumn - last column in the sheet + */ + public static void insertColumn(XSSFSheet sheet, int startColumn, int endColumn) { + + logger.info("---- start Column=" + startColumn); + + // // Find the last column by checking all rows + int lastColumn = -1; + for (Row row : sheet) { + if (row.getLastCellNum() > lastColumn) { + lastColumn = row.getLastCellNum() - 1; + } + } + + // Store original column widths before shifting + Map columnWidths = new HashMap<>(); + for (int i = startColumn; i <= endColumn; i++) { + columnWidths.put(i, sheet.getColumnWidth(i)); + logger.info(" column width " + i + " = " + sheet.getColumnWidth(i)); + } + + sheet.shiftColumns(startColumn, lastColumn, 1); + + // Restore column widths for shifted columns + for (int i = startColumn + 1; i <= endColumn + 1; i++) { + Integer width = columnWidths.get(i - 1); + logger.info(" restore width " + i + " = " + width); + sheet.setColumnWidth(i, width); + } + + // Create CellCopyPolicy + CellCopyPolicy copyPolicy = new CellCopyPolicy.Builder() + .cellStyle(true) + .cellFormula(true) + .mergedRegions(false) // Merging is already handled by shiftColumns + .build(); + + for (Row row : sheet) { + if (row != null) { + XSSFCell refCell = ((XSSFRow) row).getCell(startColumn + 1); + if (refCell != null) { + XSSFCell newCell = ((XSSFRow) row).createCell(startColumn); + newCell.copyCellFrom(refCell, copyPolicy); + } + } + } + } + + /** + * Insert a columns in a XSSFSheet from a given start column (beginning by 0). + * The end column defines the last column in the sheet to be shifted. * * @param sheet * @param cellReference * @param numberOfColumns */ - public static void insertColumns(XSSFSheet sheet, String cellReference, int numberOfColumns) { + public static void insertColumnSuperTolll(XSSFSheet sheet, int startColumn, int endColumn) { + // Convert cell reference to column index - CellReference refCellStart = new CellReference(cellReference); - int startColumn = refCellStart.getCol(); + // CellReference refCellStart = new CellReference(cellReferenceStart); + // CellReference refCellEnd = new CellReference(cellReferenceEnd); + // int startColumn = refCellStart.getCol(); + // // int lastColumn = refCellEnd.getCol(); - // logger.info("---- start Column=" + startColumn); + logger.info("---- start Column=" + startColumn); - // Find the last column by checking all rows + // lastColumn = 16000; + // // Find the last column by checking all rows int lastColumn = -1; for (Row row : sheet) { if (row.getLastCellNum() > lastColumn) { @@ -70,11 +133,12 @@ public static void insertColumns(XSSFSheet sheet, String cellReference, int numb } } + // lastColumn = 100; // Only proceed if there are columns to shift - if (lastColumn >= startColumn) { - // Shift columns to the right - sheet.shiftColumns(startColumn, lastColumn, numberOfColumns); - } + // if (lastColumn >= startColumn) { + // Shift columns to the right + sheet.shiftColumns(startColumn, lastColumn, 1); + // } // Create CellCopyPolicy CellCopyPolicy copyPolicy = new CellCopyPolicy.Builder() @@ -84,28 +148,41 @@ public static void insertColumns(XSSFSheet sheet, String cellReference, int numb .build(); // Copy formatting from reference column to new columns + // for (Row row : sheet) { + // if (row != null) { + // XSSFCell refCell = ((XSSFRow) row).getCell(startColumn + 1); + // if (refCell != null) { + // for (int i = startColumn; i < startColumn + 1; i++) { + // XSSFCell newCell = ((XSSFRow) row).createCell(i); + // newCell.copyCellFrom(refCell, copyPolicy); + // } + // } + // } + // } + for (Row row : sheet) { if (row != null) { - XSSFCell refCell = ((XSSFRow) row).getCell(startColumn + numberOfColumns); + XSSFCell refCell = ((XSSFRow) row).getCell(startColumn + 1); if (refCell != null) { - for (int i = startColumn; i < startColumn + numberOfColumns; i++) { - XSSFCell newCell = ((XSSFRow) row).createCell(i); - newCell.copyCellFrom(refCell, copyPolicy); - } + XSSFCell newCell = ((XSSFRow) row).createCell(startColumn); + newCell.copyCellFrom(refCell, copyPolicy); } } } } - public static void xxxinsertCells(XSSFSheet sheet, String cellReference, int numberOfColumns) { + public static void insertColumnGehtNoch(XSSFSheet sheet, int startColumn, int endColumn) { // Convert cell reference to column index - CellReference refCellStart = new CellReference(cellReference); - int startColumn = refCellStart.getCol(); + // CellReference refCellStart = new CellReference(cellReferenceStart); + // CellReference refCellEnd = new CellReference(cellReferenceEnd); + // int startColumn = refCellStart.getCol(); + // // int lastColumn = refCellEnd.getCol(); logger.info("---- start Column=" + startColumn); - // Find the last column by checking all rows + // lastColumn = 16000; + // // Find the last column by checking all rows int lastColumn = -1; for (Row row : sheet) { if (row.getLastCellNum() > lastColumn) { @@ -113,25 +190,32 @@ public static void xxxinsertCells(XSSFSheet sheet, String cellReference, int num } } + // lastColumn = 100; // Only proceed if there are columns to shift - if (lastColumn >= startColumn) { - // Shift columns to the right - sheet.shiftColumns(startColumn, lastColumn, numberOfColumns); - } + // if (lastColumn >= startColumn) { + // Shift columns to the right + sheet.shiftColumns(startColumn, lastColumn, 1); + // } + + // Create CellCopyPolicy + CellCopyPolicy copyPolicy = new CellCopyPolicy.Builder() + .cellStyle(true) + .cellFormula(true) + .mergedRegions(false) // Merging is already handled by shiftColumns + .build(); // Copy formatting from reference column to new columns for (Row row : sheet) { if (row != null) { - XSSFCell refCell = ((XSSFRow) row).getCell(startColumn + numberOfColumns); - for (int i = startColumn; i < startColumn + numberOfColumns; i++) { - XSSFCell newCell = ((XSSFRow) row).createCell(i); - if (refCell != null && refCell.getCellStyle() != null) { - newCell.setCellStyle(refCell.getCellStyle()); + XSSFCell refCell = ((XSSFRow) row).getCell(startColumn + 1); + if (refCell != null) { + for (int i = startColumn; i < startColumn + 1; i++) { + XSSFCell newCell = ((XSSFRow) row).createCell(i); + newCell.copyCellFrom(refCell, copyPolicy); } } } } - } } \ No newline at end of file