Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions fesod-sheet/src/main/java/org/apache/fesod/sheet/FesodSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.fesod.sheet.read.builder.ExcelReaderBuilder;
import org.apache.fesod.sheet.read.builder.ExcelReaderSheetBuilder;
import org.apache.fesod.sheet.read.listener.ReadListener;
Expand Down Expand Up @@ -330,4 +331,20 @@ public static ExcelReaderSheetBuilder readSheet(Integer sheetNo, String sheetNam
.sheetNameIfNotNull(sheetName)
.numRowsIfNotNull(numRows);
}

/**
* Build excel the 'readSheet' targeting specific column indexes.
*
* @param sheetNo Index of sheet, 0 base.
* @param columnIndexes Specific columns to read (e.g., [0, 2] for Column A and C).
* @return Excel sheet reader builder.
*/
public static ExcelReaderSheetBuilder readSheetWithColumns(
Integer sheetNo, String sheetName, Integer numRows, List<Integer> columnIndexes) {
return new ExcelReaderSheetBuilder()
.sheetNoIfNotNull(sheetNo)
.sheetNameIfNotNull(sheetName)
.numRowsIfNotNull(numRows)
.includeColumnIndexes(columnIndexes);
Comment thread
sapienza88 marked this conversation as resolved.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package org.apache.fesod.sheet.analysis.v07.handlers;

import java.math.BigDecimal;
import java.util.List;
import org.apache.fesod.common.util.BooleanUtils;
import org.apache.fesod.common.util.PositionUtils;
import org.apache.fesod.common.util.StringUtils;
Expand Down Expand Up @@ -81,6 +82,22 @@ public void startElement(XlsxReadContext xlsxReadContext, String name, Attribute
public void endElement(XlsxReadContext xlsxReadContext, String name) {
XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
ReadCellData<?> tempCellData = xlsxReadSheetHolder.getTempCellData();
int targetColumnIndex = 0;

List<Integer> includeColumnIndexes =
xlsxReadContext.readSheetHolder().getReadSheet().getColumnIndexes();

if (includeColumnIndexes == null) {
targetColumnIndex = xlsxReadSheetHolder.getColumnIndex();
} else {
// if it's a target column, rewrite the cell's internal index
targetColumnIndex = includeColumnIndexes.indexOf(xlsxReadSheetHolder.getColumnIndex());
if (targetColumnIndex < 0) {

return;
}
}

StringBuilder tempData = xlsxReadSheetHolder.getTempData();
String tempDataString = tempData.toString();
CellDataTypeEnum oldType = tempCellData.getType();
Expand Down Expand Up @@ -130,10 +147,9 @@ public void endElement(XlsxReadContext xlsxReadContext, String name) {
tempCellData.setStringValue(tempCellData.getStringValue().trim());
}
}

tempCellData.checkEmpty();
tempCellData.setRowIndex(xlsxReadSheetHolder.getRowIndex());
tempCellData.setColumnIndex(xlsxReadSheetHolder.getColumnIndex());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering the earlier logic, assigning tempCellData.setColumnIndex(targetColumnIndex) here might make the behavior more consistent. What do you think :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

targetColumnIndex is assigned in an if clause, so it would not make sense to assign here for all cases.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering the earlier logic, assigning tempCellData.setColumnIndex(targetColumnIndex) here might make the behavior more consistent.

Sorry for the confusion, my previous comment might not have been clear enough. Here is what I actually meant:

// ...

if (includeColumnIndexes == null) {
    targetColumnIndex = xlsxReadSheetHolder.getColumnIndex();
} else {
	// If it's a target column, rewrite the cell's internal index and pack it sequentially
    targetColumnIndex = includeColumnIndexes.indexOf(xlsxReadSheetHolder.getColumnIndex());
    if (targetColumnIndex < 0) {
        return;
    }
}

// ...
// line: 136 (old 154) 
tempCellData.setColumnIndex(targetColumnIndex);

Since we guarantee targetColumnIndex is resolved in both branches, placing setColumnIndex() outside the block maintains the exact same logic while avoiding code duplication. What do you think :)

@sapienza88 sapienza88 Jul 17, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is correct and more readable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bengbengbalabalabeng the refactoring has been implemented now, you can merge this PR as well as this related passing test: #945

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the previous version, when includeColumnIndexes != null, the assignment of tempCellData.setColumnIndex() was the overridden targetColumnIndex. Now, however, tempCellData.setColumnIndex() always uses the original xlsxReadSheetHolder.getColumnIndex() instead.

I want to confirm whether this change is intentional. Would you mind clarifying the expected behavior here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must have overlooked this, now it is changed as it was in the previous correct version.

xlsxReadSheetHolder.getCellMap().put(xlsxReadSheetHolder.getColumnIndex(), tempCellData);
tempCellData.setColumnIndex(targetColumnIndex);
xlsxReadSheetHolder.getCellMap().put(targetColumnIndex, tempCellData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ public ReadSheet build() {
return readSheet;
}

/**
* Specific columns to read
*
* @param columnIndexes
* @return
*/
public ExcelReaderSheetBuilder includeColumnIndexes(List<Integer> columnIndexes) {
readSheet.setColumnIndexes(columnIndexes);
return this;
}

public ExcelReaderSheetBuilder includeColumnIndexesIfNotNull(List<Integer> columnIndexes) {
if (Objects.nonNull(columnIndexes)) {
readSheet.setColumnIndexes(columnIndexes);
}
return this;
}
/**
* Sax read
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package org.apache.fesod.sheet.read.metadata;

import java.util.List;
import lombok.EqualsAndHashCode;

/**
Expand Down Expand Up @@ -54,6 +55,11 @@ public class ReadSheet extends ReadBasicParameter {
*/
public Integer numRows;

/**
* Specific columns to read (0-based indexes)
*/
private List<Integer> columnIndexes;

public ReadSheet() {}

public ReadSheet(Integer sheetNo) {
Expand All @@ -71,6 +77,13 @@ public ReadSheet(Integer sheetNo, String sheetName, Integer numRows) {
this.numRows = numRows;
}

public ReadSheet(Integer sheetNo, String sheetName, Integer numRows, List<Integer> numCols) {
this.sheetNo = sheetNo;
this.sheetName = sheetName;
this.numRows = numRows;
this.columnIndexes = numCols;
}
Comment thread
sapienza88 marked this conversation as resolved.

public Integer getSheetNo() {
return sheetNo;
}
Expand Down Expand Up @@ -111,6 +124,14 @@ public void setVeryHidden(boolean sheetVeryHidden) {
this.sheetVeryHidden = sheetVeryHidden;
}

public List<Integer> getColumnIndexes() {
return this.columnIndexes;
}

public void setColumnIndexes(List<Integer> columnIndexes) {
this.columnIndexes = columnIndexes;
}

public void copyBasicParameter(ReadSheet other) {
if (other == null) {
return;
Expand All @@ -126,6 +147,7 @@ public void copyBasicParameter(ReadSheet other) {
this.setNumRows(other.getNumRows());
this.setHidden(other.isHidden());
this.setVeryHidden(other.isVeryHidden());
this.setColumnIndexes(other.getColumnIndexes());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.fesod.sheet.read.builder.ExcelReaderBuilder;
import org.apache.fesod.sheet.read.builder.ExcelReaderSheetBuilder;
import org.apache.fesod.sheet.read.listener.ReadListener;
import org.apache.fesod.sheet.read.metadata.ReadSheet;
import org.apache.fesod.sheet.read.metadata.ReadWorkbook;
import org.apache.fesod.sheet.testkit.Tags;
import org.apache.fesod.sheet.write.builder.ExcelWriterBuilder;
Expand Down Expand Up @@ -247,4 +252,42 @@ void testReadSheet_withAllParams_shouldReturnBuilder() {
ExcelReaderSheetBuilder builder = FesodSheet.readSheet(0, "DataSheet", 100);
Assertions.assertNotNull(builder);
}

@Test
void testReadSheet_withColumnIndexes_shouldConfigureAll() {

List<List<String>> head = new ArrayList<>();
head.add(new ArrayList<>(Arrays.asList("ID")));
head.add(new ArrayList<>(Arrays.asList("Name")));
head.add(new ArrayList<>(Arrays.asList("Age")));
head.add(new ArrayList<>(Arrays.asList("Gender")));

List<List<Object>> dataList = new ArrayList<>();
dataList.add(Arrays.asList("1", "Alice", "30", "Female"));

FesodSheet.write(tempFile).head(head).sheet("Sheet1").doWrite(dataList);

List<Integer> targetColumns = Arrays.asList(0, 2);

ExcelReaderSheetBuilder builder = FesodSheet.readSheetWithColumns(0, "Sheet1", 100, targetColumns);
ReadSheet configuredSheet = builder.build();
List<Map<Integer, String>> readResults = FesodSheet.read(tempFile)
.sheet(0)
.includeColumnIndexes(targetColumns)
.doReadSync();

// builder tests
Assertions.assertNotNull(builder, "Builder should not be null");
Assertions.assertNotNull(configuredSheet, "The internal ReadSheet should be created");
Assertions.assertEquals(0, configuredSheet.getSheetNo());
Assertions.assertEquals("Sheet1", configuredSheet.getSheetName());
Assertions.assertEquals(100, configuredSheet.getNumRows());
Assertions.assertEquals(targetColumns, configuredSheet.getColumnIndexes());
// data related tests
Assertions.assertNotNull(readResults);
Map<Integer, String> parsedRow = readResults.get(0);
Assertions.assertEquals(2, parsedRow.size());
Assertions.assertEquals("1", parsedRow.get(0));
Assertions.assertEquals("30", parsedRow.get(1));
}
}
Loading