Ask Your Question
1

create pivot table with basic macro

asked 2017-07-25 08:43:54 +0200

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

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 ...
(more)
edit retag flag offensive close merge delete

Comments

-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 ...

Lupp gravatar imageLupp ( 2017-07-25 11:29:36 +0200 )edit

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.

dumischbaenger gravatar imagedumischbaenger ( 2017-07-27 07:26:33 +0200 )edit

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.

Lupp gravatar imageLupp ( 2017-07-27 08:56:32 +0200 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2017-11-04 18:06:14 +0200

JPAlpano gravatar image

updated 2017-11-05 03:12:35 +0200

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

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-07-25 08:43:54 +0200

Seen: 584 times

Last updated: Nov 05 '17