List of usage examples for org.apache.poi.hssf.util CellReference formatAsString
public String formatAsString()
From source file:edu.emory.cci.aiw.cvrg.eureka.etl.spreadsheet.XlsxDataProvider.java
License:Open Source License
private void throwException(String sheetName, Cell cell, String problemDescription, Exception cause) throws DataProviderException { String msgTemplate = messages.getString("xlsxDataProvider.error.parsing"); CellReference cellRef = new CellReference(cell.getRowIndex(), cell.getColumnIndex()); String msg = MessageFormat.format(msgTemplate, sheetName, cellRef.formatAsString(), problemDescription); throw new DataProviderException(msg, cause); }
From source file:gov.nih.nci.cananolab.util.ExcelParser.java
License:BSD License
public void printSheet(Sheet sheet) { for (Row row : sheet) { for (Cell cell : row) { CellReference cellRef = new CellReference(cell.getRowIndex(), cell.getColumnIndex()); System.out.print(cellRef.formatAsString()); System.out.print(" - "); switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: System.out.println(cell.getRichStringCellValue().getString()); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { System.out.println(cell.getDateCellValue()); } else { System.out.println(cell.getNumericCellValue()); }//from www .j a v a2 s.c o m break; case Cell.CELL_TYPE_BOOLEAN: System.out.println(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: System.out.println(cell.getCellFormula()); break; default: System.out.println(); } } } }
From source file:org.drools.scorecards.parser.xls.XLSEventDataCollector.java
License:Apache License
private void fulfillExpectation(int currentRowCtr, int currentColCtr, Object cellValue, Class expectedClass) throws ScorecardParseException { List<DataExpectation> dataExpectations = resolveExpectations(currentRowCtr, currentColCtr); CellReference cellRef = new CellReference(currentRowCtr, currentColCtr); Method method = null;/* ww w . j av a 2 s . co m*/ for (DataExpectation dataExpectation : dataExpectations) { try { if (dataExpectation != null && dataExpectation.object != null) { if (cellValue == null || StringUtils.isEmpty(cellValue.toString())) { if (dataExpectation.errorMessage != null && !StringUtils.isEmpty(dataExpectation.errorMessage)) { parseErrors.add( new ScorecardError(cellRef.formatAsString(), dataExpectation.errorMessage)); return; } } String setter = "set" + Character.toUpperCase(dataExpectation.property.charAt(0)) + dataExpectation.property.substring(1); method = getSuitableMethod(cellValue, expectedClass, dataExpectation, setter); if (method == null) { if (cellValue != null && !StringUtils.isEmpty(cellValue.toString())) { parseErrors.add(new ScorecardError(cellRef.formatAsString(), "Unexpected Value! Wrong Datatype?")); } return; } if (method.getParameterTypes()[0] == Double.class) { cellValue = Double.parseDouble(cellValue.toString()); } if (method.getParameterTypes()[0] == Boolean.class) { cellValue = Boolean.valueOf(cellValue.toString()); } if (method.getParameterTypes()[0] == String.class && !(cellValue instanceof String) && cellValue != null) { cellValue = cellValue.toString(); } method.invoke(dataExpectation.object, cellValue); if (dataExpectation.object instanceof Extension && ("cellRef".equals(((Extension) dataExpectation.object).getName()))) { ((Extension) dataExpectation.object).setValue(cellRef.formatAsString()); } //dataExpectations.remove(dataExpectation); } } catch (Exception e) { throw new ScorecardParseException(e); } } }
From source file:uk.ac.liverpool.spreadsheet.FormulaRenderer.java
License:Apache License
/** * Static method to convert an array of {@link Ptg}s in RPN order * to a human readable string format in infix mode. * @param book used for defined names and 3D references * @param ptgs must not be <code>null</code> * @return a human readable String//from w w w . j ava 2s.c o m */ public static String toFormulaString(FormulaRenderingWorkbook book, Ptg[] ptgs) { if (ptgs == null || ptgs.length == 0) { throw new IllegalArgumentException("ptgs must not be null"); } Stack<String> stack = new Stack<String>(); for (int i = 0; i < ptgs.length; i++) { Ptg ptg = ptgs[i]; // TODO - what about MemNoMemPtg? if (ptg instanceof MemAreaPtg || ptg instanceof MemFuncPtg || ptg instanceof MemErrPtg) { // marks the start of a list of area expressions which will be naturally combined // by their trailing operators (e.g. UnionPtg) // TODO - put comment and throw exception in toFormulaString() of these classes continue; } if (ptg instanceof ParenthesisPtg) { String contents = stack.pop(); stack.push("(" + contents + ")"); continue; } if (ptg instanceof AttrPtg) { AttrPtg attrPtg = ((AttrPtg) ptg); if (attrPtg.isOptimizedIf() || attrPtg.isOptimizedChoose() || attrPtg.isSkip()) { continue; } if (attrPtg.isSpace()) { // POI currently doesn't render spaces in formulas continue; // but if it ever did, care must be taken: // tAttrSpace comes *before* the operand it applies to, which may be consistent // with how the formula text appears but is against the RPN ordering assumed here } if (attrPtg.isSemiVolatile()) { // similar to tAttrSpace - RPN is violated continue; } if (attrPtg.isSum()) { String[] operands = getOperands(stack, attrPtg.getNumberOfOperands()); stack.push(attrPtg.toFormulaString(operands)); continue; } throw new RuntimeException("Unexpected tAttr: " + attrPtg.toString()); } if (ptg instanceof WorkbookDependentFormula) { WorkbookDependentFormula optg = (WorkbookDependentFormula) ptg; stack.push(optg.toFormulaString(book)); continue; } if (!(ptg instanceof OperationPtg)) { String s = ""; if (ptg instanceof AreaPtg) { AreaPtg a = (AreaPtg) ptg; s = formatReferenceAsString(a); } else if (ptg instanceof RefPtg) { RefPtg a = (RefPtg) ptg; CellReference cr = new CellReference(a.getRow(), a.getColumn(), !a.isRowRelative(), !a.isColRelative()); s = "[." + cr.formatAsString() + "]"; } else s = ptg.toFormulaString(); stack.push(s); continue; } OperationPtg o = (OperationPtg) ptg; String[] operands = getOperands(stack, o.getNumberOfOperands()); if (o instanceof AbstractFunctionPtg) { AbstractFunctionPtg a = (AbstractFunctionPtg) o; stack.push(toFormulaString(a, operands)); } else stack.push(o.toFormulaString(operands)); } if (stack.isEmpty()) { // inspection of the code above reveals that every stack.pop() is followed by a // stack.push(). So this is either an internal error or impossible. throw new IllegalStateException("Stack underflow"); } String result = stack.pop(); if (!stack.isEmpty()) { // Might be caused by some tokens like AttrPtg and Mem*Ptg, which really shouldn't // put anything on the stack throw new IllegalStateException("too much stuff left on the stack"); } return result; }
From source file:uk.ac.liverpool.spreadsheet.FormulaRenderer.java
License:Apache License
protected static String formatReferenceAsString(AreaPtg a) { CellReference topLeft = new CellReference(a.getFirstRow(), a.getFirstColumn(), !a.isFirstRowRelative(), !a.isFirstColRelative());//from w ww . jav a 2 s. c o m CellReference botRight = new CellReference(a.getLastRow(), a.getLastColumn(), !a.isLastRowRelative(), !a.isLastColRelative()); if (AreaReference.isWholeColumnReference(topLeft, botRight)) { return (new AreaReference(topLeft, botRight)).formatAsString(); } return "[." + topLeft.formatAsString() + ":" + "." + botRight.formatAsString() + "]"; }
From source file:uk.co.danielrendall.fractdim.app.workers.ExcelExportWorker.java
License:Open Source License
public void visit(AngleGridCollection collection) { intermediateAngleColumn = 3;/*from w w w . jav a2 s . c o m*/ resultsRow = 1; for (Double angle : collection.getAvailableAngles()) { currentAngle = angle; ResolutionGridCollection rgc = collection.collectionForAngle(angle); rgc.accept(this); final Row angleRow = resultsSheet.createRow(resultsRow); angleRow.createCell(0).setCellValue(angle); CellReference topXRange = new CellReference(2, 2, false, false); CellReference bottomXRange = new CellReference(intermediateResolutionRow - 1, 2, false, false); CellReference topYRange = new CellReference(2, intermediateAngleColumn + 1, false, false); CellReference bottomYRange = new CellReference(intermediateResolutionRow - 1, intermediateAngleColumn + 1, false, false); angleRow.createCell(1) .setCellFormula(String.format("SLOPE('Intermediate'!%s:%s,'Intermediate'!%s:%s)", topYRange.formatAsString(), bottomYRange.formatAsString(), topXRange.formatAsString(), bottomXRange.formatAsString())); intermediateAngleColumn += 3; resultsRow += 1; } Row grandAverageRow = resultsSheet.createRow(resultsRow + 1); grandAverageRow.createCell(0).setCellValue(ch.createRichTextString("Average:")); grandAverageRow.createCell(1).setCellFormula(String.format("AVERAGE(B2:B%d)", resultsRow)); }