Using Calc Functions in Macros/text/sbasic/shared/calc_functions.xhpcalling Calc function;macrossetting Calc function;macrosmacros;calling Calc functionmacros;setting Calc functioncreateUNOservice function;calling Calc functionAPI;sheet.addin.AnalysisAPI;sheet.FunctionAccess
Using Calc Functions in Macros
In addition to the native BASIC functions, you can call Calc functions in your macros and scripts and set Calc functions in cell formulas.
Calling Internal Calc functions in Basic
Use the CreateUNOService function to access the com.sun.star.sheet.FunctionAccess service.The example below creates a function named MyVlook that calls the VLOOKUP Calc function over a data array passed as argument and returns the value found by the function.Function MyVlook(Lookup, DataArray As Object, Index As Integer, SortedRangeLookup as Byte) Dim oService As Object Set oService = createUnoService("com.sun.star.sheet.FunctionAccess") ' Always use the function name in English MyVlook = oService.callFunction("VLOOKUP", Array(Lookup, DataArray, Index, SortedRangeLookup))End FunctionThe macro below presents an example of how the MyVlook function can be called. It first creates a 5-by-2 data array and then calls the function MyVlook and shows the returned value using MsgBox.Sub CallingMyVlook() ' Creates a 5 by 2 array and fills it with data Dim myData(1 to 5, 1 to 2) as Variant myData(1, 1) = 1 : myData(1, 2) = "Strongly disagree" myData(2, 1) = 3 : myData(2, 2) = "Disagree" myData(3, 1) = 5 : myData(3, 2) = "Undecided" myData(4, 1) = 7 : myData(4, 2) = "Agree" myData(5, 1) = 9 : myData(5, 2) = "Strongly agree" ' Looks up the data array Dim result as String result = MyVlook(4, myData, 2, 1) ' Shows the message "Disagree" MsgBox resultEnd Sub
Use the formula text string to add a formula to a spreadsheet cell. All Calc functions must be expressed with their English names.Sub AssignFormulaToCellREM Add a formula to cell A1. Function name must be in English. oCell = ThisComponent.Sheets.getByIndex(0).getCellRangeByName("A1") oCell.Formula = "=SUM(B1:B10)"REM Cell A1 displays the localized function nameEnd Sub
Calling Add-In Calc Functions in BASIC
The Calc Add-In functions are in the UNO services com.sun.star.sheet.addin.Analysis, com.sun.star.sheet.addin.DateFunctions and com.sun.star.sheet.addin.PricingFunctions.REM Example calling Add-in function SQRTPIFunction MySQRTPI(arg as double) as double Dim oService as Object oService = createUNOService("com.sun.star.sheet.addin.Analysis") MySQRTPI = oService.getSqrtPi(arg)End Function
Setting Cell Formulas with Add-In Functions
The Add-In function must be expressed by its UNO service name.Sub AssignAddInFormulaToCellREM Add an Add-In formula to cell A1. Function name is the UNO service name. oCell = ThisComponent.Sheets.getByIndex(0).getCellRangeByName("A1") oCell.Formula = "=com.sun.star.sheet.addin.Analysis.getBin2Dec(B1)"REM Cell A1 displays the localized function nameEnd Sub
UNO Service Names for Analysis Add-In Functions
The table below presents a list of all Calc Analysis Add-In functions and their respective UNO service names.
Calc Function nameUNO service nameACCRINTcom.sun.star.sheet.addin.Analysis.getAccrintACCRINTMcom.sun.star.sheet.addin.Analysis.getAccrintmAMORDEGRCcom.sun.star.sheet.addin.Analysis.getAmordegrcAMORLINCcom.sun.star.sheet.addin.Analysis.getAmorlincBESSELIcom.sun.star.sheet.addin.Analysis.getBesseliBESSELJcom.sun.star.sheet.addin.Analysis.getBesseljBESSELKcom.sun.star.sheet.addin.Analysis.getBesselkBESSELYcom.sun.star.sheet.addin.Analysis.getBesselyBIN2DECcom.sun.star.sheet.addin.Analysis.getBin2DecBIN2HEXcom.sun.star.sheet.addin.Analysis.getBin2HexBIN2OCTcom.sun.star.sheet.addin.Analysis.getBin2OctCOMPLEXcom.sun.star.sheet.addin.Analysis.getComplexCONVERTcom.sun.star.sheet.addin.Analysis.getConvertCOUPDAYBScom.sun.star.sheet.addin.Analysis.getCoupdaybsCOUPDAYScom.sun.star.sheet.addin.Analysis.getCoupdaysCOUPDAYSNCcom.sun.star.sheet.addin.Analysis.getCoupdaysncCOUPNCDcom.sun.star.sheet.addin.Analysis.getCoupncdCOUPNUMcom.sun.star.sheet.addin.Analysis.getCoupnumCOUPPCDcom.sun.star.sheet.addin.Analysis.getCouppcdCUMIPMTcom.sun.star.sheet.addin.Analysis.getCumipmtCUMPRINCcom.sun.star.sheet.addin.Analysis.getCumprincDEC2BINcom.sun.star.sheet.addin.Analysis.getDec2BinDEC2HEXcom.sun.star.sheet.addin.Analysis.getDec2HexDEC2OCTcom.sun.star.sheet.addin.Analysis.getDec2OctDELTAcom.sun.star.sheet.addin.Analysis.getDeltaDISCcom.sun.star.sheet.addin.Analysis.getDiscDOLLARDEcom.sun.star.sheet.addin.Analysis.getDollardeDOLLARFRcom.sun.star.sheet.addin.Analysis.getDollarfrDURATIONcom.sun.star.sheet.addin.Analysis.getDurationEDATEcom.sun.star.sheet.addin.Analysis.getEdateEFFECTcom.sun.star.sheet.addin.Analysis.getEffectEOMONTHcom.sun.star.sheet.addin.Analysis.getEomonthERFcom.sun.star.sheet.addin.Analysis.getErfERFCcom.sun.star.sheet.addin.Analysis.getErfcFACTDOUBLEcom.sun.star.sheet.addin.Analysis.getFactdoubleFVSCHEDULEcom.sun.star.sheet.addin.Analysis.getFvscheduleGCDcom.sun.star.sheet.addin.Analysis.getGcdGESTEPcom.sun.star.sheet.addin.Analysis.getGestepHEX2BINcom.sun.star.sheet.addin.Analysis.getHex2BinHEX2DECcom.sun.star.sheet.addin.Analysis.getHex2DecHEX2OCTcom.sun.star.sheet.addin.Analysis.getHex2OctIMABScom.sun.star.sheet.addin.Analysis.getImabsIMAGINARYcom.sun.star.sheet.addin.Analysis.getImaginaryIMARGUMENTcom.sun.star.sheet.addin.Analysis.getImargumentIMCONJUGATEcom.sun.star.sheet.addin.Analysis.getImconjugateIMCOScom.sun.star.sheet.addin.Analysis.getImcosIMCOSHcom.sun.star.sheet.addin.Analysis.getImcoshIMCOTcom.sun.star.sheet.addin.Analysis.getImcotIMCSCcom.sun.star.sheet.addin.Analysis.getImcscIMCSCHcom.sun.star.sheet.addin.Analysis.getImcschIMDIVcom.sun.star.sheet.addin.Analysis.getImdivIMEXPcom.sun.star.sheet.addin.Analysis.getImexpIMLNcom.sun.star.sheet.addin.Analysis.getImlnIMLOG10com.sun.star.sheet.addin.Analysis.getImlog10IMLOG2com.sun.star.sheet.addin.Analysis.getImlog2IMPOWERcom.sun.star.sheet.addin.Analysis.getImpowerIMPRODUCTcom.sun.star.sheet.addin.Analysis.getImproductIMREALcom.sun.star.sheet.addin.Analysis.getImrealIMSECcom.sun.star.sheet.addin.Analysis.getImsecIMSECHcom.sun.star.sheet.addin.Analysis.getImsechIMSINcom.sun.star.sheet.addin.Analysis.getImsinIMSINHcom.sun.star.sheet.addin.Analysis.getImsinhIMSQRTcom.sun.star.sheet.addin.Analysis.getImsqrtIMSUBcom.sun.star.sheet.addin.Analysis.getImsubIMSUMcom.sun.star.sheet.addin.Analysis.getImsumIMTANcom.sun.star.sheet.addin.Analysis.getImtanINTRATEcom.sun.star.sheet.addin.Analysis.getIntrateISEVENcom.sun.star.sheet.addin.Analysis.getIsevenISODDcom.sun.star.sheet.addin.Analysis.getIsoddLCMcom.sun.star.sheet.addin.Analysis.getLcmMDURATIONcom.sun.star.sheet.addin.Analysis.getMdurationMROUNDcom.sun.star.sheet.addin.Analysis.getMroundMULTINOMIALcom.sun.star.sheet.addin.Analysis.getMultinomialNETWORKDAYScom.sun.star.sheet.addin.Analysis.getNetworkdaysNOMINALcom.sun.star.sheet.addin.Analysis.getNominalOCT2BINcom.sun.star.sheet.addin.Analysis.getOct2BinOCT2DECcom.sun.star.sheet.addin.Analysis.getOct2DecOCT2HEXcom.sun.star.sheet.addin.Analysis.getOct2HexODDFPRICEcom.sun.star.sheet.addin.Analysis.getOddfpriceODDFYIELDcom.sun.star.sheet.addin.Analysis.getOddfyieldODDLPRICEcom.sun.star.sheet.addin.Analysis.getOddlpriceODDLYIELDcom.sun.star.sheet.addin.Analysis.getOddlyieldPRICEcom.sun.star.sheet.addin.Analysis.getPricePRICEDISCcom.sun.star.sheet.addin.Analysis.getPricediscPRICEMATcom.sun.star.sheet.addin.Analysis.getPricematQUOTIENTcom.sun.star.sheet.addin.Analysis.getQuotientRANDBETWEENcom.sun.star.sheet.addin.Analysis.getRandbetweenRECEIVEDcom.sun.star.sheet.addin.Analysis.getReceivedSERIESSUMcom.sun.star.sheet.addin.Analysis.getSeriessumSQRTPIcom.sun.star.sheet.addin.Analysis.getSqrtpiTBILLEQcom.sun.star.sheet.addin.Analysis.getTbilleqTBILLPRICEcom.sun.star.sheet.addin.Analysis.getTbillpriceTBILLYIELDcom.sun.star.sheet.addin.Analysis.getTbillyieldWEEKNUMcom.sun.star.sheet.addin.Analysis.getWeeknumWORKDAYcom.sun.star.sheet.addin.Analysis.getWorkdayXIRRcom.sun.star.sheet.addin.Analysis.getXirrXNPVcom.sun.star.sheet.addin.Analysis.getXnpvYEARFRACcom.sun.star.sheet.addin.Analysis.getYearfracYIELDcom.sun.star.sheet.addin.Analysis.getYieldYIELDDISCcom.sun.star.sheet.addin.Analysis.getYielddiscYIELDMATcom.sun.star.sheet.addin.Analysis.getYieldmat
UNO Service Names for Date Add-In Functions
The table below presents a list of all Calc Date Add-In functions and their respective UNO service names.
Calc Function nameUNO service nameDAYSINMONTHcom.sun.star.sheet.addin.DateFunctions.getDaysInMonthDAYSINYEARcom.sun.star.sheet.addin.DateFunctions.getDaysInMonthMONTHScom.sun.star.sheet.addin.DateFunctions.getDiffMonthsWEEKScom.sun.star.sheet.addin.DateFunctions.getDiffWeeksYEARScom.sun.star.sheet.addin.DateFunctions.getDiffYearsROT13com.sun.star.sheet.addin.DateFunctions.getRot13WEEKSINYEARcom.sun.star.sheet.addin.DateFunctions.getWeeksInYear
UNO Service Names for Pricing Add-In Functions
The table below presents a list of all Calc Pricing Add-In functions and their respective UNO service names.
Calc Function nameUNO service nameOPT_BARRIERcom.sun.star.sheet.addin.PrincingFunctions.getOptBarrierOPT_PROB_HITcom.sun.star.sheet.addin.PrincingFunctions.getOptProbHitOPT_PROB_INMONEYcom.sun.star.sheet.addin.PrincingFunctions.getOptProbInMoneyOPT_TOUCHcom.sun.star.sheet.addin.PrincingFunctions.getOptTouch