Are unsigned long types available to LibreOffice Basic? - libreoffice

I'm want to write a simple 32-bit FNV hash function for LibreOffice Calc. However, LibreOffice Basic only supports signed long data types, so you will get an "Inadmissible value or data type. Overflow." error on line 7 with the following code:
Function Hash(strText as String) as Long
Dim h As Long
Dim nextChar As String
Dim temp As Long
h = 2166136261
For i = 1 To Len(strText)
nextChar = Mid(strText, i, 1)
temp = Asc(nextChar)
h = h XOR temp
h = h * 16777619
Next
Hash = h
End Function
Because the h variable is assigned 2166136261 in the code above, it is obviously out of bounds. Is it possible to work with unsigned long (0 to 4294967295) data types in LibreOffice Basic? If so, how?

You could do this:
Sub CallHash
oMasterScriptProviderFactory = createUnoService(_
"com.sun.star.script.provider.MasterScriptProviderFactory")
oScriptProvider = oMasterScriptProviderFactory.createScriptProvider("")
oScript = oScriptProvider.getScript(_
"vnd.sun.star.script:foo.py$hash?language=Python&location=user")
hashString = oScript.invoke(Array("bar"), Array(), Array())
MsgBox hashString
End Sub
foo.py:
def hash(strText):
h = 2166136261
for nextChar in strText:
temp = ord(nextChar)
h = h ^ temp
h = h * 16777619
return str(h)
Or drop Basic and use only Python-UNO.
There are unsigned long values in the UNO API. However, I didn't find any API methods to perform calculations on this object.
Dim o As Object
o = CreateUnoValue("unsigned long", 2166136261)

Related

How can I make a code that tells VB6 that tells VB6 that you're inputting something other than Whole numbers?

Introduction: Hi, everyone! I'm new to VB6! How can I make a code that tells VB6 that tells VB6 that you're inputting something other than Whole numbers?
Details: I'm making an arithmetic progression calculator (I think the code is not needed? but I'll just provide just in case.) Here is my code:
Option Explicit
Private Sub btCalc_Click()
Dim A As Long
Dim N As Long
Dim D As Long
Dim R As Long
Dim F As Long
A = Val(txtInitterm.Text)
N = Val(txtTermint.Text)
D = Val(txtFinterm.Text)
R = Val(txtTermint.Text)
F = N / 2 * (2 * A + (N - 1) * D)
lblOutput.Caption = F
End Sub
and I wanna notify or tell VB6 that I'm putting in a fraction, not an integer and uses that fraction to do operations.
NOTE: String Fraction to Value in VBA this doesn't answer my question... :D
Thank you for helping me, everyone! it's much appreciated.
There is no Application.Evaluate(...) in Vb6 like in VBA, so you have to do it like the "question" in "String Fraction to Value in VBA". Extract the logic to a function for re-use, and replace the Val(...) calls with the function for use.
Something like below would likely work, although you may want to provide better error handling in the obvious bad-math cases. I simply return zero and mark them with a comment.
Option Explicit
Private Sub btCalc_Click()
Dim A As Long, N As Long, D As Long, R As Long, F As Long
A = GetFrac(txtInitterm)
N = GetFrac(txtTermint)
D = GetFrac(txtFinterm)
R = GetFrac(txtTermint)
F = N / 2 * (2 * A + (N - 1) * D)
lblOutput.Caption = F
End Sub
Public Function GetFrac(ByVal S As String) As Double
GetFrac = 0 ' default return on error
If InStr(S, "/") = 0 Then GetFrac = Val(S): Exit Function
Dim P() As String, N As Double, D As Double
P = Split(S, "/")
If UBound(P) <> 1 Then Exit Function ' bad input -- multiple /'s
N = Val(P(0))
D = Val(P(1))
If D = 0 Then Exit Function ' div by 0
GetFrac = N / D
End Function

matlab oop "use data of hole class" for calculation

First, sorry for the bad title - I'm new to OO programming - basically I'd like to understand how Matlab works with oop classes.
Before I ask my question, here is a basic example of what I want to do:
I have two houses and some data about them and I got the idea, to work with oop classes. Here is my .m file.
classdef building
properties
hohe = 0;
lange = 0;
breite = 0;
xabstandsolar = 0;
yabstandsolar = 0;
end
methods
function obj = building(hohe, lange, breite, xabstandsolar, yabstandsolar)
obj.hohe = hohe;
obj.lange = lange;
obj.breite = breite;
obj.xabstandsolar = xabstandsolar;
obj.yabstandsolar = yabstandsolar;
end
function hohenwinkel(h)
h = h
d = sqrt(obj.xabstandsolar^2 + yabstandsolar^2);
gamma_v = atand((obj.hohe - h)/(d));
end
end
end
I filled it with some data - for example
>>H1 = building(10,8,6,14,8)
>>H2 = building(18,8,6,14,0)
And now I want to calculate "gamma_v" (as an 1x2 array) for each building. Any ideas, how I can archive this?
Edit:
I want to create gamma_v (as an array) automatically for all objects in the class "building". There will be a lot more "houses" in the final script.
Your hohenwinkel method needs to accept two input arguments. The first argument for non-static methods is always the object itself (unlike C++, you'll have to explicitly list it as an input argument) and the second input will be your h variable. You'll also want to actually return the gamma_v value using an output argument for your method.
function gamma_v = hohenwinkel(obj, h)
d = sqrt(obj.xabstandsolar^2 + obj.yabstandsolar^2);
gamma_v = atand((obj.hohe - h)/(d));
end
Then you can invoke this method on each building to get the value
gamma_v1 = hohenwinkel(H1);
gamma_v2 = hohenwinkel(H2);
If you want to have an array of buildings, you can create that array
houses = [building(10,8,6,14,8), building(18,8,6,14,0)];
gamma_v = hohenwinkel(houses);
and then construct your hohenwinkel function to operate on each one and return the result
function gamma_v = hohenwinkel(obj, h)
if numel(obj) > 1
% Compute hohenwinkel for each one
gamma_v = arrayfun(#(x)hohenwinkel(x, h), obj);
return
end
d = sqrt(obj.xabstandsolar^2 + obj.yabstandsolar^2);
gamma_v = atand((obj.hohe - h)/(d));
end
there is some tricky solution (and its not recommended)(#Suever solution is better)
you should create a handle class
classdef gvclass<handle
properties
gvarr=[];
end
methods
function setgvarr(obj,value)
obj.gvarr=[obj.gvarr,value];
end
end
end
then use this class in your building class
classdef building<handle
properties
gv
hohe = 0;
lange = 0;
breite = 0;
xabstandsolar = 0;
yabstandsolar = 0;
end
methods
function obj = building(hohe, lange, breite, xabstandsolar, yabstandsolar,handle_of_your_gv_class,h)
obj.hohe = hohe;
obj.lange = lange;
obj.breite = breite;
obj.xabstandsolar = xabstandsolar;
obj.yabstandsolar = yabstandsolar;
obj.gv=handle_of_your_gv_class;
obj.hohenwinkel(h);
end
function hohenwinkel(obj,h)
d = sqrt(obj.xabstandsolar^2 + yabstandsolar^2);
gamma_v = atand((obj.hohe - h)/(d));
obj.gv.setgvarr(gamma_v);
end
end
end
finally before creating any building you should create an object of gv class and pass it to the building constructor,
Egv=gvclass();
H1 = building(10,8,6,14,8,Egv,2)
H2 = building(18,8,6,14,0,Egv,3)
and to access gv array:
Egv.gvarr
you should do more effort on this issue to debug possible errors.

simplest Unostructure that supports he getByName

In LibreOffice Basic sub I use a bunch of uno properties in an array. Which is the simplest Unostructure or UnoService that I must "embed" them, in order to use the getByName "function"?
Example:
dim props(1) as new com.sun.star.beans.PropertyValue
props(0).Name = "blahblah1"
props(0).Value = "blahblah1Value"
props(1).Name = "blahblah2"
props(1).Name = 3000
I want to be able to use something like:
b = props.getByName("blahblah2").Value
or something like (assuming I "assigned" them in a structure-like-object called "somestruct") :
b = somestruct.getprops.getByName("blahblah2").Value
As I understand that this can be done by creating a "UnoService" which supports the getByName and then, somehow, assigning these props to this service
Which is the "lightest" such service?
(I mean the service that uses less resources)
Thanks in advance.
Really supporting the interface XNameAccess is not as easy. The services which implement this interface are supposed using this interface for existing named properties, not for own created ones.
But you can use the service EnumerableMap to achieve what you probably want.
Example:
sub testEnumerableMap
serviceEnumerableMap = com.sun.star.container.EnumerableMap
oEnumerableMap = serviceEnumerableMap.create("string", "any")
oEnumerableMap.put("blahblah1", "blahblah1Value")
oEnumerableMap.put("blahblah2", 3000)
oEnumerableMap.put("blahblah3", 1234.67)
msgbox oEnumerableMap.get("blahblah1")
msgbox oEnumerableMap.get("blahblah2")
msgbox oEnumerableMap.get("blahblah3")
'msgbox oEnumerableMap.get("blahblah4") 'will throw error
msgbox oEnumerableMap.containsKey("blahblah2")
msgbox oEnumerableMap.containsValue(3000)
if oEnumerableMap.containsKey("blahblah4") then
msgbox oEnumerableMap.get("blahblah4")
end if
end sub
But starbasic with option Compatible is also able supporting Class programming like VBA does.
Example:
Create a module named myPropertySet. Therein put the following code:
option Compatible
option ClassModule
private aPropertyValues() as com.sun.star.beans.PropertyValue
public sub setProperty(oProp as com.sun.star.beans.PropertyValue)
bUpdated = false
for each oPropPresent in aPropertyValues
if oPropPresent.Name = oProp.Name then
oPropPresent.Value = oProp.Value
bUpdated = true
exit for
end if
next
if not bUpdated then
iIndex = ubound(aPropertyValues) + 1
redim preserve aPropertyValues(iIndex)
aPropertyValues(iIndex) = oProp
end if
end sub
public function getPropertyValue(sName as string) as variant
getPropertyValue = "N/A"
for each oProp in aPropertyValues
if oProp.Name = sName then
getPropertyValue = oProp.Value
exit for
end if
next
end function
Then within a standard module:
sub testClass
oPropertySet = new myPropertySet
dim prop as new com.sun.star.beans.PropertyValue
prop.Name = "blahblah1"
prop.Value = "blahblah1Value"
oPropertySet.setProperty(prop)
prop.Name = "blahblah2"
prop.Value = 3000
oPropertySet.setProperty(prop)
prop.Name = "blahblah3"
prop.Value = 1234.56
oPropertySet.setProperty(prop)
prop.Name = "blahblah2"
prop.Value = 8888
oPropertySet.setProperty(prop)
msgbox oPropertySet.getPropertyValue("blahblah1")
msgbox oPropertySet.getPropertyValue("blahblah2")
msgbox oPropertySet.getPropertyValue("blahblah3")
msgbox oPropertySet.getPropertyValue("blahblah4")
end sub
LibreOffice Basic supports the vb6 Collection type.
Dim coll As New Collection
coll.Add("blahblah1Value", "blahblah1")
coll.Add(3000, "blahblah2")
MsgBox(coll("blahblah1"))
Arrays of property values are the only thing that will work for certain UNO interfaces such as dispatcher calls. If you simply need a better way to deal with arrays of property values, then use a helper function.
Sub DisplayMyPropertyValue
Dim props(0 To 1) As New com.sun.star.beans.PropertyValue
props(0).Name = "blahblah1"
props(0).Value = "blahblah1Value"
props(1).Name = "blahblah2"
props(1).Name = 3000
MsgBox(GetPropertyByName(props, "blahblah1"))
End Sub
Function GetPropertyByName(props As Array, propname As String)
For Each prop In props
If prop.Name = propname Then
GetPropertyByName = prop.Value
Exit Function
End If
Next
GetPropertyByName = ""
End Function
XNameAccess is used for UNO containers such as Calc sheets. Normally these containers are obtained from the UNO interface, not created.
oSheet = ThisComponent.Sheets.getByName("Sheet1")
May UNO objects support the XPropertySet interface. Normally these are also obtained from the UNO interface, not created.
paraStyleName = cellcursor.getPropertyValue("ParaStyleName")
It may be possible to create a new class in Java that implements XPropertySet. However, Basic uses helper functions instead of class methods.
I think the serviceEnumerableMap is the answer (so far). Creating the values and searching them was much faster then creating props in a dynamic array and searching them with a for loop in basic.
(I do not "dare" to use "option Compatible", although I was a big fun of VB6 and VBA, because of the problems in code that maybe arise).
I used this code to test time in a form:
SUB testlala(Event)
TESTPROPS(Event)
' TESTENUM(Event)
MSGBOX "END OF TEST"
END SUB
SUB TESTENUM(Event)
DIM xcounter AS LONG
'b = now()
serviceEnumerableMap = com.sun.star.container.EnumerableMap
oEnumerableMap = serviceEnumerableMap.create("string", "any")
FOR xcounter= 0 TO 10000
oEnumerableMap.put("pr" & FORMAT(xcounter,"0000"), xcounter -10000)
NEXT
'b=now()-b
b = now()
FOR xcounter = 1 TO 5000
lala = Int((9000 * Rnd) +1)
g =oEnumerableMap.get("pr" & FORMAT(lala,"0000"))
'MSGBOX GetValueFromName(props,"pr" & FORMAT(xcounter,"0000"))
NEXT
b=now()-b
MSGBOX b*100000
END SUB
SUB TESTPROPS(Event)
DIM props()
DIM xcounter AS LONG
'b = now()
FOR xcounter= 0 TO 10000
AppendProperty(props,"pr" & FORMAT(xcounter,"0000"), xcounter -10000)
NEXT
'b=now()-b
b = now()
FOR xcounter = 1 TO 5000
lala = Int((9000 * Rnd) +1)
g = GetValueFromName(props,"pr" & FORMAT(lala,"0000"))
'MSGBOX GetValueFromName(props,"pr" & FORMAT(xcounter,"0000"))
NEXT
b=now()-b
MSGBOX b*100000
END SUB
REM FROM Andrew Pitonyak's OpenOffice Macro Information ------------------
Sub AppendToArray(oData(), ByVal x)
Dim iUB As Integer 'The upper bound of the array.
Dim iLB As Integer 'The lower bound of the array.
iUB = UBound(oData()) + 1
iLB = LBound(oData())
ReDim Preserve oData(iLB To iUB)
oData(iUB) = x
End Sub
Function CreateProperty(sName$, oValue) As com.sun.star.beans.PropertyValue
Dim oProperty As New com.sun.star.beans.PropertyValue
oProperty.Name = sName
oProperty.Value = oValue
CreateProperty() = oProperty
End Function
Sub AppendProperty(oProperties(), sName As String, ByVal oValue)
AppendToArray(oProperties(), CreateProperty(sName, oValue))
End Sub

Excel Range object throws error 0x800A03EC because of a string longer than 255 characters

Using an ActiveX server from MATLAB, I am trying to highlight many cells in an Excel sheet at once. These are not in specific columns or rows so I use Range('A1,B2,...') to access them. However the string accepted by the Range object has to be less than 255 characters or an error:
Error: Object returned error code: 0x800A03EC
is thrown. The following code reproduces this error with an empty Excel file.
hActX = actxserver('Excel.Application');
hWB = hActX.Workbooks.Open('C:\Book1.xlsx');
hSheet = hWB.Worksheets.Item('Sheet1');
col = repmat('A', 100, 1);
row = num2str((1:100)'); %'
cellInd = strcat(col, strtrim(cellstr(row)));
str1 = strjoin(cellInd(1:66), ','); %// 254 characters
str2 = strjoin(cellInd(1:67), ','); %// 258 characters
hSheet.Range(str1).Interior.Color = 255; %// Works
hSheet.Range(str2).Interior.Color = 255; %// Error 0x800A03EC
hWB.Save;
hWB.Close(false);
hActX.Quit;
How can I get around this? I found no other relevant method of calling Range, or of otherwise getting the cells I want to modify.
If you start with a String, you can test its length to determine if Range() can handle it. Here is an example of building a diagonal range:
Sub DiagonalRange()
Dim BigString As String, BigRange As Range
Dim i As Long, HowMany As Long, Ln As String
HowMany = 100
For i = 1 To HowMany
BigString = BigString & "," & Cells(i, i).Address(0, 0)
Next i
BigString = Mid(BigString, 2)
Ln = Len(BigString)
MsgBox Ln
If Ln < 250 Then
Set BigRange = Range(BigString)
Else
Set BigRange = Nothing
arr = Split(BigString, ",")
For Each a In arr
If BigRange Is Nothing Then
Set BigRange = Range(a)
Else
Set BigRange = Union(BigRange, Range(a))
End If
Next a
End If
BigRange.Select
End Sub
For i = 10, the code will the the direct method, but if the code were i=100, the array method would be used.
The solution, as Rory pointed out, is to use the Union method. To minimize the number of calls from MATLAB to the ActiveX server, this is what I did:
str = strjoin(cellInd, ',');
isep = find(str == ',');
isplit = diff(mod(isep, 250)) < 0;
isplit = [isep(isplit) (length(str) + 1)];
hRange = hSheet.Range(str(1:(isplit(1) - 1)));
for ii = 2:numel(isplit)
hRange = hActX.Union(hRange, ...
hSheet.Range(str((isplit(ii-1) + 1):(isplit(ii) - 1))));
end
I used 250 in the mod to account for the cell names being up to 6 characters long, which is sufficient for me.

solving the Chapman-Richards equation

I need to find a way to solve the Chapman-Richards with 3 parameters. The equation is
F=a(1-EXP(-bt)) power c
It is a nonlinear problem. The goal is to minimize the error and the constraints are that the 3 variables must be >= 0.0001. Our current implementation uses Excel and the Solver plugin (GRG nonlinear method). But now, we need to implement all this without making use of Excel.
My questions are:
1. Is it possible to use MS Solver Foundation to solve this problem?
I have read some docs and understand that MS Solver Foundation uses either the Nelder Mead Solver or the hybrid local search solver to solve nonlinear problems. Does anyone know if my particular problem can be solved using these methods? And, will the results be same as using GRG nonlinear method of Excel's Solver addin?
If not, is it possible to implement the GRG nonlinear method of Excel Solver?
Is there any other way to implement this?
Thanks for your replies in advance.
kar
Addendum:
Sorry, I forgot to mention that t is the time variable. a, b, and c are the parameters which can be changed by the solver.
Yes, it can be done with the Nelder-Mead solver in Solver Foundation. Here is some example code in C#. Just make sure you reference the Microsoft.Solver.Foundation assembly.
private const double t = 1.0;
public static void Main()
{
var solver = new NelderMeadSolver();
// Objective function.
int objId;
solver.AddRow("obj", out objId);
solver.AddGoal(objId, 0, true);
// Define variables.
int aId, bId, cId;
solver.AddVariable("a", out aId);
solver.AddVariable("b", out bId);
solver.AddVariable("c", out cId);
// Define bounds.
solver.SetLowerBound(aId, 0.001);
solver.SetLowerBound(bId, 0.001);
solver.SetLowerBound(cId, 0.001);
// Assign objective function delegate.
solver.FunctionEvaluator = FunctionValue;
// Solve.
var param = new NelderMeadSolverParams();
var solution = solver.Solve(param);
Console.WriteLine("The Result is " + solution.Result + ".");
Console.WriteLine("The minimium objective value is " +
solution.GetValue(objId) + ".");
Console.WriteLine("a = " + solution.GetValue(aId) + ".");
Console.WriteLine("b = " + solution.GetValue(bId) + ".");
Console.WriteLine("c = " + solution.GetValue(cId) + ".");
Console.ReadKey();
}
private static double FunctionValue(INonlinearModel model, int rowVid,
ValuesByIndex values, bool newValues)
{
var a = values[model.GetIndexFromKey("a")];
var b = values[model.GetIndexFromKey("b")];
var c = values[model.GetIndexFromKey("c")];
return a * Math.Pow(1.0-Math.Exp(-b * t), c);
}
I solved it with Visual Studio 2013 and Visual Basic, there is the traslation of the code.
Private Sub NelderMead()
Dim Solver As New Microsoft.SolverFoundation.Solvers.NelderMeadSolver
Dim objId As Integer
Solver.AddRow("obj", objId)
Solver.AddGoal(objId, 0, True)
Dim aId, bId, cId As Integer
Solver.AddVariable("a", aId)
Solver.AddVariable("b", bId)
Solver.AddVariable("c", cId)
Solver.SetLowerBound(aId, 0.001)
Solver.SetLowerBound(bId, 0.001)
Solver.SetLowerBound(cId, 0.001)
Solver.FunctionEvaluator = AddressOf FunctionValue
Dim Par As New Microsoft.SolverFoundation.Solvers.NelderMeadSolverParams
Dim Solucion = Solver.Solve(Par)
Debug.Print(Solucion.Result)
End Sub
Function FunctionValue(Model As Microsoft.SolverFoundation.Services.INonlinearModel, _
rowVid As Integer, _
Values As Microsoft.SolverFoundation.Services.ValuesByIndex, _
newValues As Boolean) As Object
Dim a, b, c As Double
a = Values(Model.GetIndexFromKey("a"))
b = Values(Model.GetIndexFromKey("b"))
c = Values(Model.GetIndexFromKey("c"))
Return a * Math.Pow(1.0 - Math.Exp(-b * t), c)
End Function