Skip to content

Commit

Permalink
Merge pull request #1493 from Mailaender/pcr-report-polish
Browse files Browse the repository at this point in the history
Fixed empty columns in PCR export breaking Excel auto-sort
  • Loading branch information
eselmeister authored Aug 24, 2023
2 parents 19335fa + c881c7e commit 3cb89ab
Showing 1 changed file with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
Expand Down Expand Up @@ -85,6 +88,9 @@ public IProcessingInfo<File> convert(File file, IPlate plate, IProgressMonitor m
}
sheet.createRow(sheet.getLastRowNum() + 1);
printValue(sheet, IPlate.DATE, headerDataMap);
for(String sampleSubset : sampleSubsets) {
removeEmptyColums(sheet, sampleSubset);
}
FileOutputStream fileout = new FileOutputStream(file);
workbook.write(fileout);
fileout.close();
Expand Down Expand Up @@ -216,7 +222,7 @@ private void printResultTable(IPlate plate, String targetSubset, XSSFSheet sheet
if(!empty) {
sheet.createRow(sheet.getLastRowNum() + 1);
}
for(int i = 0; i < 7; i++) {
for(int i = 0; i < 8; i++) {
sheet.autoSizeColumn(i);
}
sheet.setColumnWidth(1, 11 * 480);
Expand All @@ -241,4 +247,73 @@ private void printValue(XSSFSheet sheet, String key, Map<String, String> data) {
XSSFCell dataCell = row.createCell(0);
dataCell.setCellValue(data.getOrDefault(key, ""));
}

private void removeEmptyColums(XSSFSheet sheet, String targetSubset) {

Set<String> ignoredSubsets = PreferenceSupplier.getIgnoredSubsets();
if(ignoredSubsets.stream().anyMatch(targetSubset::equalsIgnoreCase)) {
return;
}
ChannelMappings channelMappings = PreferenceSupplier.getChannelMappings();
Set<Integer> channels = channelMappings.stream() //
.filter(c -> c.getSubset().equalsIgnoreCase(targetSubset)) //
.mapToInt(c -> c.getChannel()) //
.boxed().collect(Collectors.toSet()); //
if(channels.isEmpty()) {
return;
}
if(!channels.contains(1)) {
deleteColumn(sheet, 4);
}
}

private void deleteColumn(XSSFSheet sheet, int columnToDelete) {

int maxColumn = 0;
for(int r = 0; r < sheet.getLastRowNum() + 1; r++) {
Row row = sheet.getRow(r);
if(row == null) {
continue;
}
int lastColumn = row.getLastCellNum();
if(lastColumn > maxColumn) {
maxColumn = lastColumn;
}
if(lastColumn < columnToDelete) {
continue;
}
for(int x = columnToDelete + 1; x < lastColumn + 1; x++) {
Cell oldCell = row.getCell(x - 1);
if(oldCell != null) {
row.removeCell(oldCell);
}
Cell nextCell = row.getCell(x);
if(nextCell != null) {
Cell newCell = row.createCell(x - 1, nextCell.getCellType());
cloneCell(newCell, nextCell);
}
}
}
for(int c = 0; c < maxColumn; c++) {
sheet.setColumnWidth(c, sheet.getColumnWidth(c + 1));
}
}

private void cloneCell(Cell newCell, Cell oldCell) {

newCell.setCellComment(oldCell.getCellComment());
newCell.setCellStyle(oldCell.getCellStyle());
switch(newCell.getCellType()) {
case NUMERIC: {
newCell.setCellValue(oldCell.getNumericCellValue());
break;
}
case STRING: {
newCell.setCellValue(oldCell.getStringCellValue());
break;
}
default:
break;
}
}
}

0 comments on commit 3cb89ab

Please sign in to comment.