Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed empty columns in PCR export breaking Excel auto-sort #1493

Merged
merged 2 commits into from
Aug 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
Loading