Page 1 of 1

[Solved] Create pivot table with Basic macro

Posted: Thu Jul 27, 2017 10:53 am
by bdonauba
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:

Code: Select all

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

Re: create pivot table with basic macro

Posted: Thu Jul 27, 2017 11:47 am
by gerard24
Try :

Code: Select all

oField = oTableDescr.getDataPilotFields().getByName("Data")
oField.Orientation = com.sun.star.sheet.DataPilotFieldOrientation.COLUMN
just before or after the "wait 2000"

Re: Create pivot table with Basic macro

Posted: Thu Jul 27, 2017 1:17 pm
by bdonauba
It worked.
Thank you for your help!