Skip to content

Instantly share code, notes, and snippets.

@akaleeroy
Created March 4, 2021 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akaleeroy/ee0f2e7d2c5f1519968367dc75c03a0c to your computer and use it in GitHub Desktop.
Save akaleeroy/ee0f2e7d2c5f1519968367dc75c03a0c to your computer and use it in GitHub Desktop.
Extract all chart data to sheets LibreOffice Writer macro
REM ***** BASIC *****
'Extract data from all charts in document
'Will open all charts as new Calc spreadsheets
'and you can edit them and save them there.
'Based on ExtractDiagramToSheet by sasha.libreoffice@gmail.com
Sub ExtractAllDiagramsToSheets
'On error resume next
Dim embObjects as Object
doc = ThisComponent
embObjects = doc.embeddedObjects
'Loop over all embedded objects
For chartNumber = 0 to embObjects.Count - 1
o = embObjects.getByIndex(chartNumber)
oExt = o.ExtendedControlOverEmbeddedObject
m = oExt.component
If isEmpty(m) Then
MsgBox ("Empty diagram", "Wrong object")
Exit Sub ' it is not a chart selected
EndIf
oURL = "private:factory/scalc"
oDoc = starDeskTop.loadComponentFromURL(oURL, "_blank", 0, Array() )
oSheet = oDoc.Sheets(0)
'adding titles and names
oSheet.Name = "Extracted diagram"
oSheet.getCellByPosition(0, 0).string = m.title.string
oSheet.getCellByPosition(0, 1).string = m.subtitle.string
'Extracting data into variables
RowTitles = m.data.rowDescriptions
ColumnTitles = m.data.columnDescriptions
DiagramData = m.data.data
AmountOfRows = UBound(RowTitles) ' both zero-bazed
AmountOfColumns = UBound(ColumnTitles)
'titles of columns and rows
For i = 0 to AmountOfRows : oSheet.getCellByPosition(0, i+3).string = RowTitles(i) : Next i
For i = 0 to AmountOfColumns : oSheet.getCellByPosition(i+1, 2).string = ColumnTitles(i) : Next i
'Data of chart itself
For y = 0 to AmountOfRows
For x = 0 to AmountOfColumns
oSheet.getCellByPosition(x+1, y+3).value = DiagramData(y)(x)
Next x
Next y
Next chartNumber
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment