OO-Snippets: access cells

Commons

KeywordsCalc, cells, spreadsheet, access, XSheet, getCellByPosition, XNAmeAccess, getByName, XCell, getValue, getString, getString, getFormula, setValue, setString, setFormula
LanguageOOBasic
ApplicationCalc
AuthorsSasa Kelecevic (initial)
Andrew Pitonyak (initial)
Laurent Godard (initial)
Michael Hoennig (initial)
Tom Schindl
Supported Versions
Supported OS
Question How can the cells of a given OOo/Calc document be accessed?
Set objServiceManager = CreateObject("com.sun.star.ServiceManager")
Set objDesktop = 
objServiceManager.createInstance("com.sun.star.frame.Desktop")
Dim args()
Set objCalcDocument =
objDesktop.loadComponentFromURL("private:factory/scalc", "_blank", 0, args)
Set objSheets = objCalcDocument.Sheets
aCell=???
Answer

Cells can be accessed via the sheet in which they are: objSheets.getByName("TheSheetName").

Code-Snippet-Listing (snippet-source)

'Print Cell Value, String, Or Formula
'******************************************************************
'Author: Sasa Kelecevic
'email:  scat@teol.net
Sub ExampleGetValue
   Dim oDocument As Object, oSheet As Object, oCell As Object
   oDocument=ThisComponent
   oSheet=oDocument.Sheets.getByName("Sheet1")
   oCell=oSheet.getCellByPosition(0,0) 'A1
   print oCell.getValue
   'print oCell.getString
   'print oCell.getFormula
End sub

'Set Cell Value, Format, String, Or Formula
'******************************************************************
'Author: Sasa Kelecevic
'email:  scat@teol.net
Sub ExampleSetValue
   Dim oDocument As Object, oSheet As Object, oCell As Object
   oDocument=ThisComponent
   oSheet=oDocument.Sheets.getByName("Sheet1")
   oCell=oSheet.getCellByPosition(0,0) 'A1
   oCell.setValue(23658)
   'oCell.NumberFormat=2   '23658.00
   'oCell.setString("oops")
   'oCell.setFormula("=FUNCTION()")
   'oCell.IsCellBackgroundTransparent = TRUE
   'oCell.CellBackColor = RGB(255,141,56)
End Sub

Changelog

DateUserModification
2004-06-22tomsontomModified to match new snippet-DTD
0000-00-00sasaIntial release

and