Lesen und Schreiben von Werten in Bereiche

Makros in LibreOffice-Calc müssen häufig Werte aus/in Tabellen lesen und schreiben. Auf dieser Hilfeseite werden die verschiedenen Ansätze für den Zugriff auf Tabellen und Bereiche zum Lesen oder Schreiben ihrer Werte beschrieben.

note

Alle auf dieser Seite vorgestellten Beispiele können sowohl in Basic als auch in Python implementiert werden.


Zugriff auf eine einzelne Zelle

Im folgenden Beispiel wird der numerische Wert 123 in die Zelle "A1" der aktuellen Tabelle eingegeben.


    Dim oSheet as Object
    Dim oCell as Object
    oSheet = ThisComponent.CurrentController.getActiveSheet()
    oCell = oSheet.getCellRangeByName("A1")
    oCell.setValue(123)
  

Das Gleiche kann mit Python erreicht werden:


    doc = XSCRIPTCONTEXT.getDocument()
    sheet = doc.getCurrentController().getActiveSheet()
    cell = sheet.getCellRangeByName("A1")
    cell.setValue(123)
  

Beachten Sie, dass in den vorherigen Beispielen auf die Zelle mit ihrem Bereichsnamen "A1" zugegriffen wird. Es ist auch möglich, über Indizes auf Zellen zuzugreifen, als wäre die Tabelle eine Matrix, in der Spalten und Zeilen ab Null indiziert sind.

Dies kann mit der Methode getCellByPosition(colIndex, rowIndex) erreicht werden, die einen Spalten- und einen Zeilenindex aufnimmt. Das folgende Beispiel in Basic ändert den Textwert in Zelle "C1" (Spalte 2, Zeile 0).


    oSheet = ThisComponent.CurrentController.getActiveSheet()
    oCell = oSheet.getCellByPosition(2, 0)
    oCell.setString("Hello")
  

Dieses Beispiel kann in Python wie folgt implementiert werden:


    doc = XSCRIPTCONTEXT.getDocument()
    sheet = doc.getCurrentController().getActiveSheet()
    cell = sheet.getCellByPosition(2, 0)
    cell.setString("Hello")
  
note

Der Hauptunterschied zwischen Python- und Basic-Skripten besteht darin, wie Sie mithilfe der Kontextvariablen XSCRIPTCONTEXT Zugriff auf das Tabellen-Objekt erhalten. Danach sind alle Methoden und Eigenschaften in Basic und Python identisch.


Werte, Zeichenfolgen und Formulare

Calc cells can have three types of values: numeric, strings and formulas. Each type has its own set and get methods:

Type

Get Method

Set Method

Nummerisch

getValue()

setValue(newValue)

Text

getString()

setString(newString)

Formula

getFormula()

setFormula(newFormula)


note

Dates and currency values are considered as numeric values in Calc.


The following example enters numeric values into cells "A1" and "A2" and inserts a formula in cell "A3" that returns the multiplication of these values.


    oSheet = ThisComponent.CurrentController.getActiveSheet()
    oCell = oSheet.getCellRangeByName("A1")
    oCell.setValue(10)
    oCell = oSheet.getCellRangeByName("A2")
    oCell.setValue(20)
    oCell = oSheet.getCellRangeByName("A3")
    oCell.setFormula("=A1*A2")
  

Accessing Ranges in Different Sheets

The previous examples used only the active sheet to perform operations. It is possible to access cell ranges in different sheets by their indices or names.

The example below enters a numeric value into cell "A1" of the sheet named "Sheet2".


    oSheet = ThisComponent.Sheets.getByName("Sheet2")
    oCell = oSheet.getCellRangeByName("A1")
    oCell.setValue(123)
  

This example can also be implemented in Python as follows:


    doc = XSCRIPTCONTEXT.getDocument()
    sheet = doc.Sheets["Sheet2"]
    cell = sheet.getCellRangeByName("A1")
    cell.setValue(123)
  

Sheets can also be accessed using zero-based indices indicating which sheet considering the order they appear in the Calc file.

In Basic, instead of using the getByName method, use Sheets(sheetIndex) as shown next:


    oSheet = ThisComponent.Sheets(0)
  

This can be done in a similar fashion in Python:


    sheet = doc.Sheets[0]
  

Using the ScriptForge Library

The Calc service of the ScriptForge library can be used to get and set cell values as follows:


    

' Loads the ScriptForge library

GlobalScope.BasicLibraries.LoadLibrary("ScriptForge")

' Gets access to the current Calc document

oDoc = CreateScriptService("Calc")

' Sets the value of cells A1 and A2

oDoc.setValue("A1", "Hello")

oDoc.setValue("A2", 123)

note

The setValue method can be used to set both numeric and text values. To set a cell formula, use the setFormula method.


With the Calc service, getting and setting cell values can be done with a single line of code. The example below gets the value from cell "A1" and shows it on a message box.


    Dim val as Variant, oDoc as Object
    oDoc = CreateScriptService("Calc")
    val = oDoc.getValue("A1")
    MsgBox val
  

The ScriptForge library also makes it simpler to access ranges in different sheets, as demonstrated in the example below:


    Dim val1, val2
    ' Gets cell "A1" from the sheet named "Sheet1"
    val1 = oDoc.getValue("Sheet1.A1")
    ' Gets cell "B3" from the sheet named "Sheet2"
    val2 = oDoc.getValue("Sheet2.B3")
    ' Places the result into cell "A1" of sheet "Report"
    Dim result : result = val1 * val2
    oDoc.setValue("Report.A1", result)
  

The examples above can also be implemented in Python as follows:


    from scriptforge import CreateScriptService
    doc = CreateScriptService("Calc")
    doc.setValue("A1", "Hello")
  

    doc = CreateScriptService("Calc")
    bas = CreateScriptService("Basic")
    val = doc.getValue("A1")
    bas.MsgBox(val)
  

    first_val = doc.getValue("Sheet1.A1")
    second_val = doc.getValue("Sheet2.B3")
    result = first_val * second_val
    doc.setValue("Report.A1", result)