DeHMt‖Using Calc Functions in Macros

Contents
NAeNm‖

# wHDmE‖Using Calc Functions in Macros

vixak‖In addition to the native BASIC functions, you can call Calc functions in your macros and scripts and set Calc functions in cell formulas.

## pFoqw‖Calling Internal Calc functions in Basic

2Cm3R‖Use the CreateUnoService function to access the com.sun.star.sheet.FunctionAccess service.

### EFSA4‖Example:

LFEou‖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")
        sWUzz‖    ' Always use the function name in English
            MyVlook = oService.callFunction("VLOOKUP", Array(Lookup, DataArray, Index, SortedRangeLookup))
        End Function

TDuCT‖The 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()
        GGHzu‖    ' Creates a 5 by 2 array and fills it with data
            Dim myData(1 to 5, 1 to 2) as Variant
        dAjYS‖    myData(1, 1) = 1 : myData(1, 2) = "Strongly disagree"
        sv9rX‖    myData(2, 1) = 3 : myData(2, 2) = "Disagree"
        M4sWw‖    myData(3, 1) = 5 : myData(3, 2) = "Undecided"
        qViS9‖    myData(4, 1) = 7 : myData(4, 2) = "Agree"
        mPMYX‖    myData(5, 1) = 9 : myData(5, 2) = "Strongly agree"
        kdMgo‖    ' Looks up the data array
            Dim result as String
            result = MyVlook(4, myData, 2, 1)
        UJGMe‖    ' Shows the message "Disagree"
            MsgBox result
        End Sub

## u8bVb‖Setting Cell Formulas Containing Internal Calc Functions

3dqfQ‖Use the formula text string to add a formula to a spreadsheet cell.

o3jiB‖All Calc functions must be expressed with their English names.

### EFSA4‖Example:

    Sub AssignFormulaToCell
    dfwfw‖REM Add a formula to cell A1. Function name must be in English.
        oCell = ThisComponent.Sheets.getByIndex(0).getCellRangeByName("A1")
        oCell.Formula = "=SUM(B1:B10)"
    BcaTk‖REM Cell A1 displays the localized function name
    End Sub

## V3quU‖Calling Add-In Calc Functions in BASIC

xtVYE‖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.

### EFSA4‖Example:

    94wim‖REM Example calling Add-in function SQRTPI
    bFnDA‖Function MySQRTPI(arg as double) as double
       Dim oService as Object
       oService = CreateUnoService("com.sun.star.sheet.addin.Analysis")
    tFYCy‖   MySQRTPI = oService.getSqrtPi(arg)
    End Function

## 4Rj5v‖Setting Cell Formulas with Add-In Functions

wPiTB‖The Add-In function must be expressed by its UNO service name.

### EFSA4‖Example:

    Sub AssignAddInFormulaToCell
    nUVAe‖REM 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)"
    Q6BTa‖REM Cell A1 displays the localized function name
    End Sub

## gtxdC‖UNO Service Names for Analysis Add-In Functions

SBEpf‖The table below presents a list of all Calc Analysis Add-In functions and their respective UNO service names.

2NxGD‖Calc Function name | b2FSD‖UNO service name
--- | ---
emGWD‖ACCRINT | com.sun.star.sheet.addin.Analysis.getAccrint
oKBuD‖ACCRINTM | com.sun.star.sheet.addin.Analysis.getAccrintm
pBfUh‖AMORDEGRC | com.sun.star.sheet.addin.Analysis.getAmordegrc
ViiCh‖AMORLINC | com.sun.star.sheet.addin.Analysis.getAmorlinc
ZeeMB‖BESSELI | com.sun.star.sheet.addin.Analysis.getBesseli
Bv4xD‖BESSELJ | com.sun.star.sheet.addin.Analysis.getBesselj
Ana8S‖BESSELK | com.sun.star.sheet.addin.Analysis.getBesselk
gPmYm‖BESSELY | com.sun.star.sheet.addin.Analysis.getBessely
Rhr8N‖BIN2DEC | com.sun.star.sheet.addin.Analysis.getBin2Dec
CF6He‖BIN2HEX | com.sun.star.sheet.addin.Analysis.getBin2Hex
XJGN7‖BIN2OCT | com.sun.star.sheet.addin.Analysis.getBin2Oct
trpYC‖COMPLEX | com.sun.star.sheet.addin.Analysis.getComplex
eHuPF‖CONVERT | com.sun.star.sheet.addin.Analysis.getConvert
yF4wh‖COUPDAYBS | com.sun.star.sheet.addin.Analysis.getCoupdaybs
SArWm‖COUPDAYS | com.sun.star.sheet.addin.Analysis.getCoupdays
CkhYX‖COUPDAYSNC | com.sun.star.sheet.addin.Analysis.getCoupdaysnc
A4oUG‖COUPNCD | com.sun.star.sheet.addin.Analysis.getCoupncd
8Eyr4‖COUPNUM | com.sun.star.sheet.addin.Analysis.getCoupnum
ur3HX‖COUPPCD | com.sun.star.sheet.addin.Analysis.getCouppcd
HYaqY‖CUMIPMT | com.sun.star.sheet.addin.Analysis.getCumipmt
ccwAv‖CUMPRINC | com.sun.star.sheet.addin.Analysis.getCumprinc
CWYx5‖DEC2BIN | com.sun.star.sheet.addin.Analysis.getDec2Bin
LZGCA‖DEC2HEX | com.sun.star.sheet.addin.Analysis.getDec2Hex
FABUZ‖DEC2OCT | com.sun.star.sheet.addin.Analysis.getDec2Oct
Y2HuZ‖DELTA | com.sun.star.sheet.addin.Analysis.getDelta
XMBJo‖DISC | com.sun.star.sheet.addin.Analysis.getDisc
iTNtK‖DOLLARDE | com.sun.star.sheet.addin.Analysis.getDollarde
iWfvp‖DOLLARFR | com.sun.star.sheet.addin.Analysis.getDollarfr
iq2zc‖DURATION | com.sun.star.sheet.addin.Analysis.getDuration
yzAGQ‖EDATE | com.sun.star.sheet.addin.Analysis.getEdate
ePmwB‖EFFECT | com.sun.star.sheet.addin.Analysis.getEffect
whDH8‖EOMONTH | com.sun.star.sheet.addin.Analysis.getEomonth
T8yoU‖ERF | com.sun.star.sheet.addin.Analysis.getErf
YFhBd‖ERFC | com.sun.star.sheet.addin.Analysis.getErfc
dKas5‖FACTDOUBLE | com.sun.star.sheet.addin.Analysis.getFactdouble
87Htt‖FVSCHEDULE | com.sun.star.sheet.addin.Analysis.getFvschedule
jjTyE‖GCD | com.sun.star.sheet.addin.Analysis.getGcd
mcjKe‖GESTEP | com.sun.star.sheet.addin.Analysis.getGestep
Toc2i‖HEX2BIN | com.sun.star.sheet.addin.Analysis.getHex2Bin
CEWxE‖HEX2DEC | com.sun.star.sheet.addin.Analysis.getHex2Dec
RrpLx‖HEX2OCT | com.sun.star.sheet.addin.Analysis.getHex2Oct
6GUuy‖IMABS | com.sun.star.sheet.addin.Analysis.getImabs
mUowh‖IMAGINARY | com.sun.star.sheet.addin.Analysis.getImaginary
8GGrD‖IMARGUMENT | com.sun.star.sheet.addin.Analysis.getImargument
AaMnu‖IMCONJUGATE | com.sun.star.sheet.addin.Analysis.getImconjugate
4vbCQ‖IMCOS | com.sun.star.sheet.addin.Analysis.getImcos
dr8EH‖IMCOSH | com.sun.star.sheet.addin.Analysis.getImcosh
paAHR‖IMCOT | com.sun.star.sheet.addin.Analysis.getImcot
twyK6‖IMCSC | com.sun.star.sheet.addin.Analysis.getImcsc
jKEFp‖IMCSCH | com.sun.star.sheet.addin.Analysis.getImcsch
Lav7E‖IMDIV | com.sun.star.sheet.addin.Analysis.getImdiv
Q3Zxj‖IMEXP | com.sun.star.sheet.addin.Analysis.getImexp
7DUrG‖IMLN | com.sun.star.sheet.addin.Analysis.getImln
pVmGz‖IMLOG10 | com.sun.star.sheet.addin.Analysis.getImlog10
gTxpZ‖IMLOG2 | com.sun.star.sheet.addin.Analysis.getImlog2
Windb‖IMPOWER | com.sun.star.sheet.addin.Analysis.getImpower
4RLjP‖IMPRODUCT | com.sun.star.sheet.addin.Analysis.getImproduct
mogsD‖IMREAL | com.sun.star.sheet.addin.Analysis.getImreal
kaDjL‖IMSEC | com.sun.star.sheet.addin.Analysis.getImsec
GAsAW‖IMSECH | com.sun.star.sheet.addin.Analysis.getImsech
8Dtdh‖IMSIN | com.sun.star.sheet.addin.Analysis.getImsin
LnpEu‖IMSINH | com.sun.star.sheet.addin.Analysis.getImsinh
HoKey‖IMSQRT | com.sun.star.sheet.addin.Analysis.getImsqrt
EDQwc‖IMSUB | com.sun.star.sheet.addin.Analysis.getImsub
Qfop5‖IMSUM | com.sun.star.sheet.addin.Analysis.getImsum
DaiAF‖IMTAN | com.sun.star.sheet.addin.Analysis.getImtan
AWW2a‖INTRATE | com.sun.star.sheet.addin.Analysis.getIntrate
BNWvt‖ISEVEN | com.sun.star.sheet.addin.Analysis.getIseven
5Avoj‖ISODD | com.sun.star.sheet.addin.Analysis.getIsodd
fwZ3Y‖LCM | com.sun.star.sheet.addin.Analysis.getLcm
WDW3o‖MDURATION | com.sun.star.sheet.addin.Analysis.getMduration
CaC2W‖MROUND | com.sun.star.sheet.addin.Analysis.getMround
ipDJm‖MULTINOMIAL | com.sun.star.sheet.addin.Analysis.getMultinomial
wDMMt‖NETWORKDAYS | com.sun.star.sheet.addin.Analysis.getNetworkdays
azBMs‖NOMINAL | com.sun.star.sheet.addin.Analysis.getNominal
FGnXt‖OCT2BIN | com.sun.star.sheet.addin.Analysis.getOct2Bin
npjsC‖OCT2DEC | com.sun.star.sheet.addin.Analysis.getOct2Dec
Bd3s5‖OCT2HEX | com.sun.star.sheet.addin.Analysis.getOct2Hex
iBzAQ‖ODDFPRICE | com.sun.star.sheet.addin.Analysis.getOddfprice
ParaD‖ODDFYIELD | com.sun.star.sheet.addin.Analysis.getOddfyield
9CxRc‖ODDLPRICE | com.sun.star.sheet.addin.Analysis.getOddlprice
jBECN‖ODDLYIELD | com.sun.star.sheet.addin.Analysis.getOddlyield
opEEG‖PRICE | com.sun.star.sheet.addin.Analysis.getPrice
kXJnU‖PRICEDISC | com.sun.star.sheet.addin.Analysis.getPricedisc
24W75‖PRICEMAT | com.sun.star.sheet.addin.Analysis.getPricemat
rvmjF‖QUOTIENT | com.sun.star.sheet.addin.Analysis.getQuotient
AC3Lz‖RANDBETWEEN | com.sun.star.sheet.addin.Analysis.getRandbetween
ZCZFS‖RECEIVED | com.sun.star.sheet.addin.Analysis.getReceived
nfAzV‖SERIESSUM | com.sun.star.sheet.addin.Analysis.getSeriessum
uC2XH‖SQRTPI | com.sun.star.sheet.addin.Analysis.getSqrtpi
t6kbr‖TBILLEQ | com.sun.star.sheet.addin.Analysis.getTbilleq
BMCzh‖TBILLPRICE | com.sun.star.sheet.addin.Analysis.getTbillprice
KFSbi‖TBILLYIELD | com.sun.star.sheet.addin.Analysis.getTbillyield
Ce7D3‖WEEKNUM | com.sun.star.sheet.addin.Analysis.getWeeknum
WKvrN‖WORKDAY | com.sun.star.sheet.addin.Analysis.getWorkday
jJDdS‖XIRR | com.sun.star.sheet.addin.Analysis.getXirr
JEeBG‖XNPV | com.sun.star.sheet.addin.Analysis.getXnpv
JkpJC‖YEARFRAC | com.sun.star.sheet.addin.Analysis.getYearfrac
SaiBh‖YIELD | com.sun.star.sheet.addin.Analysis.getYield
qFVKa‖YIELDDISC | com.sun.star.sheet.addin.Analysis.getYielddisc
HY4dN‖YIELDMAT | com.sun.star.sheet.addin.Analysis.getYieldmat

## 9EfRH‖UNO Service Names for Date Add-In Functions

QMryg‖The table below presents a list of all Calc Date Add-In functions and their respective UNO service names.

dcQK3‖Calc Function name | TEzJG‖UNO service name
--- | ---
J6Jdh‖DAYSINMONTH | com.sun.star.sheet.addin.DateFunctions.getDaysInMonth
yW5C5‖DAYSINYEAR | com.sun.star.sheet.addin.DateFunctions.getDaysInMonth
vXhZ9‖MONTHS | com.sun.star.sheet.addin.DateFunctions.getDiffMonths
gWTY8‖WEEKS | com.sun.star.sheet.addin.DateFunctions.getDiffWeeks
DjCEK‖YEARS | com.sun.star.sheet.addin.DateFunctions.getDiffYears
bNKK9‖ROT13 | com.sun.star.sheet.addin.DateFunctions.getRot13
A7nYJ‖WEEKSINYEAR | com.sun.star.sheet.addin.DateFunctions.getWeeksInYear

## qqkuj‖UNO Service Names for Pricing Add-In Functions

ErZSq‖The table below presents a list of all Calc Pricing Add-In functions and their respective UNO service names.

mwzeA‖Calc Function name | TRZn5‖UNO service name
--- | ---
rQJPp‖OPT_BARRIER | com.sun.star.sheet.addin.PrincingFunctions.getOptBarrier
zF653‖OPT_PROB_HIT | com.sun.star.sheet.addin.PrincingFunctions.getOptProbHit
2xAys‖OPT_PROB_INMONEY | com.sun.star.sheet.addin.PrincingFunctions.getOptProbInMoney
WKVoC‖OPT_TOUCH | com.sun.star.sheet.addin.PrincingFunctions.getOptTouch

Related Topics

CreateUnoService Function

Please support us!