protected Object[] token_3_arrayValues;
protected ArrayPtg() {
- //Required for clone methods
+ //Required for clone methods
}
public ArrayPtg(RecordInputStream in)
for (int x=0;x<getColumnCount();x++) {
for (int y=0;y<getRowCount();y++) {
Object o = token_3_arrayValues[getValueIndex(x, y)];
- buffer.append("[").append(x).append("][").append(y).append("] = ").append(o).append("\n");
+ buffer.append("[").append(x).append("][").append(y).append("] = ").append(o).append("\n");
}
}
return buffer.toString();
}
+ /**
+ * Note - (2D) array elements are stored column by column
+ * @return the index into the internal 1D array for the specified column and row
+ */
/* package */ int getValueIndex(int colIx, int rowIx) {
if(colIx < 0 || colIx >= token_1_columns) {
throw new IllegalArgumentException("Specified colIx (" + colIx
throw new IllegalArgumentException("Specified rowIx (" + rowIx
+ ") is outside the allowed range (0.." + (token_2_rows-1) + ")");
}
- return rowIx * token_1_columns + colIx;
+ return rowIx + token_2_rows * colIx;
}
public void writeBytes(byte[] data, int offset) {
return size;
}
- public String toFormulaString(HSSFWorkbook book)
- {
+ public String toFormulaString(HSSFWorkbook book) {
StringBuffer b = new StringBuffer();
b.append("{");
- for (int x=0;x<getColumnCount();x++) {
- if (x > 0) {
+ for (int x = 0; x < getColumnCount(); x++) {
+ if (x > 0) {
b.append(";");
}
- for (int y=0;y<getRowCount();y++) {
+ for (int y = 0; y < getRowCount(); y++) {
if (y > 0) {
b.append(",");
}
- Object o = token_3_arrayValues[getValueIndex(x, y)];
- b.append(getConstantText(o));
- }
- }
+ Object o = token_3_arrayValues[getValueIndex(x, y)];
+ b.append(getConstantText(o));
+ }
+ }
b.append("}");
return b.toString();
}
return "\"" + ((UnicodeString)o).getString() + "\"";
}
if (o instanceof Double) {
+ // TODO - numeric array elements need default Excel number formatting
return ((Double)o).toString();
}
if (o instanceof Boolean) {
}
public Object clone() {
- ArrayPtg ptg = new ArrayPtg();
- ptg.field_1_reserved = (byte[]) field_1_reserved.clone();
-
- ptg.token_1_columns = token_1_columns;
- ptg.token_2_rows = token_2_rows;
- ptg.token_3_arrayValues = (Object[]) token_3_arrayValues.clone();
- ptg.setClass(ptgClass);
- return ptg;
+ ArrayPtg ptg = new ArrayPtg();
+ ptg.field_1_reserved = (byte[]) field_1_reserved.clone();
+
+ ptg.token_1_columns = token_1_columns;
+ ptg.token_2_rows = token_2_rows;
+ ptg.token_3_arrayValues = (Object[]) token_3_arrayValues.clone();
+ ptg.setClass(ptgClass);
+ return ptg;
}
}
import java.util.Arrays;
+import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.TestcaseRecordInputStream;
import org.apache.poi.hssf.record.UnicodeString;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
}
/**
- * make sure constant elements are stored row by row
+ * Excel stores array elements column by column. This test makes sure POI does the same.
*/
public void testElementOrdering() {
ArrayPtg ptg = new ArrayPtgV(new TestcaseRecordInputStream(ArrayPtgV.sid, ENCODED_PTG_DATA));
assertEquals(2, ptg.getRowCount());
assertEquals(0, ptg.getValueIndex(0, 0));
- assertEquals(1, ptg.getValueIndex(1, 0));
- assertEquals(2, ptg.getValueIndex(2, 0));
- assertEquals(3, ptg.getValueIndex(0, 1));
- assertEquals(4, ptg.getValueIndex(1, 1));
+ assertEquals(2, ptg.getValueIndex(1, 0));
+ assertEquals(4, ptg.getValueIndex(2, 0));
+ assertEquals(1, ptg.getValueIndex(0, 1));
+ assertEquals(3, ptg.getValueIndex(1, 1));
assertEquals(5, ptg.getValueIndex(2, 1));
}
+
+ /**
+ * Test for a bug which was temporarily introduced by the fix for bug 42564.
+ * A spreadsheet was added to make the ordering clearer.
+ */
+ public void testElementOrderingInSpreadsheet() {
+ HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("ex42564-elementOrder.xls");
+
+ // The formula has an array with 3 rows and 5 column
+ String formula = wb.getSheetAt(0).getRow(0).getCell((short)0).getCellFormula();
+ // TODO - These number literals should not have '.0'. Excel has different number rendering rules
+
+ if (formula.equals("SUM({1.0,6.0,11.0;2.0,7.0,12.0;3.0,8.0,13.0;4.0,9.0,14.0;5.0,10.0,15.0})")) {
+ throw new AssertionFailedError("Identified bug 42564 b");
+ }
+ assertEquals("SUM({1.0,2.0,3.0;4.0,5.0,6.0;7.0,8.0,9.0;10.0,11.0,12.0;13.0,14.0,15.0})", formula);
+ }
}