Skip to content

Commit

Permalink
Pull duplicated code into method
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonOellerer committed May 16, 2024
1 parent 1fd7109 commit ce4bc2f
Showing 1 changed file with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,7 @@ private void copyColumnStyle(int rightMostColumn) {
@Override
public void addCell(Cell cell) {
logger.trace("Creating new cell {} {}", cell.getColumnIndex(), cell.getRow().getRowNum());
var newCell = currentRow.createCell(cell.getColumnIndex(), cell.getCellType());
newCell.setCellComment(cell.getCellComment());
newCell.setCellStyle(cellStyleMap.computeIfAbsent((int) cell.getCellStyle().getIndex(), i -> copyCellStyle(cell.getCellStyle())));
newCell.setHyperlink(cell.getHyperlink());
currentSheet.setColumnWidth(cell.getColumnIndex(), cell.getSheet().getColumnWidth(cell.getColumnIndex()));
var newCell = createNewCell(cell, 0);
switch (cell.getCellType()) {
case NUMERIC -> newCell.setCellValue(cell.getNumericCellValue());
case STRING -> newCell.setCellValue(cell.getStringCellValue());
Expand Down Expand Up @@ -242,33 +238,33 @@ public void addCell(Cell templateCell, String newCellText) {

@Override
public void addCell(Cell templateCell, double newCellValue) {
//todo merge with addCell(Cell, String, int)
logger.trace("Creating new cell {} {} with text {}",
logger.trace("Creating new cell {} {} with double value {}",
templateCell.getColumnIndex(), templateCell.getRow().getRowNum(), newCellValue);
var newCell = currentRow.createCell(templateCell.getColumnIndex(), templateCell.getCellType());
newCell.setCellComment(templateCell.getCellComment());
newCell.setCellStyle(cellStyleMap.computeIfAbsent((int) templateCell.getCellStyle().getIndex(), i -> copyCellStyle(templateCell.getCellStyle())));
newCell.setHyperlink(templateCell.getHyperlink());
currentSheet.setColumnWidth(templateCell.getColumnIndex(), templateCell.getSheet().getColumnWidth(templateCell.getColumnIndex()));
var newCell = createNewCell(templateCell, 0);
newCell.setCellValue(newCellValue);
}

@Override
public void addCell(Cell templateCell, String newCellText, int columnOffset) {
logger.trace("Creating new cell {} {} with text {}",
templateCell.getColumnIndex(), templateCell.getRow().getRowNum(), newCellText);
var newCell = currentRow.createCell(templateCell.getColumnIndex() + columnOffset, templateCell.getCellType());
newCell.setCellComment(templateCell.getCellComment());
newCell.setCellStyle(cellStyleMap.computeIfAbsent((int) templateCell.getCellStyle().getIndex(), i -> copyCellStyle(templateCell.getCellStyle())));
newCell.setHyperlink(templateCell.getHyperlink());
currentSheet.setColumnWidth(templateCell.getColumnIndex(), templateCell.getSheet().getColumnWidth(templateCell.getColumnIndex()));
var newCell = createNewCell(templateCell, columnOffset);
if (templateCell.getCellType() == CellType.FORMULA) {
newCell.setCellFormula(newCellText);
} else {
newCell.setCellValue(newCellText);
}
}

private Cell createNewCell(Cell templateCell, int columnOffset) {
var newCell = currentRow.createCell(templateCell.getColumnIndex() + columnOffset, templateCell.getCellType());
newCell.setCellComment(templateCell.getCellComment());
newCell.setCellStyle(cellStyleMap.computeIfAbsent((int) templateCell.getCellStyle().getIndex(), i -> copyCellStyle(templateCell.getCellStyle())));
newCell.setHyperlink(templateCell.getHyperlink());
currentSheet.setColumnWidth(templateCell.getColumnIndex(), templateCell.getSheet().getColumnWidth(templateCell.getColumnIndex()));
return newCell;
}

@Override
public void complete() throws IOException {
var outputStream = new BufferedOutputStream(Files.newOutputStream(path));
Expand Down

0 comments on commit ce4bc2f

Please sign in to comment.