I've been using the following article "Integrating Microsoft Excel with Powershell Part 2" to creat excel charts using Powershell. I've tweaked it a bit to use pie charts instead of bar graphs and have included 2 charts on one worksheet. Everything works
fine except that the title for the second chart errors out with the follow message:
Property 'Text' cannot be found on this object; make sure it exists and is settable.
Any ideas on how to resolve this and add a title to the second chart?
The line of code causing the error is:
$chart2.ChartTitle.Text = "Helpdesk Tickets Period 2"
Here is the full script below:
[datetime]$P1 = "1/1/2013"
[datetime]$P2 = "1/29/2013"
[datetime]$P3 = "2/26/2013"
$File = import-csv "E:\Powershell\Documents\LooTok\BCMPortal\Period Reports\Excel\helpdeskstats.csv"
$Total_Stats = $File | ?{[datetime]$_.Created -ge [datetime]::Parse($P1) -and [datetime]$_.Created -lt [datetime]::Parse($P3)}
$P2Stats = $File | ?{[datetime]$_.Created -ge [datetime]::Parse($P2) -and [datetime]$_.Created -le [datetime]::Parse($P3)}
$Grouped_Total = $Total_Stats | Group RelatedTo
$GroupedP2 = $P2Stats | Group RelatedTo
Write-Verbose "Creating Excel application"
$xl=New-Object -ComObject "Excel.Application"
#we'll need some constansts
$xlConditionValues=[Microsoft.Office.Interop.Excel.XLConditionValueTypes]
$xlTheme=[Microsoft.Office.Interop.Excel.XLThemeColor]
$xlChart=[Microsoft.Office.Interop.Excel.XLChartType]
$xlIconSet=[Microsoft.Office.Interop.Excel.XLIconSet]
$xlDirection=[Microsoft.Office.Interop.Excel.XLDirection]
Write-Verbose "Adding Worksheet"
$wb=$xl.Workbooks.Add()
$ws=$wb.ActiveSheet
$cells1=$ws.Cells
$cells2=$ws.Cells
$cells1.item(1,1)="Helpdesk Report All"
$cells2.item(1,6)="Helpdesk Report P2"
#define some variables to control navigation
$row1=3
$col1=1
$row2=3
$col2=6
#insert column headings
Write-Verbose "Adding drive headings"
"Related To","Count" | foreach {
$cells1.item($row1,$col1)=$_
$cells1.item($row1,$col1).font.bold=$True
$col1++
}
"Related To","Count" | foreach {
$cells2.item($row2,$col2)=$_
$cells2.item($row2,$col2).font.bold=$True
$col2++
}
foreach ($i in $Grouped_Total) {
$row1++
$col1=1
$cells1.item($Row1,$col1)=$i.name
$col1++
$cells1.item($Row1,$col1)=$i.count
$cells1.item($Row1,$col1).NumberFormat="0"
}
foreach ($i in $GroupedP2) {
$row2++
$col2=6
$cells2.item($Row2,$col2)=$i.name
$col2++
$cells2.item($Row2,$col2)=$i.count
$cells2.item($Row2,$col2).NumberFormat="0"
}
Write-Verbose "Adding some style"
#add some style
$range1=$ws.range("A1")
$range1.Style="Title"
$range2=$ws.Range("F1")
$range2.Style="Title"
#or set it like this
$ws.Range("A3:B3").Style = "Heading 2"
$ws.Range("F3:G3").Style = "Heading 2"
#adjust some column widths
Write-Verbose "Adjusting column widths"
#$ws.columns.item("C:C").columnwidth=15
#$ws.columns.item("D:F").columnwidth=10.5
$ws.columns.item("A:A").EntireColumn.AutoFit() | out-null
$ws.columns.item("F:F").EntireColumn.AutoFit() | out-null
#add some conditional formatting
Write-Verbose "Adding conditional formatting"
#insert a graph
Write-Verbose "Creating a graph"
$chart1=$ws.Shapes.AddChart().Chart
$chart1.chartType=$xlChart::xlPie
$Chart1.ApplyDataLabels(2)
$start1=$ws.range("A3")
#get the last cell
$Y1=$ws.Range($start1,$start1.End($xlDirection::xlDown))
$start1=$ws.range("B3")
#get the last cell
$X1=$ws.Range($start1,$start1.End($xlDirection::xlDown))
$chartdata1=$ws.Range("A$($Y1.item(1).Row):A$($Y1.item($Y1.count).Row),B$($X1.item(1).Row):B$($X1.item($X1.count).Row)")
$chart1.SetSourceData($chartdata1)
#add labels
$chart1.seriesCollection(1).Select() | Out-Null
$chart1.SeriesCollection(1).ApplyDataLabels() | out-Null
#modify the chart title
$chart1.ChartTitle.Text = "Helpdesk Tickets Period 1-2"
Write-Verbose "Repositioning graph"
$ws.shapes.item("Chart 1").top=1
$ws.shapes.item("Chart 1").left=1
#insert a graph
Write-Verbose "Creating a graph"
$chart2=$ws.Shapes.AddChart().Chart
$chart2.chartType=$xlChart::xlPie
$Chart2.ApplyDataLabels(2)
$start2=$ws.range("F3")
#get the last cell
$Y2=$ws.Range($start2,$start2.End($xlDirection::xlDown))
$start2=$ws.range("G3")
#get the last cell
$X2=$ws.Range($start2,$start2.End($xlDirection::xlDown))
$chartdata2=$ws.Range("F$($Y2.item(2).Row):F$($Y2.item($Y2.count).Row),G$($X2.item(2).Row):G$($X2.item($X2.count).Row)")
$chart2.SetSourceData($chartdata2)
#add labels
$chart2.seriesCollection(1).Select() | Out-Null
$chart2.SeriesCollection(1).ApplyDataLabels() | out-Null
#modify the chart title
$chart2.ChartTitle.Text = "Helpdesk Tickets Period 2"
Write-Verbose "Repositioning graph"
$ws.shapes.item("Chart 2").top=1
$ws.shapes.item("Chart 2").left=370
Write-Verbose "Renaming the worksheet"
#rename the worksheet
$name="6.FC Activity"
$xl.worksheets.Item("Sheet1").name=$name
#make Excel visible
$xl.Visible=$True
#$filepath=Read-Host "Enter a path and filename to save the file"
if ($filepath) {
Write-Verbose "Saving file to $filepath"
$wb.SaveAs($filepath)
$xl.displayAlerts=$False
$wb.Close()
$xl.Quit()
}
Thanks,
Joe