improve some exception texts, add comment for missing Ptg for SxName git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1851210 13f79535-47bb-0310-9956-ffa450edef68tags/REL_4_1_0
import org.apache.poi.util.IOUtils; | import org.apache.poi.util.IOUtils; | ||||
import org.apache.xmlbeans.XmlException; | import org.apache.xmlbeans.XmlException; | ||||
/** | |||||
* Base class with things that can be run for any supported file handler | |||||
* in the integration tests, mostly text-extraction related at the moment. | |||||
*/ | |||||
public abstract class AbstractFileHandler implements FileHandler { | public abstract class AbstractFileHandler implements FileHandler { | ||||
public static final Set<String> EXPECTED_EXTRACTOR_FAILURES = new HashSet<>(); | public static final Set<String> EXPECTED_EXTRACTOR_FAILURES = new HashSet<>(); | ||||
static { | static { |
try { | try { | ||||
System.setOut(new PrintStream(new OutputStream() { | System.setOut(new PrintStream(new OutputStream() { | ||||
@Override | @Override | ||||
public void write(int b) throws IOException { | |||||
public void write(int b) { | |||||
} | } | ||||
})); | })); | ||||
import org.apache.poi.ss.usermodel.CellStyle; | import org.apache.poi.ss.usermodel.CellStyle; | ||||
import org.apache.poi.ss.usermodel.CellType; | import org.apache.poi.ss.usermodel.CellType; | ||||
import org.apache.poi.ss.usermodel.Row; | import org.apache.poi.ss.usermodel.Row; | ||||
import org.apache.poi.ss.usermodel.helpers.RowShifter; | |||||
import org.apache.poi.util.Configurator; | import org.apache.poi.util.Configurator; | ||||
import org.apache.poi.util.LocaleUtil; | |||||
/** | /** | ||||
* High level representation of a row of a spreadsheet. | * High level representation of a row of a spreadsheet. | ||||
*/ | */ | ||||
@Override | @Override | ||||
public void shiftCellsRight(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) { | public void shiftCellsRight(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) { | ||||
if(step < 0) | |||||
throw new IllegalArgumentException("Shifting step may not be negative "); | |||||
if(firstShiftColumnIndex > lastShiftColumnIndex) | |||||
throw new IllegalArgumentException(String.format(LocaleUtil.getUserLocale(), | |||||
"Incorrect shifting range : %d-%d", firstShiftColumnIndex, lastShiftColumnIndex)); | |||||
RowShifter.validateShiftParameters(firstShiftColumnIndex, lastShiftColumnIndex, step); | |||||
if(lastShiftColumnIndex + step + 1> cells.length) | if(lastShiftColumnIndex + step + 1> cells.length) | ||||
extend(lastShiftColumnIndex + step + 1); | extend(lastShiftColumnIndex + step + 1); | ||||
for (int columnIndex = lastShiftColumnIndex; columnIndex >= firstShiftColumnIndex; columnIndex--){ // process cells backwards, because of shifting | for (int columnIndex = lastShiftColumnIndex; columnIndex >= firstShiftColumnIndex; columnIndex--){ // process cells backwards, because of shifting | ||||
cells = new HSSFCell[newLenght]; | cells = new HSSFCell[newLenght]; | ||||
System.arraycopy(temp, 0, cells, 0, temp.length); | System.arraycopy(temp, 0, cells, 0, temp.length); | ||||
} | } | ||||
/** | /** | ||||
* Shifts column range [firstShiftColumnIndex-lastShiftColumnIndex] step places to the left. | * Shifts column range [firstShiftColumnIndex-lastShiftColumnIndex] step places to the left. | ||||
* @param firstShiftColumnIndex the column to start shifting | * @param firstShiftColumnIndex the column to start shifting | ||||
*/ | */ | ||||
@Override | @Override | ||||
public void shiftCellsLeft(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) { | public void shiftCellsLeft(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) { | ||||
if(step < 0) | |||||
throw new IllegalArgumentException("Shifting step may not be negative "); | |||||
if(firstShiftColumnIndex > lastShiftColumnIndex) | |||||
throw new IllegalArgumentException(String.format(LocaleUtil.getUserLocale(), | |||||
"Incorrect shifting range : %d-%d", firstShiftColumnIndex, lastShiftColumnIndex)); | |||||
if(firstShiftColumnIndex - step < 0) | |||||
throw new IllegalStateException("Column index less than zero : " + (Integer.valueOf(firstShiftColumnIndex + step)).toString()); | |||||
RowShifter.validateShiftLeftParameters(firstShiftColumnIndex, lastShiftColumnIndex, step); | |||||
for (int columnIndex = firstShiftColumnIndex; columnIndex <= lastShiftColumnIndex; columnIndex++){ | for (int columnIndex = firstShiftColumnIndex; columnIndex <= lastShiftColumnIndex; columnIndex++){ | ||||
HSSFCell cell = getCell(columnIndex); | HSSFCell cell = getCell(columnIndex); | ||||
if(cell != null){ | if(cell != null){ |
import javax.xml.bind.JAXBElement; | import javax.xml.bind.JAXBElement; | ||||
import javax.xml.bind.JAXBException; | import javax.xml.bind.JAXBException; | ||||
import javax.xml.bind.Unmarshaller; | import javax.xml.bind.Unmarshaller; | ||||
import javax.xml.stream.EventFilter; | |||||
import javax.xml.stream.XMLInputFactory; | import javax.xml.stream.XMLInputFactory; | ||||
import javax.xml.stream.XMLStreamConstants; | import javax.xml.stream.XMLStreamConstants; | ||||
import javax.xml.stream.XMLStreamException; | import javax.xml.stream.XMLStreamException; | ||||
import javax.xml.stream.XMLStreamReader; | import javax.xml.stream.XMLStreamReader; | ||||
import javax.xml.stream.events.XMLEvent; | |||||
import javax.xml.transform.stream.StreamSource; | import javax.xml.transform.stream.StreamSource; | ||||
import org.apache.poi.sl.draw.binding.CTCustomGeometry2D; | import org.apache.poi.sl.draw.binding.CTCustomGeometry2D; | ||||
@SuppressWarnings("unused") | @SuppressWarnings("unused") | ||||
public void init(InputStream is) throws XMLStreamException, JAXBException { | public void init(InputStream is) throws XMLStreamException, JAXBException { | ||||
// StAX: | |||||
EventFilter startElementFilter = new EventFilter() { | |||||
@Override | |||||
public boolean accept(XMLEvent event) { | |||||
return event.isStartElement(); | |||||
} | |||||
}; | |||||
XMLInputFactory staxFactory = StaxHelper.newXMLInputFactory(); | XMLInputFactory staxFactory = StaxHelper.newXMLInputFactory(); | ||||
XMLStreamReader streamReader = staxFactory.createXMLStreamReader(new StreamSource(is)); | XMLStreamReader streamReader = staxFactory.createXMLStreamReader(new StreamSource(is)); | ||||
try { | try { | ||||
// in case of failure | // in case of failure | ||||
PresetGeometries lInst = new PresetGeometries(); | PresetGeometries lInst = new PresetGeometries(); | ||||
try { | try { | ||||
InputStream is = PresetGeometries.class. | |||||
getResourceAsStream("presetShapeDefinitions.xml"); | |||||
try { | |||||
try (InputStream is = PresetGeometries.class. | |||||
getResourceAsStream("presetShapeDefinitions.xml")) { | |||||
lInst.init(is); | lInst.init(is); | ||||
} finally { | |||||
is.close(); | |||||
} | } | ||||
} catch (Exception e){ | } catch (Exception e){ | ||||
throw new RuntimeException(e); | throw new RuntimeException(e); |
final int sheetIndex = -1; //don't care? | final int sheetIndex = -1; //don't care? | ||||
Ptg[] arr = FormulaParser.parse(tableText, workbook, FormulaType.CELL, sheetIndex, rowIndex); | Ptg[] arr = FormulaParser.parse(tableText, workbook, FormulaType.CELL, sheetIndex, rowIndex); | ||||
if (arr.length != 1 || !(arr[0] instanceof Area3DPxg) ) { | if (arr.length != 1 || !(arr[0] instanceof Area3DPxg) ) { | ||||
throw new IllegalStateException("Illegal structured reference"); | |||||
throw new IllegalStateException("Illegal structured reference, had length: " + arr.length); | |||||
} | } | ||||
return (Area3DPxg) arr[0]; | return (Area3DPxg) arr[0]; | ||||
} | } | ||||
/** Read New Character From Input Stream */ | /** Read New Character From Input Stream */ | ||||
private void GetChar() { | private void GetChar() { | ||||
// The intersection operator is a space. We track whether the run of | // The intersection operator is a space. We track whether the run of | ||||
// whitespace preceeding "look" counts as an intersection operator. | |||||
// whitespace preceding "look" counts as an intersection operator. | |||||
if (IsWhite(look)) { | if (IsWhite(look)) { | ||||
if (look == ' ') { | if (look == ' ') { | ||||
_inIntersection = true; | _inIntersection = true; | ||||
// Check to see if we've walked off the end of the string. | // Check to see if we've walked off the end of the string. | ||||
if (_pointer > _formulaLength) { | if (_pointer > _formulaLength) { | ||||
throw new RuntimeException("too far"); | |||||
throw new RuntimeException("Parsed past the end of the formula, pos: " + _pointer + | |||||
", length: " + _formulaLength + ", formula: " + _formulaString); | |||||
} | } | ||||
if (_pointer < _formulaLength) { | if (_pointer < _formulaLength) { | ||||
look=_formulaString.codePointAt(_pointer); | look=_formulaString.codePointAt(_pointer); | ||||
resetPointer(savePtr1); | resetPointer(savePtr1); | ||||
break; | break; | ||||
} | } | ||||
if (specName.equals(specAll)) { | |||||
isAllSpec = true; | |||||
} else if (specName.equals(specData)) { | |||||
isDataSpec = true; | |||||
} else if (specName.equals(specHeaders)) { | |||||
isHeadersSpec = true; | |||||
} else if (specName.equals(specThisRow)) { | |||||
isThisRowSpec = true; | |||||
} else if (specName.equals(specTotals)) { | |||||
isTotalsSpec = true; | |||||
} else { | |||||
throw new FormulaParseException("Unknown special quantifier "+ specName); | |||||
switch (specName) { | |||||
case specAll: | |||||
isAllSpec = true; | |||||
break; | |||||
case specData: | |||||
isDataSpec = true; | |||||
break; | |||||
case specHeaders: | |||||
isHeadersSpec = true; | |||||
break; | |||||
case specThisRow: | |||||
isThisRowSpec = true; | |||||
break; | |||||
case specTotals: | |||||
isTotalsSpec = true; | |||||
break; | |||||
default: | |||||
throw new FormulaParseException("Unknown special quantifier " + specName); | |||||
} | } | ||||
nSpecQuantifiers++; | nSpecQuantifiers++; | ||||
if (look == ','){ | if (look == ','){ | ||||
} else { | } else { | ||||
nColQuantifiers++; | nColQuantifiers++; | ||||
if (look == ','){ | if (look == ','){ | ||||
throw new FormulaParseException("The formula "+ _formulaString + "is illegal: you should not use ',' with column quantifiers"); | |||||
throw new FormulaParseException("The formula "+ _formulaString + " is illegal: you should not use ',' with column quantifiers"); | |||||
} else if (look == ':') { | } else if (look == ':') { | ||||
GetChar(); | GetChar(); | ||||
endColumnName = parseAsColumnQuantifier(); | endColumnName = parseAsColumnQuantifier(); | ||||
nColQuantifiers++; | nColQuantifiers++; | ||||
if (endColumnName == null) { | if (endColumnName == null) { | ||||
throw new FormulaParseException("The formula "+ _formulaString + "is illegal: the string after ':' must be column quantifier"); | |||||
throw new FormulaParseException("The formula "+ _formulaString + " is illegal: the string after ':' must be column quantifier"); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
resetPointer(savePtr0); | resetPointer(savePtr0); | ||||
String name = parseAsSpecialQuantifier(); | String name = parseAsSpecialQuantifier(); | ||||
if (name!=null) { | if (name!=null) { | ||||
if (name.equals(specAll)) { | |||||
isAllSpec = true; | |||||
} else if (name.equals(specData)) { | |||||
isDataSpec = true; | |||||
} else if (name.equals(specHeaders)) { | |||||
isHeadersSpec = true; | |||||
} else if (name.equals(specThisRow)) { | |||||
isThisRowSpec = true; | |||||
} else if (name.equals(specTotals)) { | |||||
isTotalsSpec = true; | |||||
} else { | |||||
throw new FormulaParseException("Unknown special quantifier "+ name); | |||||
switch (name) { | |||||
case specAll: | |||||
isAllSpec = true; | |||||
break; | |||||
case specData: | |||||
isDataSpec = true; | |||||
break; | |||||
case specHeaders: | |||||
isHeadersSpec = true; | |||||
break; | |||||
case specThisRow: | |||||
isThisRowSpec = true; | |||||
break; | |||||
case specTotals: | |||||
isTotalsSpec = true; | |||||
break; | |||||
default: | |||||
throw new FormulaParseException("Unknown special quantifier " + name); | |||||
} | } | ||||
nSpecQuantifiers++; | nSpecQuantifiers++; | ||||
} else { | } else { | ||||
/** | /** | ||||
* Adds a name (named range or user defined function) to underlying workbook's names table | * Adds a name (named range or user defined function) to underlying workbook's names table | ||||
* @param functionName | |||||
*/ | */ | ||||
private void addName(String functionName) { | private void addName(String functionName) { | ||||
final Name name = _book.createName(); | final Name name = _book.createName(); | ||||
boolean hasUnions = false; | boolean hasUnions = false; | ||||
while (true) { | while (true) { | ||||
SkipWhite(); | SkipWhite(); | ||||
switch(look) { | |||||
case ',': | |||||
GetChar(); | |||||
hasUnions = true; | |||||
ParseNode other = intersectionExpression(); | |||||
result = new ParseNode(UnionPtg.instance, result, other); | |||||
continue; | |||||
if (look == ',') { | |||||
GetChar(); | |||||
hasUnions = true; | |||||
ParseNode other = intersectionExpression(); | |||||
result = new ParseNode(UnionPtg.instance, result, other); | |||||
continue; | |||||
} | } | ||||
if (hasUnions) { | if (hasUnions) { | ||||
return augmentWithMemPtg(result); | return augmentWithMemPtg(result); |
case MissingArgPtg.sid: return MissingArgPtg.instance; // 0x16 | case MissingArgPtg.sid: return MissingArgPtg.instance; // 0x16 | ||||
case StringPtg.sid: return new StringPtg(in); // 0x17 | case StringPtg.sid: return new StringPtg(in); // 0x17 | ||||
// not implemented yet: case SxNamePtg.sid: return new SxNamePtg(in); // 0x18 | |||||
case AttrPtg.sid: return new AttrPtg(in); // 0x19 | case AttrPtg.sid: return new AttrPtg(in); // 0x19 | ||||
case ErrPtg.sid: return ErrPtg.read(in); // 0x1c | case ErrPtg.sid: return ErrPtg.read(in); // 0x1c | ||||
case BoolPtg.sid: return BoolPtg.read(in); // 0x1d | case BoolPtg.sid: return BoolPtg.read(in); // 0x1d |
import org.apache.poi.ss.usermodel.Sheet; | import org.apache.poi.ss.usermodel.Sheet; | ||||
import org.apache.poi.ss.util.CellRangeAddress; | import org.apache.poi.ss.util.CellRangeAddress; | ||||
import org.apache.poi.util.LocaleUtil; | |||||
/** | /** | ||||
* Helper for shifting rows up or down | * Helper for shifting rows up or down | ||||
// if the merged-region and the overwritten area intersect, we need to remove it | // if the merged-region and the overwritten area intersect, we need to remove it | ||||
return merged.intersects(overwrite); | return merged.intersects(overwrite); | ||||
} | } | ||||
/** | |||||
* Verify that the given column indices and step denote a valid range of columns to shift | |||||
* | |||||
* @param firstShiftColumnIndex the column to start shifting | |||||
* @param lastShiftColumnIndex the column to end shifting | |||||
* @param step length of the shifting step | |||||
*/ | |||||
public static void validateShiftParameters(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) { | |||||
if(step < 0) { | |||||
throw new IllegalArgumentException("Shifting step may not be negative, but had " + step); | |||||
} | |||||
if(firstShiftColumnIndex > lastShiftColumnIndex) { | |||||
throw new IllegalArgumentException(String.format(LocaleUtil.getUserLocale(), | |||||
"Incorrect shifting range : %d-%d", firstShiftColumnIndex, lastShiftColumnIndex)); | |||||
} | |||||
} | |||||
/** | |||||
* Verify that the given column indices and step denote a valid range of columns to shift to the left | |||||
* | |||||
* @param firstShiftColumnIndex the column to start shifting | |||||
* @param lastShiftColumnIndex the column to end shifting | |||||
* @param step length of the shifting step | |||||
*/ | |||||
public static void validateShiftLeftParameters(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) { | |||||
validateShiftParameters(firstShiftColumnIndex, lastShiftColumnIndex, step); | |||||
if(firstShiftColumnIndex - step < 0) { | |||||
throw new IllegalStateException("Column index less than zero: " + (firstShiftColumnIndex + step)); | |||||
} | |||||
} | |||||
} | } |
} | } | ||||
} | } | ||||
import org.apache.poi.ss.usermodel.CellStyle; | import org.apache.poi.ss.usermodel.CellStyle; | ||||
import org.apache.poi.ss.usermodel.CellType; | import org.apache.poi.ss.usermodel.CellType; | ||||
import org.apache.poi.ss.usermodel.Row; | import org.apache.poi.ss.usermodel.Row; | ||||
import org.apache.poi.ss.usermodel.helpers.RowShifter; | |||||
import org.apache.poi.ss.util.CellRangeAddress; | import org.apache.poi.ss.util.CellRangeAddress; | ||||
import org.apache.poi.util.Beta; | import org.apache.poi.util.Beta; | ||||
import org.apache.poi.util.Internal; | import org.apache.poi.util.Internal; | ||||
import org.apache.poi.util.LocaleUtil; | |||||
import org.apache.poi.xssf.model.StylesTable; | import org.apache.poi.xssf.model.StylesTable; | ||||
import org.apache.poi.xssf.usermodel.helpers.XSSFRowShifter; | import org.apache.poi.xssf.usermodel.helpers.XSSFRowShifter; | ||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; | import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; | ||||
*/ | */ | ||||
@Override | @Override | ||||
public void shiftCellsRight(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) { | public void shiftCellsRight(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) { | ||||
if(step < 0) { | |||||
throw new IllegalArgumentException("Shifting step may not be negative "); | |||||
} | |||||
if(firstShiftColumnIndex > lastShiftColumnIndex) { | |||||
throw new IllegalArgumentException(String.format(LocaleUtil.getUserLocale(), | |||||
"Incorrect shifting range : %d-%d", firstShiftColumnIndex, lastShiftColumnIndex)); | |||||
} | |||||
RowShifter.validateShiftParameters(firstShiftColumnIndex, lastShiftColumnIndex, step); | |||||
for (int columnIndex = lastShiftColumnIndex; columnIndex >= firstShiftColumnIndex; columnIndex--){ // process cells backwards, because of shifting | for (int columnIndex = lastShiftColumnIndex; columnIndex >= firstShiftColumnIndex; columnIndex--){ // process cells backwards, because of shifting | ||||
shiftCell(columnIndex, step); | shiftCell(columnIndex, step); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Shifts column range [firstShiftColumnIndex-lastShiftColumnIndex] step places to the left. | * Shifts column range [firstShiftColumnIndex-lastShiftColumnIndex] step places to the left. | ||||
* @param firstShiftColumnIndex the column to start shifting | * @param firstShiftColumnIndex the column to start shifting | ||||
*/ | */ | ||||
@Override | @Override | ||||
public void shiftCellsLeft(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) { | public void shiftCellsLeft(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) { | ||||
if(step < 0) { | |||||
throw new IllegalArgumentException("Shifting step may not be negative "); | |||||
} | |||||
if(firstShiftColumnIndex > lastShiftColumnIndex) { | |||||
throw new IllegalArgumentException(String.format(LocaleUtil.getUserLocale(), | |||||
"Incorrect shifting range : %d-%d", firstShiftColumnIndex, lastShiftColumnIndex)); | |||||
} | |||||
if(firstShiftColumnIndex - step < 0) { | |||||
throw new IllegalStateException("Column index less than zero : " + (Integer.valueOf(firstShiftColumnIndex + step)).toString()); | |||||
} | |||||
RowShifter.validateShiftLeftParameters(firstShiftColumnIndex, lastShiftColumnIndex, step); | |||||
for (int columnIndex = firstShiftColumnIndex; columnIndex <= lastShiftColumnIndex; columnIndex++){ | for (int columnIndex = firstShiftColumnIndex; columnIndex <= lastShiftColumnIndex; columnIndex++){ | ||||
shiftCell(columnIndex, -step); | shiftCell(columnIndex, -step); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
private void shiftCell(int columnIndex, int step/*pass negative value for left shift*/){ | private void shiftCell(int columnIndex, int step/*pass negative value for left shift*/){ | ||||
if(columnIndex + step < 0) { | if(columnIndex + step < 0) { | ||||
throw new IllegalStateException("Column index less than zero : " + (Integer.valueOf(columnIndex + step)).toString()); | throw new IllegalStateException("Column index less than zero : " + (Integer.valueOf(columnIndex + step)).toString()); |
package org.apache.poi.xssf.usermodel; | package org.apache.poi.xssf.usermodel; | ||||
import static org.junit.Assert.assertEquals; | |||||
import static org.junit.Assert.assertFalse; | |||||
import static org.junit.Assert.assertNotNull; | import static org.junit.Assert.assertNotNull; | ||||
import static org.junit.Assert.assertTrue; | |||||
import static org.junit.Assert.fail; | import static org.junit.Assert.fail; | ||||
import java.io.File; | import java.io.File; | ||||
s1.setRepeatingRows(cra); | s1.setRepeatingRows(cra); | ||||
PrintSetup ps1 = s1.getPrintSetup(); | PrintSetup ps1 = s1.getPrintSetup(); | ||||
assertEquals(false, ps1.getValidSettings()); | |||||
assertEquals(false, ps1.getLandscape()); | |||||
assertFalse(ps1.getValidSettings()); | |||||
assertFalse(ps1.getLandscape()); | |||||
// Had valid print settings before repeating | // Had valid print settings before repeating | ||||
PrintSetup ps2 = s2.getPrintSetup(); | PrintSetup ps2 = s2.getPrintSetup(); | ||||
ps2.setLandscape(false); | ps2.setLandscape(false); | ||||
assertEquals(true, ps2.getValidSettings()); | |||||
assertEquals(false, ps2.getLandscape()); | |||||
assertTrue(ps2.getValidSettings()); | |||||
assertFalse(ps2.getLandscape()); | |||||
s2.setRepeatingColumns(cra); | s2.setRepeatingColumns(cra); | ||||
s2.setRepeatingRows(cra); | s2.setRepeatingRows(cra); | ||||
ps2 = s2.getPrintSetup(); | ps2 = s2.getPrintSetup(); | ||||
assertEquals(true, ps2.getValidSettings()); | |||||
assertEquals(false, ps2.getLandscape()); | |||||
assertTrue(ps2.getValidSettings()); | |||||
assertFalse(ps2.getLandscape()); | |||||
wb1.close(); | wb1.close(); | ||||
wb2.close(); | wb2.close(); | ||||
Cell cell = row.createCell(colIndex++); | Cell cell = row.createCell(colIndex++); | ||||
cell.setCellType(CellType.STRING); | cell.setCellType(CellType.STRING); | ||||
cell.setCellValue("multiple"); | cell.setCellValue("multiple"); | ||||
cell = row.createCell(colIndex++); | |||||
cell = row.createCell(colIndex); | |||||
cell.setCellType(CellType.STRING); | cell.setCellType(CellType.STRING); | ||||
cell.setCellValue("unique"); | cell.setCellValue("unique"); | ||||
writeRow(sheet, rowIndex++, 30d, "IFERROR(INDEX(A2:A7, MATCH(1, (COUNTIF(B2:B3, A2:A7) = 0) * (NOT(ISBLANK(A2:A7))), 0)), \"\")"); | writeRow(sheet, rowIndex++, 30d, "IFERROR(INDEX(A2:A7, MATCH(1, (COUNTIF(B2:B3, A2:A7) = 0) * (NOT(ISBLANK(A2:A7))), 0)), \"\")"); | ||||
writeRow(sheet, rowIndex++, 2d, "IFERROR(INDEX(A2:A7, MATCH(1, (COUNTIF(B2:B4, A2:A7) = 0) * (NOT(ISBLANK(A2:A7))), 0)), \"\")"); | writeRow(sheet, rowIndex++, 2d, "IFERROR(INDEX(A2:A7, MATCH(1, (COUNTIF(B2:B4, A2:A7) = 0) * (NOT(ISBLANK(A2:A7))), 0)), \"\")"); | ||||
writeRow(sheet, rowIndex++, 30d, "IFERROR(INDEX(A2:A7, MATCH(1, (COUNTIF(B2:B5, A2:A7) = 0) * (NOT(ISBLANK(A2:A7))), 0)), \"\")"); | writeRow(sheet, rowIndex++, 30d, "IFERROR(INDEX(A2:A7, MATCH(1, (COUNTIF(B2:B5, A2:A7) = 0) * (NOT(ISBLANK(A2:A7))), 0)), \"\")"); | ||||
writeRow(sheet, rowIndex++, 2d, "IFERROR(INDEX(A2:A7, MATCH(1, (COUNTIF(B2:B6, A2:A7) = 0) * (NOT(ISBLANK(A2:A7))), 0)), \"\")"); | |||||
writeRow(sheet, rowIndex, 2d, "IFERROR(INDEX(A2:A7, MATCH(1, (COUNTIF(B2:B6, A2:A7) = 0) * (NOT(ISBLANK(A2:A7))), 0)), \"\")"); | |||||
/*FileOutputStream fileOut = new FileOutputStream(filename); | /*FileOutputStream fileOut = new FileOutputStream(filename); | ||||
wb.write(fileOut); | wb.write(fileOut); | ||||
workbook.close(); | workbook.close(); | ||||
out.flush(); | out.flush(); | ||||
} | } | ||||
// logger.info("File written!"); | |||||
} | } | ||||
} | } |
import static org.junit.Assert.assertEquals; | import static org.junit.Assert.assertEquals; | ||||
import static org.junit.Assert.assertNotNull; | import static org.junit.Assert.assertNotNull; | ||||
import static org.junit.Assert.assertNull; | |||||
import static org.junit.Assert.assertTrue; | import static org.junit.Assert.assertTrue; | ||||
import static org.junit.Assert.fail; | import static org.junit.Assert.fail; | ||||
assertEquals(1, ptgs.length); | assertEquals(1, ptgs.length); | ||||
assertEquals(NameXPxg.class, ptgs[0].getClass()); | assertEquals(NameXPxg.class, ptgs[0].getClass()); | ||||
assertEquals(0, ((NameXPxg)ptgs[0]).getExternalWorkbookNumber()); | assertEquals(0, ((NameXPxg)ptgs[0]).getExternalWorkbookNumber()); | ||||
assertEquals(null, ((NameXPxg)ptgs[0]).getSheetName()); | |||||
assertNull(((NameXPxg) ptgs[0]).getSheetName()); | |||||
assertEquals("NR_Global_B2",((NameXPxg)ptgs[0]).getNameName()); | assertEquals("NR_Global_B2",((NameXPxg)ptgs[0]).getNameName()); | ||||
assertEquals("[0]!NR_Global_B2", ptgs[0].toFormulaString()); | assertEquals("[0]!NR_Global_B2", ptgs[0].toFormulaString()); | ||||
assertEquals(1, ptgs.length); | assertEquals(1, ptgs.length); | ||||
assertEquals(NameXPxg.class, ptgs[0].getClass()); | assertEquals(NameXPxg.class, ptgs[0].getClass()); | ||||
assertEquals(1, ((NameXPxg)ptgs[0]).getExternalWorkbookNumber()); | assertEquals(1, ((NameXPxg)ptgs[0]).getExternalWorkbookNumber()); | ||||
assertEquals(null, ((NameXPxg)ptgs[0]).getSheetName()); | |||||
assertNull(((NameXPxg) ptgs[0]).getSheetName()); | |||||
assertEquals("NR_Global_B2",((NameXPxg)ptgs[0]).getNameName()); | assertEquals("NR_Global_B2",((NameXPxg)ptgs[0]).getNameName()); | ||||
assertEquals("[1]!NR_Global_B2", ptgs[0].toFormulaString()); | assertEquals("[1]!NR_Global_B2", ptgs[0].toFormulaString()); | ||||
public void draw(HwmfGraphics ctx) { | public void draw(HwmfGraphics ctx) { | ||||
final HwmfDrawProperties prop = ctx.getProperties(); | final HwmfDrawProperties prop = ctx.getProperties(); | ||||
final Rectangle2D old = prop.getWindow(); | final Rectangle2D old = prop.getWindow(); | ||||
double oldX = (old == null ? 0 : old.getX()); | |||||
double oldY = (old == null ? 0 : old.getY()); | |||||
if (oldX != getX() || oldY != getY()) { | |||||
if (old.getX() != getX() || old.getY() != getY()) { | |||||
prop.setWindowOrg(getX(), getY()); | prop.setWindowOrg(getX(), getY()); | ||||
ctx.updateWindowMapMode(); | ctx.updateWindowMapMode(); | ||||
} | } |
package org.apache.poi.ss.formula.eval.forked; | package org.apache.poi.ss.formula.eval.forked; | ||||
import static org.junit.Assert.assertEquals; | import static org.junit.Assert.assertEquals; | ||||
import static org.junit.Assert.assertNotNull; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
Workbook wb = createWorkbook(); | Workbook wb = createWorkbook(); | ||||
// The stability classifier is useful to reduce memory consumption of caching logic | // The stability classifier is useful to reduce memory consumption of caching logic | ||||
IStabilityClassifier stabilityClassifier = new IStabilityClassifier() { | |||||
@Override | |||||
public boolean isCellFinal(int sheetIndex, int rowIndex, int columnIndex) { | |||||
return sheetIndex == 1; | |||||
} | |||||
}; | |||||
IStabilityClassifier stabilityClassifier = (sheetIndex, rowIndex, columnIndex) -> sheetIndex == 1; | |||||
ForkedEvaluator fe1 = ForkedEvaluator.create(wb, stabilityClassifier, null); | ForkedEvaluator fe1 = ForkedEvaluator.create(wb, stabilityClassifier, null); | ||||
ForkedEvaluator fe2 = ForkedEvaluator.create(wb, stabilityClassifier, null); | ForkedEvaluator fe2 = ForkedEvaluator.create(wb, stabilityClassifier, null); | ||||
fe2.updateCell("Inputs", 0, 0, new NumberEval(1.2)); | fe2.updateCell("Inputs", 0, 0, new NumberEval(1.2)); | ||||
fe2.updateCell("Inputs", 0, 1, new NumberEval(2.0)); | fe2.updateCell("Inputs", 0, 1, new NumberEval(2.0)); | ||||
assertEquals(18.9, ((NumberEval) fe1.evaluate("Calculations", 0, 0)).getNumberValue(), 0.0); | |||||
assertEquals(4.0, ((NumberEval) fe2.evaluate("Calculations", 0, 0)).getNumberValue(), 0.0); | |||||
NumberEval eval1 = (NumberEval) fe1.evaluate("Calculations", 0, 0); | |||||
assertNotNull(eval1); | |||||
assertEquals(18.9, eval1.getNumberValue(), 0.0); | |||||
NumberEval eval2 = (NumberEval) fe2.evaluate("Calculations", 0, 0); | |||||
assertNotNull(eval2); | |||||
assertEquals(4.0, eval2.getNumberValue(), 0.0); | |||||
fe1.updateCell("Inputs", 0, 0, new NumberEval(3.0)); | fe1.updateCell("Inputs", 0, 0, new NumberEval(3.0)); | ||||
assertEquals(13.9, ((NumberEval) fe1.evaluate("Calculations", 0, 0)).getNumberValue(), 0.0); | |||||
eval1 = (NumberEval) fe1.evaluate("Calculations", 0, 0); | |||||
assertNotNull(eval1); | |||||
assertEquals(13.9, eval1.getNumberValue(), 0.0); | |||||
wb.close(); | wb.close(); | ||||
} | } |
import org.apache.poi.hssf.usermodel.HSSFWorkbook; | import org.apache.poi.hssf.usermodel.HSSFWorkbook; | ||||
import org.apache.poi.ss.ITestDataProvider; | import org.apache.poi.ss.ITestDataProvider; | ||||
import org.apache.poi.ss.SpreadsheetVersion; | import org.apache.poi.ss.SpreadsheetVersion; | ||||
import org.apache.poi.ss.formula.FormulaParseException; | |||||
import org.apache.poi.ss.util.CellRangeAddress; | import org.apache.poi.ss.util.CellRangeAddress; | ||||
import org.apache.poi.ss.util.CellRangeAddressList; | import org.apache.poi.ss.util.CellRangeAddressList; | ||||
import org.apache.poi.ss.util.PaneInformation; | import org.apache.poi.ss.util.PaneInformation; | ||||
fmla = createFunction(name, ssVersion.getMaxFunctionArgs() + 1); | fmla = createFunction(name, ssVersion.getMaxFunctionArgs() + 1); | ||||
cell.setCellFormula(fmla); | cell.setCellFormula(fmla); | ||||
fail("Expected FormulaParseException"); | fail("Expected FormulaParseException"); | ||||
} catch (RuntimeException e){ | |||||
} catch (FormulaParseException e){ | |||||
assertTrue(e.getMessage().startsWith("Too many arguments to function '"+name+"'")); | assertTrue(e.getMessage().startsWith("Too many arguments to function '"+name+"'")); | ||||
} | } | ||||
} | } | ||||
double leadingWhitespaceRatio = ((double) leadingWhitespaceColWidth)/noWhitespaceColWidth; | double leadingWhitespaceRatio = ((double) leadingWhitespaceColWidth)/noWhitespaceColWidth; | ||||
double trailingWhitespaceRatio = ((double) leadingWhitespaceColWidth)/noWhitespaceColWidth; | double trailingWhitespaceRatio = ((double) leadingWhitespaceColWidth)/noWhitespaceColWidth; | ||||
assertGreaterThan("leading whitespace is longer than no whitespace", leadingWhitespaceRatio, expectedRatioThreshold); | |||||
assertGreaterThan("trailing whitespace is longer than no whitespace", trailingWhitespaceRatio, expectedRatioThreshold); | |||||
assertGreaterThan("leading whitespace is longer than no whitespace", | |||||
leadingWhitespaceRatio, expectedRatioThreshold); | |||||
assertGreaterThan("trailing whitespace is longer than no whitespace", | |||||
trailingWhitespaceRatio, expectedRatioThreshold); | |||||
assertEquals("cells with equal leading and trailing whitespace have equal width", | assertEquals("cells with equal leading and trailing whitespace have equal width", | ||||
leadingWhitespaceColWidth, trailingWhitespaceColWidth); | leadingWhitespaceColWidth, trailingWhitespaceColWidth); | ||||
import static org.junit.Assert.assertFalse; | import static org.junit.Assert.assertFalse; | ||||
import static org.junit.Assert.assertNotNull; | import static org.junit.Assert.assertNotNull; | ||||
import static org.junit.Assert.assertNull; | import static org.junit.Assert.assertNull; | ||||
import static org.junit.Assert.assertSame; | |||||
import static org.junit.Assert.assertTrue; | import static org.junit.Assert.assertTrue; | ||||
import static org.junit.Assert.fail; | import static org.junit.Assert.fail; | ||||
// First up, no policy given, uses default | // First up, no policy given, uses default | ||||
assertEquals(CellType.STRING, row.getCell(0).getCellType()); | assertEquals(CellType.STRING, row.getCell(0).getCellType()); | ||||
assertEquals(CellType.NUMERIC, row.getCell(1).getCellType()); | assertEquals(CellType.NUMERIC, row.getCell(1).getCellType()); | ||||
assertEquals(null, row.getCell(2)); | |||||
assertEquals(null, row.getCell(3)); | |||||
assertNull(row.getCell(2)); | |||||
assertNull(row.getCell(3)); | |||||
assertEquals(CellType.BLANK, row.getCell(4).getCellType()); | assertEquals(CellType.BLANK, row.getCell(4).getCellType()); | ||||
assertEquals(CellType.NUMERIC, row.getCell(5).getCellType()); | assertEquals(CellType.NUMERIC, row.getCell(5).getCellType()); | ||||
// RETURN_NULL_AND_BLANK - same as default | // RETURN_NULL_AND_BLANK - same as default | ||||
assertEquals(CellType.STRING, row.getCell(0, MissingCellPolicy.RETURN_NULL_AND_BLANK).getCellType()); | assertEquals(CellType.STRING, row.getCell(0, MissingCellPolicy.RETURN_NULL_AND_BLANK).getCellType()); | ||||
assertEquals(CellType.NUMERIC, row.getCell(1, MissingCellPolicy.RETURN_NULL_AND_BLANK).getCellType()); | assertEquals(CellType.NUMERIC, row.getCell(1, MissingCellPolicy.RETURN_NULL_AND_BLANK).getCellType()); | ||||
assertEquals(null, row.getCell(2, MissingCellPolicy.RETURN_NULL_AND_BLANK)); | |||||
assertEquals(null, row.getCell(3, MissingCellPolicy.RETURN_NULL_AND_BLANK)); | |||||
assertNull(row.getCell(2, MissingCellPolicy.RETURN_NULL_AND_BLANK)); | |||||
assertNull(row.getCell(3, MissingCellPolicy.RETURN_NULL_AND_BLANK)); | |||||
assertEquals(CellType.BLANK, row.getCell(4, MissingCellPolicy.RETURN_NULL_AND_BLANK).getCellType()); | assertEquals(CellType.BLANK, row.getCell(4, MissingCellPolicy.RETURN_NULL_AND_BLANK).getCellType()); | ||||
assertEquals(CellType.NUMERIC, row.getCell(5, MissingCellPolicy.RETURN_NULL_AND_BLANK).getCellType()); | assertEquals(CellType.NUMERIC, row.getCell(5, MissingCellPolicy.RETURN_NULL_AND_BLANK).getCellType()); | ||||
// RETURN_BLANK_AS_NULL - nearly the same | // RETURN_BLANK_AS_NULL - nearly the same | ||||
assertEquals(CellType.STRING, row.getCell(0, MissingCellPolicy.RETURN_BLANK_AS_NULL).getCellType()); | assertEquals(CellType.STRING, row.getCell(0, MissingCellPolicy.RETURN_BLANK_AS_NULL).getCellType()); | ||||
assertEquals(CellType.NUMERIC, row.getCell(1, MissingCellPolicy.RETURN_BLANK_AS_NULL).getCellType()); | assertEquals(CellType.NUMERIC, row.getCell(1, MissingCellPolicy.RETURN_BLANK_AS_NULL).getCellType()); | ||||
assertEquals(null, row.getCell(2, MissingCellPolicy.RETURN_BLANK_AS_NULL)); | |||||
assertEquals(null, row.getCell(3, MissingCellPolicy.RETURN_BLANK_AS_NULL)); | |||||
assertEquals(null, row.getCell(4, MissingCellPolicy.RETURN_BLANK_AS_NULL)); | |||||
assertNull(row.getCell(2, MissingCellPolicy.RETURN_BLANK_AS_NULL)); | |||||
assertNull(row.getCell(3, MissingCellPolicy.RETURN_BLANK_AS_NULL)); | |||||
assertNull(row.getCell(4, MissingCellPolicy.RETURN_BLANK_AS_NULL)); | |||||
assertEquals(CellType.NUMERIC, row.getCell(5, MissingCellPolicy.RETURN_BLANK_AS_NULL).getCellType()); | assertEquals(CellType.NUMERIC, row.getCell(5, MissingCellPolicy.RETURN_BLANK_AS_NULL).getCellType()); | ||||
// CREATE_NULL_AS_BLANK - creates as needed | // CREATE_NULL_AS_BLANK - creates as needed | ||||
assertEquals(CellType.STRING, row.getCell(0).getCellType()); | assertEquals(CellType.STRING, row.getCell(0).getCellType()); | ||||
assertEquals(CellType.NUMERIC, row.getCell(1).getCellType()); | assertEquals(CellType.NUMERIC, row.getCell(1).getCellType()); | ||||
assertEquals(null, row.getCell(2)); | |||||
assertEquals(null, row.getCell(3)); | |||||
assertEquals(null, row.getCell(4)); | |||||
assertNull(row.getCell(2)); | |||||
assertNull(row.getCell(3)); | |||||
assertNull(row.getCell(4)); | |||||
assertEquals(CellType.NUMERIC, row.getCell(5).getCellType()); | assertEquals(CellType.NUMERIC, row.getCell(5).getCellType()); | ||||
workbook.close(); | workbook.close(); | ||||
Cell cell1 = row.createCell(1); | Cell cell1 = row.createCell(1); | ||||
Iterator<Cell> it = row.cellIterator(); | Iterator<Cell> it = row.cellIterator(); | ||||
assertTrue(it.hasNext()); | assertTrue(it.hasNext()); | ||||
assertTrue(cell1 == it.next()); | |||||
assertSame(cell1, it.next()); | |||||
assertFalse(it.hasNext()); | assertFalse(it.hasNext()); | ||||
// Add another cell at the end | // Add another cell at the end | ||||
Cell cell2 = row.createCell(99); | Cell cell2 = row.createCell(99); | ||||
it = row.cellIterator(); | it = row.cellIterator(); | ||||
assertTrue(it.hasNext()); | assertTrue(it.hasNext()); | ||||
assertTrue(cell1 == it.next()); | |||||
assertSame(cell1, it.next()); | |||||
assertTrue(it.hasNext()); | assertTrue(it.hasNext()); | ||||
assertTrue(cell2 == it.next()); | |||||
assertSame(cell2, it.next()); | |||||
// Add another cell at the beginning | // Add another cell at the beginning | ||||
Cell cell3 = row.createCell(0); | Cell cell3 = row.createCell(0); | ||||
it = row.cellIterator(); | it = row.cellIterator(); | ||||
assertTrue(it.hasNext()); | assertTrue(it.hasNext()); | ||||
assertTrue(cell3 == it.next()); | |||||
assertSame(cell3, it.next()); | |||||
assertTrue(it.hasNext()); | assertTrue(it.hasNext()); | ||||
assertTrue(cell1 == it.next()); | |||||
assertSame(cell1, it.next()); | |||||
assertTrue(it.hasNext()); | assertTrue(it.hasNext()); | ||||
assertTrue(cell2 == it.next()); | |||||
assertSame(cell2, it.next()); | |||||
// Replace cell1 | // Replace cell1 | ||||
Cell cell4 = row.createCell(1); | Cell cell4 = row.createCell(1); | ||||
it = row.cellIterator(); | it = row.cellIterator(); | ||||
assertTrue(it.hasNext()); | assertTrue(it.hasNext()); | ||||
assertTrue(cell3 == it.next()); | |||||
assertSame(cell3, it.next()); | |||||
assertTrue(it.hasNext()); | assertTrue(it.hasNext()); | ||||
assertTrue(cell4 == it.next()); | |||||
assertSame(cell4, it.next()); | |||||
assertTrue(it.hasNext()); | assertTrue(it.hasNext()); | ||||
assertTrue(cell2 == it.next()); | |||||
assertSame(cell2, it.next()); | |||||
assertFalse(it.hasNext()); | assertFalse(it.hasNext()); | ||||
// Add another cell, specifying the cellType | // Add another cell, specifying the cellType | ||||
it = row.cellIterator(); | it = row.cellIterator(); | ||||
assertNotNull(cell5); | assertNotNull(cell5); | ||||
assertTrue(it.hasNext()); | assertTrue(it.hasNext()); | ||||
assertTrue(cell3 == it.next()); | |||||
assertSame(cell3, it.next()); | |||||
assertTrue(it.hasNext()); | assertTrue(it.hasNext()); | ||||
assertTrue(cell4 == it.next()); | |||||
assertSame(cell4, it.next()); | |||||
assertTrue(it.hasNext()); | assertTrue(it.hasNext()); | ||||
assertTrue(cell5 == it.next()); | |||||
assertSame(cell5, it.next()); | |||||
assertTrue(it.hasNext()); | assertTrue(it.hasNext()); | ||||
assertTrue(cell2 == it.next()); | |||||
assertSame(cell2, it.next()); | |||||
assertEquals(CellType.STRING, cell5.getCellType()); | assertEquals(CellType.STRING, cell5.getCellType()); | ||||
wb.close(); | wb.close(); | ||||
} | } | ||||
Row row2 = sheet.createRow(1); | Row row2 = sheet.createRow(1); | ||||
// Won't be styled currently | // Won't be styled currently | ||||
assertEquals(false, row1.isFormatted()); | |||||
assertEquals(false, row2.isFormatted()); | |||||
assertEquals(null, row1.getRowStyle()); | |||||
assertEquals(null, row2.getRowStyle()); | |||||
assertFalse(row1.isFormatted()); | |||||
assertFalse(row2.isFormatted()); | |||||
assertNull(row1.getRowStyle()); | |||||
assertNull(row2.getRowStyle()); | |||||
// Style one | // Style one | ||||
CellStyle style = wb1.createCellStyle(); | CellStyle style = wb1.createCellStyle(); | ||||
row2.setRowStyle(style); | row2.setRowStyle(style); | ||||
// Check | // Check | ||||
assertEquals(false, row1.isFormatted()); | |||||
assertEquals(true, row2.isFormatted()); | |||||
assertEquals(null, row1.getRowStyle()); | |||||
assertFalse(row1.isFormatted()); | |||||
assertTrue(row2.isFormatted()); | |||||
assertNull(row1.getRowStyle()); | |||||
assertEquals(style, row2.getRowStyle()); | assertEquals(style, row2.getRowStyle()); | ||||
// Save, load and re-check | // Save, load and re-check | ||||
row2 = sheet.getRow(1); | row2 = sheet.getRow(1); | ||||
style = wb2.getCellStyleAt(style.getIndex()); | style = wb2.getCellStyleAt(style.getIndex()); | ||||
assertEquals(false, row1.isFormatted()); | |||||
assertEquals(true, row2.isFormatted()); | |||||
assertEquals(null, row1.getRowStyle()); | |||||
assertFalse(row1.isFormatted()); | |||||
assertTrue(row2.isFormatted()); | |||||
assertNull(row1.getRowStyle()); | |||||
assertEquals(style, row2.getRowStyle()); | assertEquals(style, row2.getRowStyle()); | ||||
assertEquals(4, style.getDataFormat()); | assertEquals(4, style.getDataFormat()); | ||||
workbook.close(); | workbook.close(); | ||||
} | } | ||||
/** | /** | ||||
* Set single-cell array formula | * Set single-cell array formula | ||||
*/ | */ |
@Test | @Test | ||||
public void test62828() { | public void test62828() { | ||||
Workbook wb = new HSSFWorkbook(); | |||||
final Workbook wb = new HSSFWorkbook(); | |||||
final Sheet sheet = wb.createSheet("Ctor test"); | final Sheet sheet = wb.createSheet("Ctor test"); | ||||
final String sheetName = sheet.getSheetName(); | final String sheetName = sheet.getSheetName(); | ||||
final Row row = sheet.createRow(0); | final Row row = sheet.createRow(0); |