, 2 min read
Programming in Excel VBA Compared To LibreOffice (OpenOffice) Calc
Original post is here eklausmeier.goip.de/blog/2013/10-27-programming-in-excel-vba-compared-to-libreoffice-openoffice-calc.
Programming in Excel VBA is quite easy. Below are some simple basic concepts to access and change the spreadsheet data.
Worksheets(i)
references the i-th sheet, usually they are called Sheet1, Sheet2, Sheet3Cells(i,j)
references i-th row, j-th column, e.g.,Cells(2,3)
is C2Cells(i,j).Value
is the content of the cell- `Cells(i,j).AddComment("my comment")` adds a comment to the cell
Cells(i,j).Interior.Color
sets the background color of the cellMsgBox(string)
for displaying a short text in a message boxIsEmpty(cell)
checks whether cell is emptyWorksheets(i).Cells.Interior.ColorIndex=xlNone
to clear background color in all cells in a worksheetWorksheets(i).Cells.ClearComments
to clear all comments in all cells in a worksheetWorksheets(i).Cells.Clear
to wipe out all content in a worksheetWorksheets(i).Activate
makes i-th worksheet active (bring to foreground)Application.Dialogs(xlDialogSaveAs).Show "filename.csv", xlCSV
open file-save-as dialogUCase(...)
return upper case of argument
The usual BASIC-style statements are used, e.g.,
for i=1 to n ... next
if ... then ... else ... end if
select case variable case value ...end select
Dim x as String
,Dim i as Integer
That's basically all it needs to program in Excel VBA.
Doing the same in LibreOffice (OpenOffice) is somewhat similar but usually much more arcane and more dogmatic. Information on various BASIC functionality seems to be spread across various internet forums.
In case one wants a push-button in the spreadsheet: The connection between clicking on a push button and its association with a subroutine is not by naming convention but rather by explicit association with Events.
Now the above Excel functionality in LibreOffice.
ThisComponent.getSheets().getByIndex(0)
references the first sheet, usually called Sheet1ThisComponent.getSheets().getByIndex(0).getCellByPosition(i,j).getString()
fetching character string from cell at row (i+1), and column (j+1)...getValue()
for the numeric valueThisComponent.getSheets().getByIndex(0).Annotations.insertNew( ThisComponent.getSheets().getByIndex(0).getCellByPosition(0,0).getCellAddress, "New comment" )
for adding a commentThisComponent.getSheets().getByIndex(0).getCellByPosition(0,0).CellBackcolor = 48000
sets the background colorMsgBox(string)
as aboveThisComponent.getSheets().getByIndex(0).getCellByPosition(0,0).Type = 2
to check if cell is emptyThisComponent.getSheets().getByIndex(1).ClearContents( 1023 )
to delete everything in that sheet- See
ClearContents()
above - See
ClearContents()
above ThisComponent.getcurrentController.setActiveSheet( ThisComponent.getSheets().getByIndex(2) )
to activate sheet #3- Somehow the lines with
ThisComponent.storeAsUrl("file://tmp/Test.csv", args2() )
, but didn't dwell into that any further ...setString( UCase(...getString()))
to uppercase a cell