This is an automated email from the ASF dual-hosted git repository. jimjag pushed a commit to branch openformula in repository https://gitbox.apache.org/repos/asf/openoffice.git
commit 6f13be1d67015e4e304b8da30bfda377e92e7d96 Author: Jim Jagielski <[email protected]> AuthorDate: Mon Nov 16 09:57:11 2026 -0500 sc: implement IFERROR as a jump function like IFNA --- main/formula/inc/formula/compiler.hrc | 5 ++-- main/formula/inc/formula/opcode.hxx | 1 + main/formula/source/core/api/FormulaCompiler.cxx | 10 +++---- main/formula/source/core/api/token.cxx | 7 +++-- .../formula/source/core/resource/core_resource.src | 6 ++++ main/sc/inc/helpids.h | 1 + main/sc/source/core/inc/interpre.hxx | 2 ++ main/sc/source/core/tool/interpr1.cxx | 29 +++++++++++++++----- main/sc/source/core/tool/interpr4.cxx | 6 ++-- main/sc/source/core/tool/parclass.cxx | 1 + main/sc/source/core/tool/token.cxx | 1 + main/sc/source/ui/src/scfuncs.src | 32 ++++++++++++++++++++++ main/sc/util/hidother.src | 1 + test/odf-validate/calcval-cases14.tsv | 28 +++++++++++++++++++ 14 files changed, 111 insertions(+), 19 deletions(-) diff --git a/main/formula/inc/formula/compiler.hrc b/main/formula/inc/formula/compiler.hrc index 3e5312f87e..9984e9b50c 100644 --- a/main/formula/inc/formula/compiler.hrc +++ b/main/formula/inc/formula/compiler.hrc @@ -404,10 +404,11 @@ #define SC_OPCODE_BITRSHIFT 408 #define SC_OPCODE_IFNA 409 #define SC_OPCODE_SKEWP 410 +#define SC_OPCODE_IFERROR 411 -#define SC_OPCODE_STOP_2_PAR 411 +#define SC_OPCODE_STOP_2_PAR 412 -#define SC_OPCODE_LAST_OPCODE_ID 410 /* last OpCode */ +#define SC_OPCODE_LAST_OPCODE_ID 411 /* last OpCode */ /*** Interna ***/ #define SC_OPCODE_INTERNAL_BEGIN 9999 diff --git a/main/formula/inc/formula/opcode.hxx b/main/formula/inc/formula/opcode.hxx index 69759898fb..6a5d9bf432 100644 --- a/main/formula/inc/formula/opcode.hxx +++ b/main/formula/inc/formula/opcode.hxx @@ -401,6 +401,7 @@ enum OpCodeEnum ocBitRShift = SC_OPCODE_BITRSHIFT, ocIfna = SC_OPCODE_IFNA, ocSkewP = SC_OPCODE_SKEWP, + ocIfError = SC_OPCODE_IFERROR, // internal stuff ocInternalBegin = SC_OPCODE_INTERNAL_BEGIN, ocTTT = SC_OPCODE_TTT, diff --git a/main/formula/source/core/api/FormulaCompiler.cxx b/main/formula/source/core/api/FormulaCompiler.cxx index d233ccd36c..7c56e6d6f2 100644 --- a/main/formula/source/core/api/FormulaCompiler.cxx +++ b/main/formula/source/core/api/FormulaCompiler.cxx @@ -1012,7 +1012,7 @@ void FormulaCompiler::Factor() eOp = NextToken(); } else if ((SC_OPCODE_START_2_PAR <= eOp && eOp < SC_OPCODE_STOP_2_PAR - && (eOp != ocIfna || bCompileForFAP)) + && ((eOp != ocIfna && eOp != ocIfError) || bCompileForFAP)) || eOp == ocExternal || eOp == ocMacro || eOp == ocAnd @@ -1074,13 +1074,13 @@ void FormulaCompiler::Factor() pArr->SetRecalcModeAlways(); PutCode( pFacToken ); } - else if (eOp == ocIf || eOp == ocChose || eOp == ocIfna) + else if (eOp == ocIf || eOp == ocChose || eOp == ocIfna || eOp == ocIfError) { // the PC counters are -1 pFacToken = pToken; if ( eOp == ocIf ) pFacToken->GetJump()[ 0 ] = 3; // if, else, behind - else if ( eOp == ocIfna ) + else if ( eOp == ocIfna || eOp == ocIfError ) pFacToken->GetJump()[ 0 ] = 2; // fallback, behind else pFacToken->GetJump()[ 0 ] = MAXJUMPCOUNT+1; @@ -1100,7 +1100,7 @@ void FormulaCompiler::Factor() // nJump[0]*2+2, which is 3*2+2 on ocIf. const short nJumpMax = (pFacToken->GetOpCode() == ocIf ? 3 : - (pFacToken->GetOpCode() == ocIfna ? 2 : MAXJUMPCOUNT)); + ((pFacToken->GetOpCode() == ocIfna || pFacToken->GetOpCode() == ocIfError) ? 2 : MAXJUMPCOUNT)); while ( (nJumpCount < (MAXJUMPCOUNT - 1)) && (eOp == ocSep) && (!pArr->GetCodeError() || bIgnoreErrors) ) { @@ -1127,7 +1127,7 @@ void FormulaCompiler::Factor() if ( ++nJumpCount <= nJumpMax ) pFacToken->GetJump()[ nJumpCount ] = pc-1; if ((pFacToken->GetOpCode() == ocIf && (nJumpCount > 3)) || - (pFacToken->GetOpCode() == ocIfna && (nJumpCount != 2)) || + ((pFacToken->GetOpCode() == ocIfna || pFacToken->GetOpCode() == ocIfError) && (nJumpCount != 2)) || (nJumpCount >= MAXJUMPCOUNT)) SetError(errIllegalParameter); else diff --git a/main/formula/source/core/api/token.cxx b/main/formula/source/core/api/token.cxx index 3184f7f526..fe54da75bf 100644 --- a/main/formula/source/core/api/token.cxx +++ b/main/formula/source/core/api/token.cxx @@ -125,7 +125,7 @@ sal_uInt8 FormulaToken::GetParamCount() const return 0; // no parameter else if (SC_OPCODE_START_1_PAR <= eOp && eOp < SC_OPCODE_STOP_1_PAR) return 1; // one parameter - else if ( eOp == ocIf || eOp == ocChose || eOp == ocIfna ) + else if ( eOp == ocIf || eOp == ocChose || eOp == ocIfna || eOp == ocIfError ) return 1; // only the condition counts as parameter else return 0; // all the rest, no Parameter, or @@ -846,7 +846,7 @@ sal_Bool FormulaTokenArray::HasMatrixDoubleRefOps() } if ( eOp == ocPush || lcl_IsReference( eOp, t->GetType() ) ) pStack[sp++] = t; - else if ( eOp == ocIf || eOp == ocChose || eOp == ocIfna ) + else if ( eOp == ocIf || eOp == ocChose || eOp == ocIfna || eOp == ocIfError ) { // Jumps ignorieren, vorheriges Result (Condition) poppen if ( sp ) --sp; @@ -1199,9 +1199,10 @@ FormulaToken* FormulaTokenArray::AddOpCode( OpCode eOp ) case ocIf: case ocChose: case ocIfna: + case ocIfError: { short nJump[MAXJUMPCOUNT + 1]; - nJump[ 0 ] = ocIf == eOp ? 3 : (ocIfna == eOp ? 2 : MAXJUMPCOUNT+1); + nJump[ 0 ] = ocIf == eOp ? 3 : ((ocIfna == eOp || ocIfError == eOp) ? 2 : MAXJUMPCOUNT+1); pRet = new FormulaJumpToken( eOp, (short*)nJump ); } break; diff --git a/main/formula/source/core/resource/core_resource.src b/main/formula/source/core/resource/core_resource.src index db88f55fd4..cd2911465c 100644 --- a/main/formula/source/core/resource/core_resource.src +++ b/main/formula/source/core/resource/core_resource.src @@ -355,6 +355,7 @@ Resource RID_STRLIST_FUNCTION_NAMES_ENGLISH_ODFF String SC_OPCODE_BITLSHIFT { Text= "BITLSHIFT" ; }; String SC_OPCODE_BITRSHIFT { Text= "BITRSHIFT" ; }; String SC_OPCODE_IFNA { Text = "IFNA" ; }; + String SC_OPCODE_IFERROR { Text = "IFERROR" ; }; String SC_OPCODE_SKEWP { Text = "SKEWP" ; }; /* BEGIN defined ERROR.TYPE() values. */ @@ -699,6 +700,7 @@ Resource RID_STRLIST_FUNCTION_NAMES_ENGLISH String SC_OPCODE_BITLSHIFT { Text= "BITLSHIFT" ; }; String SC_OPCODE_BITRSHIFT { Text= "BITRSHIFT" ; }; String SC_OPCODE_IFNA { Text = "IFNA" ; }; + String SC_OPCODE_IFERROR { Text = "IFERROR" ; }; String SC_OPCODE_SKEWP { Text = "SKEWP" ; }; /* BEGIN defined ERROR.TYPE() values. */ @@ -1952,6 +1954,10 @@ Resource RID_STRLIST_FUNCTION_NAMES { Text [ en-US ] = "IFNA"; }; + String SC_OPCODE_IFERROR + { + Text [ en-US ] = "IFERROR"; + }; /* BEGIN defined ERROR.TYPE() values. */ /* ERROR.TYPE( #NULL! ) == 1 */ String SC_OPCODE_ERROR_NULL diff --git a/main/sc/inc/helpids.h b/main/sc/inc/helpids.h index db6713b8ac..7bd4e35ee6 100644 --- a/main/sc/inc/helpids.h +++ b/main/sc/inc/helpids.h @@ -780,4 +780,5 @@ #define HID_FUNC_BITLSHIFT "SC_HID_FUNC_BITLSHIFT" #define HID_FUNC_BITRSHIFT "SC_HID_FUNC_BITRSHIFT" #define HID_FUNC_IFNA "SC_HID_FUNC_IFNA" +#define HID_FUNC_IFERROR "SC_HID_FUNC_IFERROR" #define HID_FUNC_SKEWP "SC_HID_FUNC_SKEWP" diff --git a/main/sc/source/core/inc/interpre.hxx b/main/sc/source/core/inc/interpre.hxx index 8fbd240954..bc868204d3 100644 --- a/main/sc/source/core/inc/interpre.hxx +++ b/main/sc/source/core/inc/interpre.hxx @@ -793,6 +793,8 @@ void ScStandard(); void ScSkew(); void ScSkewP(); void ScIfna(); +void ScIfError(); +void ScIfnaOrError( bool bAnyError ); void ScMedian(); double GetMedian( ::std::vector<double> & rArray ); double GetPercentile( ::std::vector<double> & rArray, double fPercentile ); diff --git a/main/sc/source/core/tool/interpr1.cxx b/main/sc/source/core/tool/interpr1.cxx index da1655752c..2c7a56b6a1 100644 --- a/main/sc/source/core/tool/interpr1.cxx +++ b/main/sc/source/core/tool/interpr1.cxx @@ -321,9 +321,24 @@ void ScInterpreter::ScChoseJump() aCode.Jump( pJump[ nJumpCount ], pJump[ nJumpCount ] ); } +static bool lcl_IsIfnaHit( sal_uInt16 nErr, bool bAnyError ) +{ + return bAnyError ? (nErr != 0) : (nErr == NOTAVAILABLE); +} + void ScInterpreter::ScIfna() { - RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIfna" ); + ScIfnaOrError( false ); +} + +void ScInterpreter::ScIfError() +{ + ScIfnaOrError( true ); +} + +void ScInterpreter::ScIfnaOrError( bool bAnyError ) +{ + RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "sc", "er", "ScInterpreter::ScIfnaOrError" ); const short* pJump = pCur->GetJump(); bool bIsNA = false; MatrixDoubleRefToMatrix(); @@ -338,11 +353,11 @@ void ScInterpreter::ScIfna() { ScAddress aAdr; PopDoubleRefOrSingleRef( aAdr ); - if ( nGlobalError == NOTAVAILABLE ) + if ( lcl_IsIfnaHit( nGlobalError, bAnyError ) ) bIsNA = true; else if ( nGlobalError ) PushError( nGlobalError ); - else if ( GetCellErrCode( GetCell( aAdr ) ) == NOTAVAILABLE ) + else if ( lcl_IsIfnaHit( GetCellErrCode( GetCell( aAdr ) ), bAnyError ) ) bIsNA = true; else PushCellResultToken( false, aAdr, &nFuncFmtType, &nFuncFmtIndex ); @@ -372,7 +387,7 @@ void ScInterpreter::ScIfna() bool bAnyNA = false; for ( SCSIZE nC = 0; nC < nCols && !bAnyNA; ++nC ) for ( SCSIZE nR = 0; nR < nRows && !bAnyNA; ++nR ) - bAnyNA = pMat->IsValue( nC, nR ) && pMat->GetError( nC, nR ) == NOTAVAILABLE; + bAnyNA = pMat->IsValue( nC, nR ) && lcl_IsIfnaHit( pMat->GetError( nC, nR ), bAnyError ); if ( !bAnyNA ) { // nothing to substitute, the fallback is not evaluated @@ -389,7 +404,7 @@ void ScInterpreter::ScIfna() { for ( SCSIZE nR = 0; nR < nRows; ++nR ) { - if ( pMat->IsValue( nC, nR ) && pMat->GetError( nC, nR ) == NOTAVAILABLE ) + if ( pMat->IsValue( nC, nR ) && lcl_IsIfnaHit( pMat->GetError( nC, nR ), bAnyError ) ) pJumpMat->SetJump( nC, nR, 1.0, pJump[ 1 ], pJump[ 2 ] ); else { @@ -414,8 +429,8 @@ void ScInterpreter::ScIfna() return; } default: - if ( nGlobalError == NOTAVAILABLE || (GetStackType() == svError && - pStack[ sp - 1 ]->GetError() == NOTAVAILABLE) ) + if ( lcl_IsIfnaHit( nGlobalError, bAnyError ) || (GetStackType() == svError && + lcl_IsIfnaHit( pStack[ sp - 1 ]->GetError(), bAnyError )) ) { Pop(); bIsNA = true; diff --git a/main/sc/source/core/tool/interpr4.cxx b/main/sc/source/core/tool/interpr4.cxx index 86184201a0..600b2ae3f1 100644 --- a/main/sc/source/core/tool/interpr4.cxx +++ b/main/sc/source/core/tool/interpr4.cxx @@ -3398,7 +3398,7 @@ StackVar ScInterpreter::Interpret() // RPN code push without error PushWithoutError( (FormulaToken&) *pCur ); } - else if (pTokenMatrixMap && !(eOp == ocIf || eOp == ocChose || eOp == ocIfna) && + else if (pTokenMatrixMap && !(eOp == ocIf || eOp == ocChose || eOp == ocIfna || eOp == ocIfError) && ((aTokenMatrixMapIter = pTokenMatrixMap->find( pCur)) != pTokenMatrixMap->end()) && (*aTokenMatrixMapIter).second->GetType() != svJumpMatrix) @@ -3419,7 +3419,7 @@ StackVar ScInterpreter::Interpret() nFuncFmtType = NUMBERFORMAT_NUMBER; nFuncFmtIndex = 0; - if ( eOp == ocIf || eOp == ocChose || eOp == ocIfna ) + if ( eOp == ocIf || eOp == ocChose || eOp == ocIfna || eOp == ocIfError ) nStackBase = sp; // don't mess around with the jumps else { @@ -3449,6 +3449,7 @@ StackVar ScInterpreter::Interpret() case ocExternalRef : ScExternalRef(); break; case ocIf : ScIfJump(); break; case ocIfna : ScIfna(); break; + case ocIfError : ScIfError(); break; case ocChose : ScChoseJump(); break; case ocAdd : ScAdd(); break; case ocSub : ScSub(); break; @@ -3855,6 +3856,7 @@ StackVar ScInterpreter::Interpret() case ocIsErr : \ case ocIsError : \ case ocIfna : \ + case ocIfError : \ case ocIsFormula : \ case ocIsLogical : \ case ocIsNA : \ diff --git a/main/sc/source/core/tool/parclass.cxx b/main/sc/source/core/tool/parclass.cxx index 2309fbe753..972874478d 100644 --- a/main/sc/source/core/tool/parclass.cxx +++ b/main/sc/source/core/tool/parclass.cxx @@ -63,6 +63,7 @@ const ScParameterClassification::RawData ScParameterClassification::pRawData[] = { ocIf, {{ Array, Reference, Reference }, 0 }}, { ocChose, {{ Array, Reference }, 1 }}, { ocIfna, {{ Reference, Reference }, 0 }}, + { ocIfError, {{ Reference, Reference }, 0 }}, // Other specials. { ocOpen, {{ Bounds }, 0 }}, { ocClose, {{ Bounds }, 0 }}, diff --git a/main/sc/source/core/tool/token.cxx b/main/sc/source/core/tool/token.cxx index 3ddd04eff9..8e3d9e6348 100644 --- a/main/sc/source/core/tool/token.cxx +++ b/main/sc/source/core/tool/token.cxx @@ -161,6 +161,7 @@ void ScRawToken::SetOpCode( OpCode e ) nJump[ 0 ] = MAXJUMPCOUNT+1; break; case ocIfna: + case ocIfError: eType = svJump; nJump[ 0 ] = 2; // Fallback, Behind break; diff --git a/main/sc/source/ui/src/scfuncs.src b/main/sc/source/ui/src/scfuncs.src index 93cc3e0448..bdc41be4b2 100644 --- a/main/sc/source/ui/src/scfuncs.src +++ b/main/sc/source/ui/src/scfuncs.src @@ -1056,6 +1056,38 @@ Resource RID_SC_FUNCTION_DESCRIPTIONS1 Text [ en-US ] = "The value to be returned if Value is an #N/A error." ; }; }; + // -=*# Resource for function IFERROR #*=- + Resource SC_OPCODE_IFERROR + { + String 1 // Description + { + Text [ en-US ] = "Returns the value if it is not an error value, otherwise it returns the alternative value." ; + }; + ExtraData = + { + 0; + ID_FUNCTION_GRP_LOGIC; + U2S( HID_FUNC_IFERROR ); + 2; 0; 0; + 0; + }; + String 2 // Name of Parameter 1 + { + Text [ en-US ] = "Value" ; + }; + String 3 // Description of Parameter 1 + { + Text [ en-US ] = "The value to be checked for an error value." ; + }; + String 4 // Name of Parameter 2 + { + Text [ en-US ] = "Alternative" ; + }; + String 5 // Description of Parameter 2 + { + Text [ en-US ] = "The value to be returned if Value is an error value." ; + }; + }; // -=*# Resource for function BW #*=- Resource SC_OPCODE_BW { diff --git a/main/sc/util/hidother.src b/main/sc/util/hidother.src index 86014604a7..2d6a034435 100644 --- a/main/sc/util/hidother.src +++ b/main/sc/util/hidother.src @@ -492,4 +492,5 @@ hidspecial HID_FUNC_BITXOR { HelpID = HID_FUNC_BITXOR; }; hidspecial HID_FUNC_BITLSHIFT { HelpID = HID_FUNC_BITLSHIFT; }; hidspecial HID_FUNC_BITRSHIFT { HelpID = HID_FUNC_BITRSHIFT; }; hidspecial HID_FUNC_IFNA { HelpID = HID_FUNC_IFNA; }; +hidspecial HID_FUNC_IFERROR { HelpID = HID_FUNC_IFERROR; }; hidspecial HID_FUNC_SKEWP { HelpID = HID_FUNC_SKEWP; }; diff --git a/test/odf-validate/calcval-cases14.tsv b/test/odf-validate/calcval-cases14.tsv index 0ee7901b17..aa84c89f14 100644 --- a/test/odf-validate/calcval-cases14.tsv +++ b/test/odf-validate/calcval-cases14.tsv @@ -153,6 +153,34 @@ IFNA.arr.sum =SUM(IFNA(AN1:AN4;0)) array 5 6.x IFNA inside SUM in an array formu IFNA.arr.nested =IFNA(IFNA(AN1:AN4;AN1:AN4);0) array:1x4 5|0|s:x|0 6.x nested: the inner fallback is #N/A again and the outer IFNA replaces it IFNA.arr.inif =IF(D1:D3>15;IFNA(AN2:AN4;1);0) array:1x3 0|s:x|1 6.x IFNA array in an IF path (AN2:AN4 = #N/A, x, #N/A) IFNA.arr.sumprod =SUMPRODUCT(IFNA(AN1:AN2+0;100)) cell - 6.x record only: whether the force-array context reaches the operator inside IFNA +IFNA.onearg =IFNA(1/0) cell - 6.x record only: one argument, the partner of IFERROR.onearg +IFNA.emptyalt =IFNA(NA();) cell - 6.x record only: empty alternative, the partner of IFERROR.emptyalt +IFERROR.num =IFERROR(5;10) cell 5 6.x not an error, returned unchanged +IFERROR.text =IFERROR("hello";"world") cell s:hello 6.x text returned unchanged +IFERROR.na =IFERROR(NA();10) cell 10 6.x #N/A replaced +IFERROR.div0 =IFERROR(1/0;10) cell 10 6.x #DIV/0! replaced +IFERROR.err502 =IFERROR(SQRT(-1);10) cell 10 6.x Err:502 replaced +IFERROR.value =IFERROR("a"+1;10) cell 10 6.x #VALUE! replaced +IFERROR.ref =IFERROR(INDIRECT("ZZZ1048577");10) cell 10 6.x #REF! replaced +IFERROR.text.alt =IFERROR(1/0;"fallback") cell s:fallback 6.x text alternative +IFERROR.lazy.ok =IFERROR(5;1/0) cell 5 6.x alternative not evaluated +IFERROR.lazy.err =IFERROR(1/0;SQRT(-1)) cell #ERR 6.x error in the alternative propagates +IFERROR.nested =IFERROR(IFERROR(1/0;NA());10) cell 10 6.x nested +IFERROR.cellref =IFERROR(D1;99) cell 10 6.x reference to a value +IFERROR.cellref.text =IFERROR(A1;99) cell s:y 6.x reference to text +IFERROR.cellref.na =IFERROR(AM1;99) cell 99 6.x reference to #N/A +IFERROR.cellref.err =IFERROR(AM2;99) cell 99 6.x reference to #DIV/0! +IFERROR.cellref.empty =IFERROR(AM4;99) cell 0 6.x empty cell is not an error +IFERROR.range =IFERROR(D1:D3;99) cell 10 6.x implicit intersection as IFNA.range +IFERROR.arr.sub =IFERROR(AN1:AN6;99) array:1x6 5|99|s:x|99|7|99 6.x every error element replaced +IFERROR.arr.fallback =IFERROR(AN1:AN4;D1:D4) array:1x4 5|20|s:x|40 6.x array alternative element by element +IFERROR.arr.none =IFERROR(D1:D3;99) array:1x3 10|20|30 6.x no error, unchanged +IFERROR.arr.lazy =IFERROR(D1:D3;1/0) array:1x3 10|20|30 6.x alternative not evaluated +IFERROR.arr.fallerr =IFERROR(AN1:AN2;1/0) array:1x2 5|#ERR 6.x alternative only for the error element +IFERROR.arr.inif =IF(D1:D3>15;IFERROR(AN2:AN4;1);0) array:1x3 0|s:x|1 6.x inside an IF path, as IFNA.arr.inif +IFERROR.arr.sumprod =SUMPRODUCT(IFERROR(AN1:AN2+0;100)) cell - 6.x record only; must equal IFNA.arr.sumprod's recorded result +IFERROR.onearg =IFERROR(1/0) cell - 6.x record only; must equal IFNA.onearg's recorded result +IFERROR.emptyalt =IFERROR(1/0;) cell - 6.x record only; must equal IFNA.emptyalt's recorded result SKEWP.two =SKEWP(1;2) cell 0 6.x two observations, non-constant, zero skewness SKEW.text =SKEW(1;"text";2) cell - 6.3.7 a direct Text argument converts as in 6.3.5, implementation-defined (0 or an Error); AOO returns an Error SKEW.err =SKEW(1/0) cell #ERR 6.x regression: error argument propagates
