Table of Contents
In the case we want edit cell from top to down in LibreOffice Calc, the loop function is useful.
Environment
- LibreOffice 5.2.0.4
Sample
When the number of loop iteration is fixed
You can fix the max and min value and use For
for loop.
The sample code below changes cell’s background color in LibreOffice Calc.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Sub Main Dim document As Object Dim sheet As Object document = ThisComponent sheet = document.Sheets(0) Dim cell As Object Dim rowIndex As Integer For rowIndex = 0 To 10 cell = sheet.getCellByPosition(1, rowindex) cell.CellBackColor = RGB(50, 50, 50) Next rowIndex End Sub |
Adding Step 2
at the last of For
line, the value rowIndex
increases by 2. With this function, you can make table zebra for example.
If you want to exit the loop while iteration is going, write Exit For
. It’s the same as Visual Basic.
Continue iteration only when the condition is meeted
You can use many style for loop in LibreOffice Basic, then I recommend simple one. It is like while (condition) { ... }
style in many languages.
1 2 3 4 5 6 7 8 |
Sub Main Dim i As Integer i = 0 Do While i < 100 i = 2 * i Loop End Sub |
If you want to exit the loop while the iteration is going, write Exit Do
.