Fix setting active cell in .xls by populating field_6_refs whenever row/column changes, this fixes bug 61905

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1830115 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2018-04-25 20:14:07 +00:00
parent 2d6380833a
commit ad92fcca9e
2 changed files with 47 additions and 10 deletions

View File

@ -79,6 +79,7 @@ public final class SelectionRecord extends StandardRecord {
*/ */
public void setActiveCellRow(int row) { public void setActiveCellRow(int row) {
field_2_row_active_cell = row; field_2_row_active_cell = row;
resetField6();
} }
/** /**
@ -87,6 +88,14 @@ public final class SelectionRecord extends StandardRecord {
*/ */
public void setActiveCellCol(short col) { public void setActiveCellCol(short col) {
field_3_col_active_cell = col; field_3_col_active_cell = col;
resetField6();
}
private void resetField6() {
// this is necessary in Excel to actually make Workbook.setActiveCell() take effect
field_6_refs = new CellRangeAddress8Bit[] {
new CellRangeAddress8Bit(field_2_row_active_cell, field_2_row_active_cell, field_3_col_active_cell, field_3_col_active_cell),
};
} }
/** /**
@ -130,17 +139,15 @@ public final class SelectionRecord extends StandardRecord {
@Override @Override
public String toString() { public String toString() {
StringBuffer sb = new StringBuffer(); return "[SELECTION]\n" +
" .pane = " + HexDump.byteToHex(getPane()) + "\n" +
sb.append("[SELECTION]\n"); " .activecellrow = " + HexDump.shortToHex(getActiveCellRow()) + "\n" +
sb.append(" .pane = ").append(HexDump.byteToHex(getPane())).append("\n"); " .activecellcol = " + HexDump.shortToHex(getActiveCellCol()) + "\n" +
sb.append(" .activecellrow = ").append(HexDump.shortToHex(getActiveCellRow())).append("\n"); " .activecellref = " + HexDump.shortToHex(getActiveCellRef()) + "\n" +
sb.append(" .activecellcol = ").append(HexDump.shortToHex(getActiveCellCol())).append("\n"); " .numrefs = " + HexDump.shortToHex(field_6_refs.length) + "\n" +
sb.append(" .activecellref = ").append(HexDump.shortToHex(getActiveCellRef())).append("\n"); "[/SELECTION]\n";
sb.append(" .numrefs = ").append(HexDump.shortToHex(field_6_refs.length)).append("\n");
sb.append("[/SELECTION]\n");
return sb.toString();
} }
@Override @Override
protected int getDataSize() { protected int getDataSize() {
return 9 // 1 byte + 4 shorts return 9 // 1 byte + 4 shorts

View File

@ -50,6 +50,7 @@ import org.apache.poi.POIXMLDocumentPart.RelationPart;
import org.apache.poi.POIXMLException; import org.apache.poi.POIXMLException;
import org.apache.poi.POIXMLProperties; import org.apache.poi.POIXMLProperties;
import org.apache.poi.common.usermodel.HyperlinkType; import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.HSSFITestDataProvider;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator; import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@ -64,6 +65,7 @@ import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.util.ZipSecureFile; import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.ITestDataProvider;
import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.ConditionalFormattingEvaluator; import org.apache.poi.ss.formula.ConditionalFormattingEvaluator;
import org.apache.poi.ss.formula.EvaluationConditionalFormatRule; import org.apache.poi.ss.formula.EvaluationConditionalFormatRule;
@ -79,6 +81,7 @@ import org.apache.poi.ss.formula.functions.Function;
import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.CellUtil; import org.apache.poi.ss.util.CellUtil;
@ -3293,4 +3296,31 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
sheet.autoSizeColumn(i); sheet.autoSizeColumn(i);
} }
} }
@Test
public void test61905xlsx() throws IOException {
Workbook wb = new XSSFWorkbook();
checkActiveSheet(wb, XSSFITestDataProvider.instance);
wb.close();
}
@Test
public void test61905xls() throws IOException {
Workbook wb = new HSSFWorkbook();
checkActiveSheet(wb, HSSFITestDataProvider.instance);
wb.close();
}
private void checkActiveSheet(Workbook wb, ITestDataProvider instance) throws IOException {
Sheet sheet = wb.createSheet("new sheet");
sheet.setActiveCell(new CellAddress("E11"));
assertEquals("E11", sheet.getActiveCell().formatAsString());
Workbook wbBack = instance.writeOutAndReadBack(wb);
sheet = wbBack.getSheetAt(0);
assertEquals("E11", sheet.getActiveCell().formatAsString());
wbBack.close();
//wb.write(new FileOutputStream("c:/temp/61905." + instance.getStandardFileNameExtension()));
}
} }