That is a complex task. You will find enough information how to read a formula from a cell or write a formula. Here some additions for the Math object. The code is not ready to use, but shows you some solutions.
The first one is for first time writing the Math objects.
sub AddMathToCell(byval nCol as long, byval nRow as long, byval nSheet as long)
rem Make sure it is a spreadsheet document and sheet exists
dim oDoc as variant: oDoc=thisComponent
dim sDocType as string: sDocType = oDoc.Identifier
if sDocType <> "com.sun.star.sheet.SpreadsheetDocument" then
msgbox "Only for spreadsheets"
exit sub
end if
dim oSheets as variant: oSheets = oDoc.Sheets
if oSheets.count <= nSheet then
msgbox "Sheet does not exists"
exit sub
end if
dim oOneSheet as variant: oOneSheet = oSheets.getByIndex(nSheet)
dim oOneCell as variant: oOneCell = oOneSheet.getCellByPosition(nCol,nRow)
dim oDrawPage as variant: oDrawPage = oOneSheet.DrawPage
dim oMathOLE as variant: oMathOLE = oDoc.createInstance("com.sun.star.drawing.OLE2Shape")
dim oPos as new com.sun.star.awt.Point
Dim oSize as new com.sun.star.awt.Size
oPos.x = oOneCell.Position.X
oPos.y = oOneCell.Position.Y
oMathOLE.Position = oPos
oSize.Width = oOneCell.Size.Width
oSize.Height = oOneCell.Size.Height
oMathOLE.Size = oSize
oMathOLE.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
oDrawPage.add(oMathOLE)
dim oMathDoc as variant: oMathDoc = oMathOLE.Model
dim sFormel as string: sFormel ="{a^2-b^2}over{a-b}"
oMathDoc.Formula = sFormel
oDoc.Modified = true
end sub
The second one is for accessing the existing Math objects.
sub ShowAllMathFormulas(byval nSheet as long)
rem Make sure it is a spreadsheet document and sheet exists
dim oDoc as variant: oDoc=thisComponent
dim sDocType as string: sDocType = oDoc.Identifier
if sDocType <> "com.sun.star.sheet.SpreadsheetDocument" then
msgbox "Only for spreadsheets"
exit sub
end if
dim oSheets as variant: oSheets = oDoc.Sheets
if oSheets.count <= nSheet then
msgbox "Sheet does not exists"
exit sub
end if
dim oOneSheet as variant: oOneSheet = oSheets.getByIndex(nSheet)
dim oDrawPage as variant: oDrawPage = oOneSheet.DrawPage
dim nDrawObjectCount as integer: nDrawObjectCount = oDrawPage.count
dim i as integer
dim oShape as variant
dim oModel as variant
dim sMathFormula as string
for i = 0 to nDrawObjectCount - 1
oShape = oDrawPage.getByIndex(i)
if oShape.supportsService("com.sun.star.drawing.OLE2Shape") then
if oShape.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997" then
rem It is a formula object
oModel = oShape.model
sMathFormula = oModel.formula
msgbox(sMathFormula)
else
rem external OLE do not have a model, but all have a CLSID
msgbox(oShape.CLSID)
end if
else
msgbox(oShape.ShapeType)
end if
msgbox(oShape.Anchor.AbsoluteName)
next i
end sub