Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
Issue #165
  • Loading branch information
rsoika committed Nov 17, 2024
1 parent 08cd405 commit 642d407
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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
Expand Down
142 changes: 113 additions & 29 deletions imixs-adapters-poi/src/main/java/org/imixs/workflow/poi/POIUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -49,32 +51,94 @@ 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<Integer, Integer> 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) {
lastColumn = row.getLastCellNum() - 1;
}
}

// 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()
Expand All @@ -84,54 +148,74 @@ 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) {
lastColumn = row.getLastCellNum() - 1;
}
}

// 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);
}
}
}
}

}

}

0 comments on commit 642d407

Please sign in to comment.