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) {
field_2_row_active_cell = row;
resetField6();
}
/**
@ -87,6 +88,14 @@ public final class SelectionRecord extends StandardRecord {
*/
public void setActiveCellCol(short 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
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("[SELECTION]\n");
sb.append(" .pane = ").append(HexDump.byteToHex(getPane())).append("\n");
sb.append(" .activecellrow = ").append(HexDump.shortToHex(getActiveCellRow())).append("\n");
sb.append(" .activecellcol = ").append(HexDump.shortToHex(getActiveCellCol())).append("\n");
sb.append(" .activecellref = ").append(HexDump.shortToHex(getActiveCellRef())).append("\n");
sb.append(" .numrefs = ").append(HexDump.shortToHex(field_6_refs.length)).append("\n");
sb.append("[/SELECTION]\n");
return sb.toString();
return "[SELECTION]\n" +
" .pane = " + HexDump.byteToHex(getPane()) + "\n" +
" .activecellrow = " + HexDump.shortToHex(getActiveCellRow()) + "\n" +
" .activecellcol = " + HexDump.shortToHex(getActiveCellCol()) + "\n" +
" .activecellref = " + HexDump.shortToHex(getActiveCellRef()) + "\n" +
" .numrefs = " + HexDump.shortToHex(field_6_refs.length) + "\n" +
"[/SELECTION]\n";
}
@Override
protected int getDataSize() {
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.POIXMLProperties;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.HSSFITestDataProvider;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
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.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.ITestDataProvider;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.ConditionalFormattingEvaluator;
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.usermodel.*;
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.CellReference;
import org.apache.poi.ss.util.CellUtil;
@ -3293,4 +3296,31 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
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()));
}
}