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.

Esimerkki:

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 Function
  

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()
        ' 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 result
    End Sub
  

Setting Cell Formulas Containing Internal Calc Functions

Use the formula text string to add a formula to a spreadsheet cell.

note

All Calc functions must be expressed with their English names.


Esimerkki:


Sub AssignFormulaToCell
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)"
REM Cell A1 displays the localized function name
End 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.

Esimerkki:


REM Example calling Add-in function SQRTPI
Function 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.

Esimerkki:


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

UNO service name

KERTYNYT.KORKO

com.sun.star.sheet.addin.Analysis.getAccrint

KERTYNYT.KORKO.LOPUSSA

com.sun.star.sheet.addin.Analysis.getAccrintm

AMORDEGRC

com.sun.star.sheet.addin.Analysis.getAmordegrc

AMORLINC

com.sun.star.sheet.addin.Analysis.getAmorlinc

BESSELI

com.sun.star.sheet.addin.Analysis.getBesseli

BESSELJ

com.sun.star.sheet.addin.Analysis.getBesselj

BESSELK

com.sun.star.sheet.addin.Analysis.getBesselk

BESSELY

com.sun.star.sheet.addin.Analysis.getBessely

BINDES

com.sun.star.sheet.addin.Analysis.getBin2Dec

BINHEKSA

com.sun.star.sheet.addin.Analysis.getBin2Hex

BINOKT

com.sun.star.sheet.addin.Analysis.getBin2Oct

KOMPLEKSI

com.sun.star.sheet.addin.Analysis.getComplex

CONVERT

com.sun.star.sheet.addin.Analysis.getConvert

KORKOPÄIVÄT.ALUSTA

com.sun.star.sheet.addin.Analysis.getCoupdaybs

KORKOPÄIVÄT

com.sun.star.sheet.addin.Analysis.getCoupdays

KORKOPÄIVÄT.SEURAAVA

com.sun.star.sheet.addin.Analysis.getCoupdaysnc

KORKOMAKSU.SEURAAVA

com.sun.star.sheet.addin.Analysis.getCoupncd

KORKOPÄIVÄ.JAKSOT

com.sun.star.sheet.addin.Analysis.getCoupnum

KORKOPÄIVÄ.EDELLINEN

com.sun.star.sheet.addin.Analysis.getCouppcd

MAKSETTU.KORKO

com.sun.star.sheet.addin.Analysis.getCumipmt

MAKSETTU.LYHENNYS

com.sun.star.sheet.addin.Analysis.getCumprinc

DESBIN

com.sun.star.sheet.addin.Analysis.getDec2Bin

DESHEKSA

com.sun.star.sheet.addin.Analysis.getDec2Hex

DESOKT

com.sun.star.sheet.addin.Analysis.getDec2Oct

SAMA.ARVO

com.sun.star.sheet.addin.Analysis.getDelta

DISKONTTOKORKO

com.sun.star.sheet.addin.Analysis.getDisc

VALUUTTA.DES

com.sun.star.sheet.addin.Analysis.getDollarde

VALUUTTA.MURTO

com.sun.star.sheet.addin.Analysis.getDollarfr

KESTO

com.sun.star.sheet.addin.Analysis.getDuration

PÄIVÄ.KUUKAUSI

com.sun.star.sheet.addin.Analysis.getEdate

KORKO.EFEKT

com.sun.star.sheet.addin.Analysis.getEffect

KUUKAUSI.LOPPU

com.sun.star.sheet.addin.Analysis.getEomonth

VIRHEFUNKTIO

com.sun.star.sheet.addin.Analysis.getErf

VIRHEFUNKTIO.KOMPLEMENTTI

com.sun.star.sheet.addin.Analysis.getErfc

KERTOMA.OSA

com.sun.star.sheet.addin.Analysis.getFactdouble

TULEVA.ARVO.ERIKORKO

com.sun.star.sheet.addin.Analysis.getFvschedule

SUURIN.YHT.TEKIJÄ

com.sun.star.sheet.addin.Analysis.getGcd

RAJA

com.sun.star.sheet.addin.Analysis.getGestep

HEKSABIN

com.sun.star.sheet.addin.Analysis.getHex2Bin

HEKSADES

com.sun.star.sheet.addin.Analysis.getHex2Dec

HEKSAOKT

com.sun.star.sheet.addin.Analysis.getHex2Oct

KOMPLEKSI.ABS

com.sun.star.sheet.addin.Analysis.getImabs

KOMPLEKSI.IMAG

com.sun.star.sheet.addin.Analysis.getImaginary

KOMPLEKSI.ARG

com.sun.star.sheet.addin.Analysis.getImargument

KOMPLEKSI.KONJ

com.sun.star.sheet.addin.Analysis.getImconjugate

KOMPLEKSI.COS

com.sun.star.sheet.addin.Analysis.getImcos

KOMPLEKSI.COSH

com.sun.star.sheet.addin.Analysis.getImcosh

KOMPLEKSI.COT

com.sun.star.sheet.addin.Analysis.getImcot

KOMPLEKSI.CSC

com.sun.star.sheet.addin.Analysis.getImcsc

KOMPLEKSI.CSCH

com.sun.star.sheet.addin.Analysis.getImcsch

KOMPLEKSI.OSAM

com.sun.star.sheet.addin.Analysis.getImdiv

KOMPLEKSI.EKSP

com.sun.star.sheet.addin.Analysis.getImexp

KOMPLEKSI.LN

com.sun.star.sheet.addin.Analysis.getImln

KOMPLEKSI.LOG10

com.sun.star.sheet.addin.Analysis.getImlog10

KOMPLEKSI.LOG2

com.sun.star.sheet.addin.Analysis.getImlog2

KOMPLEKSI.POT

com.sun.star.sheet.addin.Analysis.getImpower

KOMPLEKSI.TULO

com.sun.star.sheet.addin.Analysis.getImproduct

KOMPLEKSI.REAALI

com.sun.star.sheet.addin.Analysis.getImreal

KOMPLEKSI.SEC

com.sun.star.sheet.addin.Analysis.getImsec

KOMPLEKSI.SECH

com.sun.star.sheet.addin.Analysis.getImsech

KOMPLEKSI.SIN

com.sun.star.sheet.addin.Analysis.getImsin

KOMPLEKSI.SINH

com.sun.star.sheet.addin.Analysis.getImsinh

KOMPLEKSI.NELIÖJ

com.sun.star.sheet.addin.Analysis.getImsqrt

KOMPLEKSI.EROTUS

com.sun.star.sheet.addin.Analysis.getImsub

KOMPLEKSI.SUM

com.sun.star.sheet.addin.Analysis.getImsum

KOMPLEKSI.TAN

com.sun.star.sheet.addin.Analysis.getImtan

KORKO.ARVOPAPERI

com.sun.star.sheet.addin.Analysis.getIntrate

ONPARILLINEN

com.sun.star.sheet.addin.Analysis.getIseven

ONPARITON

com.sun.star.sheet.addin.Analysis.getIsodd

PIENIN.YHT.JAETTAVA

com.sun.star.sheet.addin.Analysis.getLcm

KESTO.MUUNN

com.sun.star.sheet.addin.Analysis.getMduration

PYÖRISTÄ.KERR

com.sun.star.sheet.addin.Analysis.getMround

MULTINOMI

com.sun.star.sheet.addin.Analysis.getMultinomial

TYÖPÄIVÄT

com.sun.star.sheet.addin.Analysis.getNetworkdays

KORKO.VUOSI

com.sun.star.sheet.addin.Analysis.getNominal

OKTBIN

com.sun.star.sheet.addin.Analysis.getOct2Bin

OKTDES

com.sun.star.sheet.addin.Analysis.getOct2Dec

OKTHEKSA

com.sun.star.sheet.addin.Analysis.getOct2Hex

PARITON.ENS.NIMELLISARVO

com.sun.star.sheet.addin.Analysis.getOddfprice

PARITON.ENS.TUOTTO

com.sun.star.sheet.addin.Analysis.getOddfyield

PARITON.VIIM.NIMELLISARVO

com.sun.star.sheet.addin.Analysis.getOddlprice

PARITON.VIIM.TUOTTO

com.sun.star.sheet.addin.Analysis.getOddlyield

HINTA

com.sun.star.sheet.addin.Analysis.getPrice

HINTA.DISK

com.sun.star.sheet.addin.Analysis.getPricedisc

HINTA.LUNASTUS

com.sun.star.sheet.addin.Analysis.getPricemat

OSAMÄÄRÄ

com.sun.star.sheet.addin.Analysis.getQuotient

SATUNNAISLUKU.VÄLILTÄ

com.sun.star.sheet.addin.Analysis.getRandbetween

SAATU.HINTA

com.sun.star.sheet.addin.Analysis.getReceived

SARJA.SUMMA

com.sun.star.sheet.addin.Analysis.getSeriessum

NELIÖJUURI.PII

com.sun.star.sheet.addin.Analysis.getSqrtpi

OBLIG.TUOTTOPROS

com.sun.star.sheet.addin.Analysis.getTbilleq

OBLIG.HINTA

com.sun.star.sheet.addin.Analysis.getTbillprice

OBLIG.TUOTTO

com.sun.star.sheet.addin.Analysis.getTbillyield

VIIKKO.NRO

com.sun.star.sheet.addin.Analysis.getWeeknum

TYÖPÄIVÄ

com.sun.star.sheet.addin.Analysis.getWorkday

SISÄINEN.KORKO.JAKSOTON

com.sun.star.sheet.addin.Analysis.getXirr

NNA.JAKSOTON

com.sun.star.sheet.addin.Analysis.getXnpv

VUOSI.OSA

com.sun.star.sheet.addin.Analysis.getYearfrac

TUOTTO

com.sun.star.sheet.addin.Analysis.getYield

TUOTTO.DISK

com.sun.star.sheet.addin.Analysis.getYielddisc

TUOTTO.ERÄP

com.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 name

UNO service name

PÄIVIÄ.KUUKAUDESSA

com.sun.star.sheet.addin.DateFunctions.getDaysInMonth

PÄIVIÄ.VUODESSA

com.sun.star.sheet.addin.DateFunctions.getDaysInMonth

KUUKAUDET

com.sun.star.sheet.addin.DateFunctions.getDiffMonths

VIIKOT

com.sun.star.sheet.addin.DateFunctions.getDiffWeeks

VUODET

com.sun.star.sheet.addin.DateFunctions.getDiffYears

ROT13

com.sun.star.sheet.addin.DateFunctions.getRot13

VIIKKOJA.VUODESSA

com.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 name

UNO service name

OPT_RAJAHINTA

com.sun.star.sheet.addin.PrincingFunctions.getOptBarrier

OPT_TNÄK_SAAVUTTAA

com.sun.star.sheet.addin.PrincingFunctions.getOptProbHit

OPT_TNÄK_PLUS

com.sun.star.sheet.addin.PrincingFunctions.getOptProbInMoney

OPT_TOUCH

com.sun.star.sheet.addin.PrincingFunctions.getOptTouch


Please support us!