Zizi64 wrote:I have not idea of the reason why it is not working on my Spreadsheet.
Please give us some details about "how you use the created function?". (maybe you passed too large numbers, negative numbers, strings, floats to the custom function...)
I use it in the function ach2ahx() which is used on my sheet on a string (e.g. "AT#SCFG=1,1,300,0,600,50").
The function has to create a new string where each char of the string is converted in Hex.
For instance "AT#SCFG=1,1,300,0,600,50" becomes "415423534346473D312C312C3330302C302C3630302C3530
"
If I replace fnDec2Hex with my custom function "Dec2HexC", everything is fine.
Code: Select all
Function ach2ahx(instr As Variant) As String
Dim i%, s$, val1, val2%, val3$
s$ = ""
For i% = 1 To LEN(instr)
val1 = Mid(instr, i%, 1)
val2 = calc_CODE(val1)
val3 = fnDec2Hex(val2)
s$ = s$ & val3
Next
achar2ahex = s$
End Function
Function fnDec2Hex(nDec AS Integer) AS String
Dim oFunctionAccess : oFunctionAccess = createUnoService( "com.sun.star.sheet.FunctionAccess" )
arg = Array(nDec)
fnDec2Hex = oFunctionAccess.CallFunction( "DEC2HEX", arg)
End Function
Function Dec2HexC(nDec AS Integer) AS String
Dim ccM$, ccL$, vM%, vL%, valin%
Dim Hx As Variant
Hx = Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","X")
vM = 0
valin = nDec
Do While valin >= 16
vM = vM + 1
valin = valin - 16
Loop
vL = nDec - (vM * 16)
ccM = Hx(vM)
ccL = Hx(vL)
Dec2HexC = ccM & ccL
End Function
Function calc_CODE(vRange)
' The first argument to callFunction() is the Calc Function Name
' The 2nd argument is the parameters of that function as an array
FuncAcc = createunoservice("com.sun.star.sheet.FunctionAccess")
calc_CODE = FuncAcc.callFunction("CODE", array(vRange))
End Function