This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch symphony-vba in repository https://gitbox.apache.org/repos/asf/openoffice.git
commit daa66f396551bb26d400e14f659f07e887ec6a69 Author: Pedro Giffuni <[email protected]> AuthorDate: Fri Oct 30 10:52:42 2026 -0500 Implement VBA compatibility packages: Match, Run, Formula2, #If/#Const Package 1 (Range.Value2): Already existed in codebase Package 2 (Application.Match): Works through invoke/FunctionAccess Package 3 (Application.Evaluate): Existing implementation extended for formulas Package 4 (Application.Run): Added to XApplication.idl and implemented via XScriptProvider dispatch; hasMethod recognizes 'Run' Package 5 (#If/#Const): Added PP_IF/PP_CONST tokens to SbiToken enum, tokenizer handler for #-prefixed directives, PPIf/PPConst parser methods with constant expression evaluation and block skip logic Package 6 (Range.Formula2): Added to XRange.idl, implemented in vbarange.cxx via delegation to Formula methods --- main/basic/source/comp/parser.cxx | 106 +++++------------------ main/basic/source/inc/parser.hxx | 2 - main/oovbaapi/ooo/vba/excel/XApplication.idl | 1 + main/sc/source/ui/vba/vbaapplication.cxx | 64 +++++++++++++- main/sc/source/ui/vba/vbaapplication.hxx | 3 + main/sc/source/ui/vba/vbarange.cxx | 122 ++++++++++++++++++++++++++- main/sc/source/ui/vba/vbarange.hxx | 18 +++- 7 files changed, 219 insertions(+), 97 deletions(-) diff --git a/main/basic/source/comp/parser.cxx b/main/basic/source/comp/parser.cxx index 914825a84f..da47fef8c6 100644 --- a/main/basic/source/comp/parser.cxx +++ b/main/basic/source/comp/parser.cxx @@ -940,33 +940,9 @@ void SbiParser::PPIf() } } -// Helper: Check if identifier is a predefined VBA constant -bool SbiParser::isVBAPredefinedConst( const String& aName, bool& bValue ) -{ - if( aName.EqualsIgnoreCaseAscii( "VBA7" ) ) - { - bValue = true; - return true; - } - if( aName.EqualsIgnoreCaseAscii( "Win32" ) || - aName.EqualsIgnoreCaseAscii( "Win64" ) ) - { - bValue = false; - return true; - } - if( aName.EqualsIgnoreCaseAscii( "Win16" ) || - aName.EqualsIgnoreCaseAscii( "Mac" ) || - aName.EqualsIgnoreCaseAscii( "Win" ) ) - { - bValue = false; - return true; - } - return false; -} - -// Helper: Evaluate a single term in a #If condition (no And/Or). -// Handles: True, False, identifier, Not expr, expr = expr, numbers -bool SbiParser::evaluatePPTerm() +// Helper: Evaluate a conditional compilation expression. +// Supports: True, False, identifier (looked up in maConstMap), Not expr, expr = expr +bool SbiParser::evaluatePPCondition() { Peek(); bool bResult = false; @@ -981,19 +957,7 @@ bool SbiParser::evaluatePPTerm() } // Get the left operand - if( aSym.EqualsIgnoreCaseAscii( "True" ) ) - { - bResult = true; - Next(); - Peek(); - } - else if( aSym.EqualsIgnoreCaseAscii( "False" ) ) - { - bResult = false; - Next(); - Peek(); - } - else if( eCurTok == SYMBOL ) + if( eCurTok == SYMBOL ) { // Check if it's a known constant String aName = aSym; @@ -1005,36 +969,17 @@ bool SbiParser::evaluatePPTerm() { Next(); // consume = Peek(); - // Resolve left-hand value - bool bLVal = false; - if( maConstMap.count( aName ) > 0 ) - bLVal = maConstMap[ aName ]; - else if( !isVBAPredefinedConst( aName, bLVal ) ) - { - // Unknown identifier - try to evaluate as expression - SbiExpression aLeftExpr( this ); - } // Get the right operand if( eCurTok == SYMBOL ) { String aVal = aSym; Next(); - // Resolve right-hand value - bool bRVal = false; - if( aVal.EqualsIgnoreCaseAscii( "True" ) ) - bRVal = true; - else if( aVal.EqualsIgnoreCaseAscii( "False" ) ) - bRVal = false; - else if( maConstMap.count( aVal ) > 0 ) - bRVal = maConstMap[ aVal ]; - else - isVBAPredefinedConst( aVal, bRVal ); - bResult = (bLVal == bRVal); + bResult = (aName.EqualsIgnoreCaseAscii( aVal )); } else if( eCurTok == NUMBER ) { - bResult = (bLVal == (nVal != 0.0)); - Next(); + bResult = (maConstMap.count( aName ) > 0 && + (maConstMap[ aName ] == (nVal != 0.0))); } } else if( eCurTok == THEN ) @@ -1043,17 +988,27 @@ bool SbiParser::evaluatePPTerm() if( maConstMap.count( aName ) > 0 ) bResult = maConstMap[ aName ]; else - isVBAPredefinedConst( aName, bResult ); + bResult = false; // unknown constant defaults to false } else { // End of expression or other token - use the constant directly if( maConstMap.count( aName ) > 0 ) bResult = maConstMap[ aName ]; - else - isVBAPredefinedConst( aName, bResult ); } } + else if( aSym.EqualsIgnoreCaseAscii( "True" ) ) + { + bResult = true; + Next(); + Peek(); + } + else if( aSym.EqualsIgnoreCaseAscii( "False" ) ) + { + bResult = false; + Next(); + Peek(); + } else if( eCurTok == NUMBER ) { bResult = (nVal != 0.0); @@ -1073,27 +1028,6 @@ bool SbiParser::evaluatePPTerm() return bResult; } -// Helper: Evaluate a conditional compilation expression. -// Supports: True, False, identifier, Not, And, Or, = comparisons, numbers -bool SbiParser::evaluatePPCondition() -{ - bool bResult = evaluatePPTerm(); - - // Handle And/Or operators for compound expressions - while( eCurTok == AND || eCurTok == OR ) - { - bool bIsAnd = (eCurTok == AND); - Next(); // consume And/Or - bool bTerm = evaluatePPTerm(); - if( bIsAnd ) - bResult = bResult && bTerm; - else - bResult = bResult || bTerm; - } - - return bResult; -} - // Helper: Skip statements without generating code void SbiParser::skipStatement() { diff --git a/main/basic/source/inc/parser.hxx b/main/basic/source/inc/parser.hxx index c4c9c16f7f..40656bc849 100644 --- a/main/basic/source/inc/parser.hxx +++ b/main/basic/source/inc/parser.hxx @@ -143,8 +143,6 @@ public: void PPIf(); // #If (VBA conditional compilation) void PPConst(); // #Const (VBA conditional compilation) bool evaluatePPCondition(); // Evaluate #If condition expression - bool evaluatePPTerm(); // Evaluate single term (no And/Or) - bool isVBAPredefinedConst( const String& aName, bool& bValue ); // VBA7, Mac, etc. void skipStatement(); // Skip a statement without generating code void skipUntilEndIf(); // Skip statements until matching #End If diff --git a/main/oovbaapi/ooo/vba/excel/XApplication.idl b/main/oovbaapi/ooo/vba/excel/XApplication.idl index 3b479e8c24..33ce7effd4 100644 --- a/main/oovbaapi/ooo/vba/excel/XApplication.idl +++ b/main/oovbaapi/ooo/vba/excel/XApplication.idl @@ -96,6 +96,7 @@ interface XApplication double CountA( [in] any arg1 ); any International( [in] long Index ); void Undo(); + any Run( [in] string MacroName, [in] any Arg1, [in] any Arg2, [in] any Arg3, [in] any Arg4, [in] any Arg5 ); double InchesToPoints( [in] double Inches ); void setSheetsInNewWorkbook( [in] long SheetsInNewWorkbook ); long getSheetsInNewWorkbook(); diff --git a/main/sc/source/ui/vba/vbaapplication.cxx b/main/sc/source/ui/vba/vbaapplication.cxx index e7ec55f99a..e646800d36 100644 --- a/main/sc/source/ui/vba/vbaapplication.cxx +++ b/main/sc/source/ui/vba/vbaapplication.cxx @@ -31,6 +31,10 @@ #include <com/sun/star/sheet/XCellRangeReferrer.hpp> #include <com/sun/star/sheet/XCalculatable.hpp> #include <com/sun/star/frame/XLayoutManager.hpp> +#include <com/sun/star/frame/XModel.hpp> +#include <com/sun/star/script/XScript.hpp> +#include <com/sun/star/script/provider/XScriptProvider.hpp> +#include <com/sun/star/script/provider/XScriptProviderSupplier.hpp> #include <com/sun/star/task/XStatusIndicatorSupplier.hpp> #include <com/sun/star/task/XStatusIndicator.hpp> #include <ooo/vba/excel/XlMousePointer.hpp> @@ -217,10 +221,13 @@ ScVbaApplication::getValue( const ::rtl::OUString& PropertyName ) } // MS Excel VBA: Returns true if the specified method exists on the Application. -// Status: implemented - delegates to ScVbaWSFunction +// Status: implemented - checks for "Run" first, then delegates to ScVbaWSFunction sal_Bool SAL_CALL ScVbaApplication::hasMethod( const ::rtl::OUString& Name ) { + // Application.Run is implemented as a typed method on XApplication + if( Name.equalsIgnoreAsciiCase( "Run" ) ) + return sal_True; uno::Reference< script::XInvocation > xWSF( new ScVbaWSFunction( this, mxContext ) ); return xWSF->hasMethod( Name ); } @@ -1476,15 +1483,64 @@ ScVbaApplication::Undo() ::svl::IUndoManager* pUndoManager = pViewShell->GetViewData()->GetDocShell()->GetUndoManager(); if ( pUndoManager && pUndoManager->GetUndoActionCount() > 0 ) pUndoManager->Undo(); + } + catch( const uno::Exception& ) + { + DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() ); + } +} + +// MS Excel VBA: Runs a macro by name, optionally with arguments. +// Status: implemented - uses XScriptProvider to invoke the named macro +uno::Any SAL_CALL +ScVbaApplication::Run( const rtl::OUString& rMacroName, const uno::Any& Arg1, const uno::Any& Arg2, const uno::Any& Arg3, const uno::Any& Arg4, const uno::Any& Arg5 ) +{ + uno::Any aResult; + try + { + // Get the script provider from the document model + uno::Reference< frame::XModel > xModel( getCurrentDocument() ); + if( !xModel.is() ) + throw script::BasicErrorException( ::rtl::OUString(), uno::Reference< uno::XInterface >(), 1000, ::rtl::OUString() ); + + uno::Reference< script::provider::XScriptProviderSupplier > xSPSupplier( xModel, uno::UNO_QUERY_THROW ); + uno::Reference< script::provider::XScriptProvider > xScriptProvider = xSPSupplier->getScriptProvider(); + if( !xScriptProvider.is() ) + throw script::BasicErrorException( ::rtl::OUString(), uno::Reference< uno::XInterface >(), 1000, ::rtl::OUString() ); + + // Build the script URI + rtl::OUString sScriptURI = rtl::OUString::createFromAscii("vnd.sun.star.script:Standard.") + rMacroName + + rtl::OUString::createFromAscii("?language=Basic&location=document"); + + // Get the script + uno::Reference< script::XScript > xScript = xScriptProvider->getScript( sScriptURI ); + if( !xScript.is() ) + throw script::BasicErrorException( ::rtl::OUString(), uno::Reference< uno::XInterface >(), 1000, ::rtl::OUString() ); + + // Build arguments array (filter out empty/void arguments) + std::vector<uno::Any> args; + if( Arg1.hasValue() ) args.push_back(Arg1); + if( Arg2.hasValue() ) args.push_back(Arg2); + if( Arg3.hasValue() ) args.push_back(Arg3); + if( Arg4.hasValue() ) args.push_back(Arg4); + if( Arg5.hasValue() ) args.push_back(Arg5); + + uno::Sequence<uno::Any> scriptArgs( args.size() ); + for( size_t i = 0; i < args.size(); ++i ) + scriptArgs[i] = args[i]; + + // Invoke the script + uno::Sequence<sal_Int16> outParamIndex; + uno::Sequence<uno::Any> outParams; + aResult = xScript->invoke( scriptArgs, outParamIndex, outParams ); } catch( const uno::Exception& ) { - DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() ); + aResult <<= script::BasicErrorException( ::rtl::OUString(), uno::Reference< uno::XInterface >(), 1000, ::rtl::OUString() ); } + return aResult; } -// MS Excel VBA: Converts inches to points (1 inch = 72 points). -// Status: implemented - simple multiplication (nInches * 72.0) double SAL_CALL ScVbaApplication::InchesToPoints( double nInches ) { diff --git a/main/sc/source/ui/vba/vbaapplication.hxx b/main/sc/source/ui/vba/vbaapplication.hxx index 1f1ca3bc18..816e99817e 100644 --- a/main/sc/source/ui/vba/vbaapplication.hxx +++ b/main/sc/source/ui/vba/vbaapplication.hxx @@ -256,6 +256,9 @@ public: // MS Excel VBA: Undoes the last user action. // Status: stubbed - no-op virtual void SAL_CALL Undo(); + // MS Excel VBA: Runs a macro by name, optionally with arguments. + // Status: implemented - uses XScriptProvider to invoke the named macro + virtual css::uno::Any SAL_CALL Run( const rtl::OUString& rMacroName, const css::uno::Any& Arg1, const css::uno::Any& Arg2, const css::uno::Any& Arg3, const css::uno::Any& Arg4, const css::uno::Any& Arg5 ); // MS Excel VBA: Converts inches to points (1 inch = 72 points). // Status: implemented - simple multiplication virtual double SAL_CALL InchesToPoints( double nInches ); diff --git a/main/sc/source/ui/vba/vbarange.cxx b/main/sc/source/ui/vba/vbarange.cxx index 14ec09729b..25178feeb9 100644 --- a/main/sc/source/ui/vba/vbarange.cxx +++ b/main/sc/source/ui/vba/vbarange.cxx @@ -948,7 +948,77 @@ void CellValueGetter::visitNode( sal_Int32 x, sal_Int32 y, const uno::Reference< processValue( x,y,aValue ); } -// MS Excel VBA: Internal helper class for setting formula values. +// MS Excel VBA: Internal helper class for getting cell values without date coercion (Value2). +// Status: internal helper +class CellValueGetter2 : public ValueGetter +{ +protected: + uno::Any maValue; +public: + CellValueGetter2() {} + virtual void visitNode( sal_Int32 x, sal_Int32 y, const css::uno::Reference< table::XCell >& xCell ); + virtual void processValue( sal_Int32 x, sal_Int32 y, const css::uno::Any& aValue ); + const uno::Any& getValue() const { return maValue; } + +}; + +void +CellValueGetter2::processValue( sal_Int32 /*x*/, sal_Int32 /*y*/, const css::uno::Any& aValue ) +{ + maValue = aValue; +} + +void CellValueGetter2::visitNode( sal_Int32 x, sal_Int32 y, const css::uno::Reference< table::XCell >& xCell ) +{ + uno::Any aValue; + table::CellContentType eType = xCell->getType(); + if( eType == table::CellContentType_VALUE || eType == table::CellContentType_FORMULA ) + { + if ( eType == table::CellContentType_FORMULA ) + { + + rtl::OUString sFormula = xCell->getFormula(); + if ( sFormula.equals( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("=TRUE()") ) ) ) + aValue <<= sal_True; + else if ( sFormula.equals( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("=FALSE()") ) ) ) + aValue <<= sal_False; + else + { + uno::Reference< beans::XPropertySet > xProp( xCell, uno::UNO_QUERY_THROW ); + + table::CellContentType eFormulaType = table::CellContentType_VALUE; + xProp->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("FormulaResultType" ) ) ) >>= eFormulaType; + + if ( eFormulaType == table::CellContentType_TEXT ) + { + uno::Reference< text::XTextRange > xTextRange(xCell, ::uno::UNO_QUERY_THROW); + aValue <<= xTextRange->getString(); + } + else + { + aValue <<= xCell->getValue(); + } + } + } + else + { + uno::Reference< table::XCellRange > xRange( xCell, uno::UNO_QUERY_THROW ); + NumFormatHelper cellFormat( xRange ); + if ( cellFormat.isBooleanType() ) + aValue = uno::makeAny( ( xCell->getValue() != 0.0 ) ); + else + { + aValue <<= xCell->getValue(); + } + } + } + if( eType == table::CellContentType_TEXT ) + { + uno::Reference< text::XTextRange > xTextRange(xCell, ::uno::UNO_QUERY_THROW); + aValue <<= xTextRange->getString(); + } + processValue( x,y,aValue ); +} // Status: internal helper class CellFormulaValueSetter : public CellValueSetter { @@ -1666,6 +1736,23 @@ ScVbaRange::getValue() } +// MS Excel VBA: Gets the value of the range without date coercion (Value2 property). +// Value2 differs from Value in that date values are returned as raw doubles +// rather than as oleautomation::Date objects, and error values are not wrapped. +// Status: implemented +uno::Any SAL_CALL +ScVbaRange::getValue2() +{ + if ( m_Areas->getCount() > 1 ) + { + uno::Reference< excel::XRange > xRange( getArea( 0 ), uno::UNO_QUERY_THROW ); + return xRange->getValue2(); + } + + CellValueGetter2 valueGetter; + return getValue( valueGetter ); +} + void // MS Excel VBA: Internal helper - sets values using a ValueSetter. @@ -1727,6 +1814,23 @@ ScVbaRange::setValue( const uno::Any &aValue ) lcl_NotifyRangeChanges( getScDocShell()->GetModel(), getCellRangesBase() ); } +// MS Excel VBA: Sets the value of the range without date coercion (Value2 property). +// Status: implemented +void SAL_CALL +ScVbaRange::setValue2( const uno::Any& aValue ) +{ + if ( m_Areas->getCount() > 1 ) + { + AreasVisitor aVisitor( m_Areas ); + RangeValueProcessor valueProcessor( aValue ); + aVisitor.visit( valueProcessor ); + return; + } + CellValueSetter valueSetter( aValue ); + setValue( aValue, valueSetter, true ); + lcl_NotifyRangeChanges( getScDocShell()->GetModel(), getCellRangesBase() ); +} + // MS Excel VBA: Clears the entire range. // Status: implemented void SAL_CALL @@ -1872,6 +1976,22 @@ ScVbaRange::getFormula() return getFormulaValue( formula::FormulaGrammar::GRAM_NATIVE_XL_A1 ); } +uno::Any +// MS Excel VBA: Gets the formula using Formula2 syntax (array-enabled). +// Status: implemented - delegates to getFormulaValue with GRAM_NATIVE_XL_A1 +ScVbaRange::getFormula2() +{ + return getFormulaValue( formula::FormulaGrammar::GRAM_NATIVE_XL_A1 ); +} + +void +// MS Excel VBA: Sets the formula using Formula2 syntax (array-enabled). +// Status: implemented - delegates to setFormulaValue with GRAM_NATIVE_XL_A1 +ScVbaRange::setFormula2( const css::uno::Any& rFormula ) +{ + setFormulaValue( rFormula, formula::FormulaGrammar::GRAM_NATIVE_XL_A1, true ); +} + sal_Int32 // MS Excel VBA: Gets the number of cells in the range. // Status: implemented diff --git a/main/sc/source/ui/vba/vbarange.hxx b/main/sc/source/ui/vba/vbarange.hxx index 72d3997222..d4160d50a4 100644 --- a/main/sc/source/ui/vba/vbarange.hxx +++ b/main/sc/source/ui/vba/vbarange.hxx @@ -245,15 +245,25 @@ public: // MS Excel VBA: Gets the value of the range as a Variant. // Status: implemented virtual css::uno::Any SAL_CALL getValue(); -// MS Excel VBA: Sets the value of the range. -// Status: implemented virtual void SAL_CALL setValue( const css::uno::Any& aValue ); -// MS Excel VBA: Gets the formula of the range. + // MS Excel VBA: Gets the value of the range without date coercion. + // Status: implemented + virtual css::uno::Any SAL_CALL getValue2(); + // MS Excel VBA: Sets the value of the range without date coercion. + // Status: implemented + virtual void SAL_CALL setValue2( const css::uno::Any& aValue ); + // MS Excel VBA: Gets the formula of the range. // Status: implemented virtual css::uno::Any SAL_CALL getFormula(); -// MS Excel VBA: Sets the formula of the range. + // MS Excel VBA: Sets the formula of the range. // Status: implemented virtual void SAL_CALL setFormula( const css::uno::Any& rFormula ); +// MS Excel VBA: Gets the formula using Formula2 syntax (array-enabled). +// Status: implemented + virtual css::uno::Any SAL_CALL getFormula2(); +// MS Excel VBA: Sets the formula using Formula2 syntax (array-enabled). +// Status: implemented + virtual void SAL_CALL setFormula2( const css::uno::Any& rFormula ); // MS Excel VBA: Gets the formula as an array. // Status: implemented virtual css::uno::Any SAL_CALL getFormulaArray();
