I am having issues doing a pivot on a excel sheet and I am not sure what I am doing wrong.
Here is the powershell code I used
# requires excell COM
#Create excel COM object
$excel = New-Object -ComObject excel.application
#Make Visible
$excel.Visible = $True
#Add a workbook
$workbook = $excel.Workbooks.Add()
#Remove other worksheets
1..2 | ForEach {
$Workbook.worksheets.item(2).Delete()
}
#Connect to first worksheet to rename and make active
$serverInfoSheet = $workbook.Worksheets.Item(1)
$serverInfoSheet.Name = 'DiskInformation'
$serverInfoSheet.Activate() | Out-Null
#Create a Title for the first worksheet and adjust the font
$row = 1
$Column = 1
#Create a header for Disk Space Report; set each cell to Bold and add a background color
$serverInfoSheet.Cells.Item($row,$column)= 'ColumnA'
$serverInfoSheet.Cells.Item($row,$column).Interior.ColorIndex =48
$serverInfoSheet.Cells.Item($row,$column).Font.Bold=$True
$Column++
$serverInfoSheet.Cells.Item($row,$column)= 'ColumnB'
$serverInfoSheet.Cells.Item($row,$column).Interior.ColorIndex =48
$serverInfoSheet.Cells.Item($row,$column).Font.Bold=$True
$Column++
#Now it is time to add the data into the worksheet!
#Increment Row and reset Column back to first column
$row++
$Column = 1
$serverInfoSheet.Cells.Item($row,$column)= "a"
$Column++
$serverInfoSheet.Cells.Item($row,$column)= "b"
$Column++
#Increment to next row and reset Column to 1
$Column = 1
$row++
# rename workbook
$workbook = $workbook
#$workbook = $excel.Workbooks.Add()
# Get sheets
$ws3 = $workbook.worksheets | where {$_.name -eq "DiskInformation"} #<------- Selects sheet 3
$xlPivotTableVersion12 = 3
$xlPivotTableVersion10 = 1
$xlCount = -4112
$xlDescending = 2
$xlDatabase = 1
$xlHidden = 0
$xlRowField = 1
$xlColumnField = 2
$xlPageField = 3
$xlDataField = 4
# R1C1 means Row 1 Column 1 or "A1"
# R65536C5 means Row 65536 Column E or "E65536"
$PivotTable = $workbook.PivotCaches().Create($xlDatabase,"Report!R1C1:R65536C5",$xlPivotTableVersion10)
$PivotTable.CreatePivotTable("Pivot!R1C1") | Out-Null
[void]$ws3.Select()
$ws3.Cells.Item(3,1).Select()
$workbook.ShowPivotTableFieldList = $true
$PivotFields = $ws3.PivotTables('Tables1') #.PivotFields("Computername") # Worksheet Name is Server
$PivotFields.Orientation = $xlRowField
$PivotFields.Position = 1
Here is the error message I receive Exception calling "CreatePivotTable" with "1" argument(s): "The parameter is incorrect. (Exception from HRESULT: 0x8007 0057 (E_INVALIDARG))" Property 'Orientation' cannot be found on this object; make sure it exists and is settable. At C:\ASM\scripts\Powershell\TEST\test_excel4.ps1:80 char:14 + $PivotFields. <<<< Orientation = $xlRowField + CategoryInfo : InvalidOperation: (Orientation:String) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound
Property 'Position' cannot be found on this object; make sure it exists and is settable. At C:\ASM\scripts\Powershell\TEST\test_excel4.ps1:81 char:14 + $PivotFields. <<<< Position = 1 + CategoryInfo : InvalidOperation: (Position:String) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound
Thank you for the help in advanced.
WB.PivotCaches.Add(...).CreatePivotTable()
but if you look at your$PivotTable
variable the line you're receiving the error on would be read as$workbook.PivotCaches().Create(...).CreatePivotTable()
which would be incorrect syntax (I think) Here's an MSDN article about using this method with Excel that might help msdn.microsoft.com/en-us/library/office/… – SierraOscar