在Powershell中使用辅助yaxis创建Excel图表

时间:2018-10-05 16:14:07

标签: powershell charts

我希望通过Powershell脚本创建excel图表,但要使用两组数据系列-每组分别在yaxis上。我能够创建图表,但是用一个yaxis代码:

function drawchart{
param($value1,$value2=$null,$items)

$excelAxes = [Microsoft.Office.Interop.Excel.XlAxisType]
$xl.SheetsInNewWorkbook = 2
$wb = $xl.Workbooks.Add()
$wb.worksheets("Sheet1").name = "Raw Data"
$wb.worksheets("Sheet2").name = "Graphs"

$wsdata = $wb.worksheets.item("Raw Data")
$wschart = $wb.worksheets.item("Graphs")

$chart = $wschart.Shapes.AddChart().Chart
$currentseries1 = $chart.SeriesCollection().NewSeries
$currentseries2 = $chart.SeriesCollection().NewSeries

foreach($item in $items) # $items is a collection with custom objects
{
    if(Where-Object -InputObject $item -Property name_ -match -value "$($value1)")
    {
        $currentseries1 = $chart.SeriesCollection().add($wsdata.Range($wsdata.Cells(1,$item.indexStart), $wsdata.Cells(20,$item.indexEnd))
        $currentseries1.name = $item.name_
        $currentseries1.xvalues= $wsdata.Range($wsdata.Cells(1,1), $wsdata.Cells(20,1)) 
        $currentseries1.AxisGroup = 1
    }
    if($value2 -ne $null -and (Where-Object -InputObject $item -Property name_ -match -value "$($value2)"))
    {
        $currentseries2 = $chart.SeriesCollection().add($wsdata.Range($wsdata.Cells(1,$item.indexStart), $wsdata.Cells(20,$item.indexEnd))
        $currentseries2.name = $item.name_
        $currentseries2.xvalues= $wsdata.Range($wsdata.Cells(1,1), $wsdata.Cells(20,1)) 
        $currentseries1.AxisGroup = 2
    }
}

$chart.HasTitle = $true
$chart.ChartTitle.Text = "My chart"
$chart.ChartType = 4
$chart.ChartArea.Height = 50
$chart.ChartArea.Width = 100
$chart.ChartArea.Top = 10
$chart.ChartArea.Left = 10
}

1 个答案:

答案 0 :(得分:0)

我找到了问题的根源。是:

$chart.ChartType = 4

行。因为它被放置在数据序列设置之后,所以会导致重置AxisGroup系列。而且

$currentseries1 = $chart.SeriesCollection().NewSeries
$currentseries2 = $chart.SeriesCollection().NewSeries

不是必需的,可以将其删除。

最后,工作代码是:

function drawchart{
param($value1,$value2=$null,$items)

$excelAxes = [Microsoft.Office.Interop.Excel.XlAxisType]
$xl.SheetsInNewWorkbook = 2
$wb = $xl.Workbooks.Add()
$wb.worksheets("Sheet1").name = "Raw Data"
$wb.worksheets("Sheet2").name = "Graphs"

$chart.ChartType = 4

$wsdata = $wb.worksheets.item("Raw Data")
$wschart = $wb.worksheets.item("Graphs")

$chart = $wschart.Shapes.AddChart().Chart

foreach($item in $items) # $items is a collection with custom objects
{
    if(Where-Object -InputObject $item -Property name_ -match -value "$($value1)")
    {
        $currentseries1 = $chart.SeriesCollection().add($wsdata.Range($wsdata.Cells(1,$item.indexStart), $wsdata.Cells(20,$item.indexEnd))
        $currentseries1.name = $item.name_
        $currentseries1.xvalues= $wsdata.Range($wsdata.Cells(1,1), $wsdata.Cells(20,1)) 
        $currentseries1.AxisGroup = 1
    }
    if($value2 -ne $null -and (Where-Object -InputObject $item -Property name_ -match -value "$($value2)"))
    {
        $currentseries2 = $chart.SeriesCollection().add($wsdata.Range($wsdata.Cells(1,$item.indexStart), $wsdata.Cells(20,$item.indexEnd))
        $currentseries2.name = $item.name_
        $currentseries2.xvalues= $wsdata.Range($wsdata.Cells(1,1), $wsdata.Cells(20,1)) 
        $currentseries2.AxisGroup = 2
    }
}

$chart.HasTitle = $true
$chart.ChartTitle.Text = "My chart"
$chart.ChartArea.Height = 50
$chart.ChartArea.Width = 100
$chart.ChartArea.Top = 10
$chart.ChartArea.Left = 10
}