create pivot table with basic macro

Hello,

I have the need to create a pivot table with a macro. I’m quite far. My test macro creates the pivot table with filter and subtotals and so on. But the layout is not as expected. I can adjust it with the data pilot layout dialogue but it is the aim to automate the task. It has something to do with the virtual “data” field (that’s the name I gave it). It is none of my columns.

If I drag it in the layout dialogue to “column fields” I get a horizontal orientation of my fields. If drag it in “row fields” I get an vertical alignment.

My questions are:
How can I reference this “data” tag from my basic macro?
How can I move it between “row fields” and “vertical fields” from my basic macro?

Here is my test macro. Perhaps it is useful for others trying to create pivot table via API:

Sub makePivot

dim doc
doc = thisComponent
dim sheets
sheets = doc.Sheets

dim pivotSheetNameOverview as string
pivotSheetNameOverview="Ueberblick"

dim oController
oController = ThisComponent.CurrentController
dim oSheetObj
'oSheetObj = oController.ActiveSheet
oSheetObj = Sheets.getByName("Unvollständigmeldungen")


dim DataCellRange
DataCellRange = oSheetObj.getCellRangeByName("Unvollständigmeldungen.A:Unvollständigmeldungen.M")
dim RangeAddress
RangeAddress = DataCellRange.RangeAddress
dim Tables
Tables = oSheetObj.DataPilotTables()   'Tables has all the DataPilot Tables in the Active Sheet

'This part of the code just removes the table if it already exists. Prevents error from running the code several times
If Tables.hasByName(pivotSheetNameOverview) THEN 
   Tables.removeByName(pivotSheetNameOverview)
End If



dim Descriptor
Descriptor = Tables.createDataPilotDescriptor()      'Descriptor contains the description of a DataPilot Table
Descriptor.setSourceRange(RangeAddress)     'RangeAddress is defined above to cover A1:C10       
Descriptor.ShowFilterButton = True                   'Don't show the Filter Button
Descriptor.IgnoreEmptyRows = True
Descriptor.RowGrand = true
Descriptor.ColumnGrand = false
Descriptor.DrillDownOnDoubleClick = true
'Descriptor.GrandTotalName="myName"


dim fields
Fields = Descriptor.getDataPilotFields()          


dim fieldVerursacher
fieldVerursacher = Fields.getByIndex(3)   'The first column of the data range has index 0
fieldVerursacher.Orientation = com.sun.star.sheet.DataPilotFieldOrientation.ROW                                    
Dim generalFunctionSeqAuto(0) 
generalFunctionSeqAuto(0)=com.sun.star.sheet.GeneralFunction.AUTO
fieldVerursacher.Subtotals = generalFunctionSeqAuto



dim fieldGruppe
fieldVerursacher = Fields.getByIndex(4)   'The first column of the data range has index 0
fieldVerursacher.Orientation = com.sun.star.sheet.DataPilotFieldOrientation.ROW                                    

'dim fieldDaten
'fieldVerursacher = Fields.getByIndex(4)   'The first column of the data range has index 0
'fieldVerursacher.Orientation = com.sun.star.sheet.DataPilotFieldOrientation.ROW                                    


dim fieldABNummer
fieldABNummer = Fields.getByIndex(0)
fieldABNummer.Orientation = com.sun.star.sheet.DataPilotFieldOrientation.DATA
fieldABNummer.Function = com.sun.star.sheet.GeneralFunction.COUNT
fieldABNummer.setName("gesamt")

dim fieldAktiv
fieldAktiv = Fields.getByIndex(12)
fieldAktiv.Orientation = com.sun.star.sheet.DataPilotFieldOrientation.DATA
'fieldAktiv.UsedHierarchy=0
fieldAktiv.Function = com.sun.star.sheet.GeneralFunction.SUM
fieldAktiv.setName("aktiv")


if(Sheets.hasByName(pivotSheetNameOverview)) then
  sheets.removeByName(pivotSheetNameOverview) 
end if

dim sheet
sheets.insertNewByName(pivotSheetNameOverview, sheets.Count) 
Sheet = Sheets.getByName(pivotSheetNameOverview)

dim cell
Cell = sheet.getCellrangeByName("A1")
Tables.insertNewByName(pivotSheetNameOverview, Cell.CellAddress, Descriptor)

'dim names
'names=Sheets.getByName("Unvollständigmeldungen").DataPilotTables().getElementNames()
'names=Sheets.getByName("Ueberblick").DataPilotTables().getElementNames()

dim hasElem
hasElem=Tables.hasElements()

wait 2000

Dim table
table=Sheets.getByName("Ueberblick").DataPilotTables().getByName(pivotSheetNameOverview)

dim filterDescription
filterDescription = table.getFilterDescriptor()
Dim filterFields(1) as New com.sun.star.sheet.TableFilterField

dim filterField

filterField=filterFields(0)
filterFields(0).Field = 3
filterFields(0).Operator = com.sun.star.sheet.FilterOperator.NOT_EMPTY
'filterFields(0).IsNumeric = False
'filterFields(0).NumericValue = 2
'filterFields(0).StringValue = ""

filterFields(1).Field = 3
filterFields(1).Operator = com.sun.star.sheet.FilterOperator.NOT_EQUAL
filterFields(1).IsNumeric = False
'filterFields(1).NumericValue = 2
filterFields(1).StringValue = "keiner"


filterDescription.FilterFields = FilterFields
wait 2000

setColWidth(0, 6, sheet)
setColWidth(1, 6, sheet)
setColWidth(2, 3, sheet)

End Sub

-1- Posting wiki is not a good idea. Any contributor with enough “karma” can edit questions and answers by others anyway.- and one with low karma will not be able to help you.
-2- You surely studied chapter “15.5.6. Data pilot and pivot tables” of Andrew Pitonyak’s famous OOME book. Very few contrbutors will be able to help you beyond that.
-3- You may also post (properly cross-linked) in the much better structurd forum.openoffice.org/en
There are some clever guys trained in old times …

Yes - I use Andrew Pitonyak’s book. I’ll try the openoffice.org forum. There are several examples regarding pivot tables in this forum. Perhaps somebody can help but I fear this is a question only a LO developer can answer. Using API in LO is always kind of exploring an unknown jungle.

Just a remark: The forum.openoffice.org/en is not owned by or under control of the Apache Software Foundation. The mentioned forum also accepts questions and contributions concerning mainly LibreOffice. Some of the most experienced contributors there regard AOO near the end of its life.
Feel invited to also use that forum as one for LibO, a structured one as distinguished from this breathlessly running ticker.

The Virtual “Data” field you are referring to can be accessed by using the name “Data”.

oVirtualDataField = oYourTableDescr.getDataPilotFields().getByName(“Data”)

You can then move its orientation using the normal way.

oVirtualDataField.Orientation = com.sun.star.sheet.DataPilotFieldOrientation.COLUMN