OpenOffice Compare two cell strings - macros

So I am working on a macro in Basic, and where I'm at now I need to compare two columns for duplicates.
Here's what I have going so far:
for i = 0 To 5
Cell1 = Sheet.getCellByPosition(0,i)
for j = 0 to 5
Cell2 = Sheet.getCellByPosition(1,j)
rem COMPARISON WOULD HAPPEN HERE
Next j
Next i
I would like to do something along the lines of: if Cell1.String == Cell2.String then ...
This is my first attempt at writing a macro and so I would greatly appreciate any help and/or guidance.
Thanks!
Also on a side note if anyone know of good tutorials.documentation for this other than wiki, I would be extremely grateful for the link

You should store all values of the first column into an array and then compare every value of the second column with all entries of the array using a simple recursion.
A code that may work is
Dim firstcolumn(5) As String
For i = 0 to 5
firstcolumn(i) = Sheet.getCellByPosition(0,i)
Next i
For j = 0 to 5
Cell2 = Sheet.getCellByPosition(1,j)
for i = 0 to 5
if Cell2 = firstcolumn(i) then
MsgBox("The value of the cell " + i + " in the first column is the same with the value of the cell " + j + " of the second column")
Next i
Next j
The best place to look for code samples is the openoffice forum https://forum.openoffice.org/en/forum/
I hope that the above will help you.

Related

Removing number from for loop

I have a question about Matlab.
My chunk of code looks like this:
piece = 1:25;
piece([12]) = [];
for sub = 1:(valid_subject)
group_data = [];
for j = 1:length(piece)
group_data = [group_data; subject_data{sub}.trial{j}'];
end
group_mat(:,:,1,sub) = group_data
end
What I want to do is to loop through all trials, from 1 to 25, but omitting trial 12. By using length(piece) I just go through 24 numbers, from 1 to 24. Do you know another way to have numbers from 1 to 25 without 12 as j?
Thank you!
You have modified piece removing 1 item therefore piece now only has 24 elements and you are telling the for loop to follow [1:1:length(piece)]
Instead of for j=1:length(piece) use for j=1:piece

Libreoffice calc - how to write a same value into a range

I know how to 'select' a range in LO (7.2.4.1) Calc BASIC ....
ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("D1:H6")
But how to write a value, e.g. "1", into that range using BASIC?
myRange = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("D1:H6")
myRange.Value = 1
Gives an "property or method not found" error. But I can't find any properties or values to go after Range to allow me to do what I want. Flailing around and trying
myRange.setValue = 1
myRange.writeValue = 1
myRange.setString = "1"
and numerous other variants don't work either.
Would really appreciate the solution. Thanks.
You can edit the value of an individual cell, but not the entire range. You will have to iterate over all the cells in the range one at a time, changing the value of each of them.
Sub Set1ToD1H6
myRange = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("D1:H6")
For i = 0 To myRange.getRows().getCount()-1
For j = 0 To myRange.getColumns().getCount()-1
myRange.getCellByPosition(j, i).setValue(1)
Next j
Next i
End Sub
But since the read-write operation to a cell is comparable in time to the read-write operation to a whole range, it is preferable to use another method - to prepare data in an array and write from it to a range in one operation:
Sub Set1ToRange
myRange = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("D1:H6")
dataOfRange = myRange.getData()
For i = LBound(dataOfRange) To UBound(dataOfRange)
For j = LBound(dataOfRange(i)) To UBound(dataOfRange(i))
dataOfRange(i)(j) = 1
Next j
Next i
myRange.setData(dataOfRange)
End Sub
(For your example, this will be approximately 30 times faster, for a larger range the time winnings will be even more significant)
The .getData() and .setData() methods work on numeric range values. To work with text strings (and numbers), use .getDataArray() and .setDataArray(), for working with cell formulas use .getFormulaArray() and .setFormulaArray()

Get Row and Column number from "ThisComponent.CurrentSelection" in libreoffice calc basic

I have this code where I can get which one is the current selected cell and use it to modify its value:
theSelection = ThisComponent.CurrentSelection
theSelection.setString("some value")
Now I want to move to the next column to the right, if it was Microsoft excel VBA I could just use something like theSelection.Offset(0,1) but that's not the case. So I'm doing some workarounds of course:
nextCell = oActiveSheet.getCellByPosition( ???currentColumn + 1, ???currentRow)
ThisComponent.CurrentController.select( nextCell )
I just want to know the simplest way to replace these ??? to the actual values of the theSelection var to move to the next column to the right.
I also tried this:
nextCell = oActiveSheet.getCellByPosition( column() + 1, row())
But I don't know why it is always returning column() = 1 and row() = 1 in regardless of which is the value of the CurrentSelection. Thanks in advance for the help.
Get the cell address.
Sub ChangeAndThenGoToCellToRightOfSelection
oActiveSheet = ThisComponent.getCurrentController().getActiveSheet()
oSels = ThisComponent.getCurrentSelection()
If oSels.supportsService("com.sun.star.sheet.SheetCell") Then
'A single cell is selected.
oSels.setString("some value")
address = oSels.getCellAddress()
nextCell = oActiveSheet.getCellByPosition(address.Column + 1, address.Row)
ThisComponent.CurrentController.select(nextCell)
End If
End Sub
To see what an object can do, use an introspection tool such as XrayTool or MRI.

how to select multiple cells using offset variable but keeping what is in top 2 rows selected

I want to multi select cell on a worksheet. I might start with selecting a2:g22 but next time I want to select a2:g2 and offset the remaining rows by 20 so they will become a23:g23. The offset will have a variable which will have 20 added to it each time the code runs.
NextRow = Range("ba2")
NextRow = NextRow + 20
Range("a2:g2,a3:g22").Offset(NextRow, 0).Select
If nextrow = 0 then range a2:g2 is selected and a3:g22 is selected then I add 20 to nextrow and I want a2:g2 to be selected and a23:g42 selected.
What I get instead is a22:g22 selected and a23:g42 selected.
After doing something else for a few hours I realized that offset will always work with the entire range whatever that might be. So I tried replacing the cell references in the range with a variable with only partial success. If I have a variable called SelRow and I put in it the cell references Will that work. So that code looked like this:
SelRow = "A23:G42"
Range("A2:G2",SelRow).Select
This didn't work because my selection was A2:G42 so I changed it to this:
SelRow = "A2:G2,A23:G42"
Range(SelRow).Select
And that worked. So all I needed to do was to start with a reference number that was used to calculate 2 more variables called StRow and EndRowThe reference number can and will be anything but for now I started with 23.
StRow=23
EndRow = StRow + 19
NowSel = "a2:G2,A" & StRow & ":G" & EndRow
Range(NowSel).Select
That worked. The result of NowSelin this case was A2:G2,A23:G42This can of course get quite long so my final code looks like this:
StRow = Worksheets("Cell List").Range("A1") 'Get the last row number used
StRow = StRow + 20 'Set the next start row to last row + 20
EndRow = StRow + 19 'Set the last row for selection to start row + 19
NowSel = "a2:c2,f2:g2,a" & StRow & ":c" & EndRow & ",f" & StRow & ":g" & EndRow
Range(NowSel).Select
And now the following cells are selected A2:C2and F2:G2 and A23:C42and F23:G42
Now I can create charts just by changing a reference number.

What does for i = 1 ; i <= power ; 1 += 1) { mean?

Sorry for pretty basic question.
Just starting out. Using flowgorithm to write code that gives out a calculation of exponential numbers.
The code to do it is:
function exponential(base, power) {
var answer;
answer = 1;
var i;
for (i = 1 ; i <= power ; i+= 1) {
answer = answer * base;
}
return answer;
f
then it loops up to the number of power. And i just understand that in the flowgorithm chart but i dont understand the code for it.
what does each section of the for statement mean?
i = 1 to power, i just need help understanding how it is written? What is the 1+= 1 bit?
Thanks.
The exponential function will take in 2 parameters, base and power.
You can create this function and call (fire) it when ever it is needed like so exponential(2,4).
The for (i = 1; 1 <= power; i+=1) is somewhat of an ugly for loop.
for loops traditionaly take three parameters. The first parameter in this case i =1 is the assignment parameter, the next one 1 <= power is the valadation parameter. So if we call the function like so...exponential(2,4) is i less than 4? The next parameter is an increment/decrement parameter. but this doesnt get executed until the code inside the for loop gets executed. Once the code inside the for loop is executed then this variable i adds 1 to itself so it is now 2. This is usefull because once i is no longer less than or equal to power it will exit the for loop. So in the case of exponential(2,4) once the code inside this for loop is ran 5 times it will exit the for loop because 6 > 5.
So if we look at a variable answer, we can see that before this for loop was called answer was equal to 1. After the first iteration of this for loop answer = answer times base. In the case of exponential(2,4) then answer equals 1 times 2, now answer =2. But we have only looped through the foor loop once , and like i said a for loop goes like (assignment, validator, "code inside the foor loop". then back up to increment/decrement). So since we to loop through this for loop 5 times in the case of exponential(2,4) it will look like so.
exponential(2,4)
answer = 1 * 2
now answer = 2
answer = 2 * 2
now answer = 4
answer = 4 * 2
now answer = 8
answer = 8 * 2
now answer = 16
answer = 16 * 2
now answer = 32
So if we could say... var int ans = exponential(2,4)
Then ans would equal 32 hence, the return answer; at the last line of your code.