ASF Bugzilla – Attachment 21281 Details for
Bug 44095
[PATCH] Mid function in formula cell
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Generated using svn diff > FormulaMidFunction.patch
FormulaMidFunction.patch (text/plain), 3.93 KB, created by
Manda Wilson
on 2007-12-18 17:05:45 UTC
(
hide
)
Description:
Generated using svn diff > FormulaMidFunction.patch
Filename:
MIME Type:
Creator:
Manda Wilson
Created:
2007-12-18 17:05:45 UTC
Size:
3.93 KB
patch
obsolete
>Index: src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Mid.java >=================================================================== >--- src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Mid.java (revision 605370) >+++ src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Mid.java (working copy) >@@ -20,6 +20,80 @@ > */ > package org.apache.poi.hssf.record.formula.functions; > >-public class Mid extends NotImplementedFunction { >+import org.apache.poi.hssf.record.formula.eval.BlankEval; >+import org.apache.poi.hssf.record.formula.eval.ErrorEval; >+import org.apache.poi.hssf.record.formula.eval.Eval; >+import org.apache.poi.hssf.record.formula.eval.NumericValueEval; >+import org.apache.poi.hssf.record.formula.eval.StringEval; >+import org.apache.poi.hssf.record.formula.eval.StringValueEval; >+import org.apache.poi.hssf.record.formula.eval.ValueEval; > >+/** >+ * An implementation of the MID function: >+ * Returns a specific number of characters from a text string, >+ * starting at the position you specify, based on the number >+ * of characters you specify. >+ * @author Manda Wilson < wilson at c bio dot msk cc dot org > >+ */ >+public class Mid extends TextFunction { >+ /** >+ * Returns a specific number of characters from a text string, >+ * starting at the position you specify, based on the number >+ * of characters you specify. >+ * >+ * @see org.apache.poi.hssf.record.formula.eval.Eval >+ */ >+ public Eval evaluate(Eval[] operands, int srcCellRow, short srcCellCol) { >+ Eval retval = null; >+ String str = null; >+ int startNum = 0; >+ int numChars = 0; >+ >+ switch (operands.length) { >+ default: >+ retval = ErrorEval.VALUE_INVALID; >+ case 3: >+ // first operand is text string containing characters to extract >+ // second operand is position of first character to extract >+ // third operand is the number of characters to return >+ ValueEval firstveval = singleOperandEvaluate(operands[0], srcCellRow, srcCellCol); >+ ValueEval secondveval = singleOperandEvaluate(operands[1], srcCellRow, srcCellCol); >+ ValueEval thirdveval = singleOperandEvaluate(operands[2], srcCellRow, srcCellCol); >+ if (firstveval instanceof StringValueEval >+ && secondveval instanceof NumericValueEval >+ && thirdveval instanceof NumericValueEval) { >+ >+ StringValueEval strEval = (StringValueEval) firstveval; >+ str = strEval.getStringValue(); >+ >+ NumericValueEval startNumEval = (NumericValueEval) secondveval; >+ // NOTE: it is safe to cast to int here >+ // because in Excel =MID("test", 1, 1.7) returns t >+ // so 1.7 must be truncated to 1 >+ // and =MID("test", 1.9, 2) returns te >+ // so 1.9 must be truncated to 1 >+ startNum = (int) startNumEval.getNumberValue(); >+ >+ NumericValueEval numCharsEval = (NumericValueEval) thirdveval; >+ numChars = (int) numCharsEval.getNumberValue(); >+ >+ } else { >+ retval = ErrorEval.VALUE_INVALID; >+ } >+ } >+ >+ if (retval == null) { >+ if (startNum < 1 || numChars < 0) { >+ retval = ErrorEval.VALUE_INVALID; >+ } else if (startNum > str.length() || numChars == 0) { >+ retval = BlankEval.INSTANCE; >+ } else if (startNum + numChars > str.length()) { >+ retval = new StringEval(str.substring(startNum - 1)); >+ } else { >+ retval = new StringEval(str.substring(startNum - 1, numChars)); >+ } >+ } >+ return retval; >+ } >+ > } >Index: src/scratchpad/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls >=================================================================== >Cannot display: file marked as a binary type. >svn:mime-type = application/octet-stream
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 44095
: 21281 |
21282