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.

Richiamare funzioni interne di Calc in Basic

Usate la funzione CreateUNOService per accedere al servizio com.sun.star.sheet.FunctionAccess.

Esempio:

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. If 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.


Esempio:


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

Richiamare funzioni degli add-in di Calc in BASIC

Le funzioni Add-in di Calc si trovano nel servizio com.sun.star.sheet.addin.Analysis.

Esempio:


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.

Esempio:


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

Add-In Functions UNO service Names

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

Calc Function name

UNO service name

INT.MATURATO.PER

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

INT.MATURATO.SCAD

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

AMMORT.DEGR

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

AMMORT.PER

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

BESSEL.I

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

BESSEL.J

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

BESSEL.K

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

BESSEL.Y

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

BINARIO.DECIMALE

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

BINARIO.HEX

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

BINARIO.OCT

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

COMPLESSO

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

CONVERTI

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

GIORNI.CED.INIZ.LIQ

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

GIORNI.CED

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

GIORNI.CED.NUOVA

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

DATA.CED.SUCC

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

NUM.CEDOLA

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

DATA.CED.PREC

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

INT.CUMUL

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

CAP.CUM

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

DECIMALE.BINARIO

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

DECIMALE.HEX

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

DECIMALE.OCT

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

DELTA

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

TASSO.SCONTO

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

VALUTA.DEC

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

VALUTA.FRAZ

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

DURATA

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

DATA.MESE

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

INT.EFFETTIVO

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

FINE.MESE

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

FUNZ.ERRORE

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

FUNZIONE.ERRORE.COMP

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

FATT.DOPPIO

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

VAL.FUT.CAPITALE

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

MCD

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

SOGLIA

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

HEX.BINARIO

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

HEX.DECIMALE

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

HEX.OCT

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

COMP.MODULO

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

COMP.IMMAGINARIO

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

COMP.ARGOMENTO

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

COMP.CONIUGATO

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

COMP.COS

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

COMP.COSH

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

COMP.COT

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

COMP.COSEC

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

COMP.COSECH

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

COMP.DIV

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

COMP.EXP

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

COMP.LN

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

COMP.LOG10

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

COMP.LOG2

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

COMP.POTENZA

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

COMP.PRODOTTO

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

COMP.PARTE.REALE

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

COMP.SEC

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

COMP.SECH

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

COMP.SEN

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

COMP.SENH

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

COMP.RADQ

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

COMP.DIFF

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

COMP.SOMMA

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

COMP.TAN

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

TASSO.INT

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

VAL.PARI

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

VAL.DISPARI

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

MCM

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

DURATA.M

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

ARROTONDA.MULTIPLO

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

MULTINOMIALE

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

GIORNI.LAVORATIVI.TOT

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

NOMINALE

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

OCT.BINARIO

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

OCT.DECIMALE

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

OCT.HEX

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

PREZZO.PRIMO.IRR

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

REND.PRIMO.IRR

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

PREZZO.ULTIMO.IRR

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

REND.ULTIMO.IRR

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

PREZZO

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

PREZZO.SCONT

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

PREZZO.SCAD

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

QUOZIENTE

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

CASUALE.TRA

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

RICEV.SCAD

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

SOMMA.SERIE

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

RADQ.PI.GRECO

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

BOT.EQUIV

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

BOT.PREZZO

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

BOT.REND

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

NUM.SETTIMANA

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

GIORNO.LAVORATIVO

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

TIR.X

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

VAN.X

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

FRAZIONE.ANNO

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

REND

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

REND.TITOLI.SCONT

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

REND.SCAD

com.sun.star.sheet.addin.Analysis.getYieldmat