So I have the below code (remove “stop” if you want to test it) , that copy paste tables and charts from LibreCalc in libre writer.
I want to change the Table head (the first row) in order to be formatted in specific font, color, bold type, and size. How I define this?
example calc file (link may expire after 4 Weeks)
A very “bad” written page with explanation for macro code for tables but useful: I will test it… table font properties
Sub LoadingLibraries
BasicLibraries.LoadLibrary("XrayTool")
End Sub
Sub CopyChartstoWriter
'Definitions of variables
'Calc Dims
Dim oSheet 'Sheet containing the chart
Dim oDoc
'Document Dims
Dim oDocWriter as object
Dim sUrl$
Dim oVCur as object
Dim oDocument as object
Dim oDispatcher as object
' ThisComponent.getDrawPages.getByIndex(0).getByIndex(0).Title
' ThisComponent.getDrawPages.getByIndex(0).getByIndex(0).Description
' ThisComponent.getSheets().getbyName( "Graphs_stat" ) .getDrawPage().getByIndex(0).PersistName
' oCharts = oSheet.getCharts() | oChart = oCharts.getByName("Object 1").EmbeddedObject
' ThisComponent.getDrawPages.getByIndex(0).getByIndex(0).Name = "aaa"
oDoc = ThisComponent
oSheets = ThisComponent.getSheets()
oSheet = oSheets.getbyName( "Graphs_stat" )
oDrawPage = oSheet.getDrawPage()
Const DataRanges = "B4:D15,B21:F32,B37:E48"
split_DataRanges = Split(DataRanges,",")
cLightBlue = RGB(180,199,220)
cLightPurple = RGB(224,194,205)
cLightGreen = RGB(175,208,149)
cBlue = RGB(114,159,207)
cElectricBlue = RGB(86,10,216)
cOrange = RGB(232,162,2)
cGreen = RGB(63,175,70)
cYellow = RGB(255,255,175)
cBlack = RGB(49,64,4 )
cGray = RGB(221,221,221 )
cWhite = RGB(255,255,255)
sUrl="private:factory/swriter" 'new writer document
'sUrl=ConvertToUrl("d:/documents/example.odt") 'or your writer file
oDocWriter = StarDesktop.LoadComponentFromUrl(sURL, "_blank", 0, array()) 'open Writer document
oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
oDocument = ThisComponent.CurrentController.Frame
for n = 0 to 100 step 1
oDispatcher.executeDispatch(oDocument, ".uno:InsertPara", "", 0, Array())
next n
For i = 0 to 2 step 1
genNames = "statChart_" & i
oChart = oDrawPage.getByIndex(i)
oTableData = oSheet.getCellRangeByName( split_DataRanges(i) )
DrawObject = oChart.Title
Component = oChart.EmbeddedObject.Component
if genNames = DrawObject then
'very essential otherwise calc freeze on error
set mCurSelection = oDoc.currentSelection 'Record current selection
on local error goto finished:
Component.supportsService("com.sun.star.chart.ChartDocument")
'Transfer Table Data into Writer (copy and then Paste them)
oDoc.CurrentController.select( oTableData )
oTableData = oDoc.CurrentController.getTransferable()
oVCur = oDocWriter.CurrentController.getViewCursor 'visible cursor
oDocWriter.CurrentController.Select( oVCur ) 'select visible cursor
' Optimizing in which position Table Data will be placed
oVCur.JumpToPage( i + 1 , False)
oVCur.goDown(2, False)
oDocWriter.CurrentController.insertTransferable( oTableData ) 'Libre internal Ctrl+V
oVCur.goUp(2, False)
Dim oCursor As Object
Dim oTable As Object
Dim oTableBorder As Object
oCursor = ThisComponent.CurrentController.ViewCursor
oStarTable = com.sun.star.table
If (isEmpty(oCursor.textTable)) Then
MsgBox "Cursor is not inside a table."
Else
oTable = oCursor.TextTable
oTableBorder = oTable.TableBorder
' oStarTable.BorderLineStyle = 0 ' Solid
oTableBorder.IsLeftLineValid = True
oTableBorder.IsRightLineValid = True
oTableBorder.IsTopLineValid = True
oTableBorder.IsBottomLineValid = True
oTableBorder.IsHorizontalLineValid = True
oTableBorder.IsVerticalLineValid = True
oTableBorder.LeftLine.Color = cBlack
oTableBorder.LeftLine.InnerLineWidth = 10
oTableBorder.RightLine.Color = cBlack
oTableBorder.RightLine.InnerLineWidth = 10
oTableBorder.TopLine.Color = cBlack
oTableBorder.TopLine.InnerLineWidth = 10
oTableBorder.BottomLine.Color = cBlack
oTableBorder.BottomLine.InnerLineWidth = 10
oTableBorder.HorizontalLine.Color = cBlack
oTableBorder.HorizontalLine.InnerLineWidth = 0
oTableBorder.HorizontalLine.Color = cBlack
oTableBorder.HorizontalLine.InnerLineWidth = 0
'oTableBorder.TopLine.OutetLineWidth = 111
'oTableBorder.TopLine.LineDistance = 1
oTable.TableBorder = oTableBorder
oTable.HoriOrient = com.sun.star.text.HoriOrientation.FULL
oTable.CollapsingBorders = False
End If
stop
'Transfer Chart into Writer (copy and then Paste them)
oDoc.CurrentController.select( oChart )
data = oDoc.CurrentController.getTransferable() 'Libre internal Ctrl+C
oVCur = oDocWriter.CurrentController.getViewCursor 'visible cursor
oDoc.unlockControllers 'very essential on error
' Optimizing in which position charts will be placed
oVCur.JumpToPage( i+1 , False) 'cursor goes to x page
oVCur.goDown(31, False)
' Placing text below the Chart
oText = oVCur.text
oText.insertString(oVCur, "Some text", False)
oVCur.goUp(1, False)
oDocWriter.CurrentController.Select(oVCur) 'select visible cursor
oDocWriter.CurrentController.insertTransferable(data) 'Libre internal Ctrl+V
finished:
on error goto 0
else msgbox "no name found " & genNames
endif
next i
end sub