powershell无法向Excel图表添加多个图例条目(系列)

时间:2013-01-11 20:24:34

标签: excel powershell charts legend series

我在通过powershell向excels图表对象中的seriescollection添加多个系列时遇到问题,这是我的代码:

[threading.thread]::CurrentThread.CurrentCulture = 'en-US'

$excel = New-Object -comobject Excel.Application

$workbook = $excel.workbooks.add()

$datasheet  = $workbook.Worksheets.Item(2)
$chartsheet = $workbook.Worksheets.Item(1)

[datetime] $startDate  = "2012-11-29 00:00:00" 
[datetime] $finishDate = "2012-12-07 00:00:00"
[datetime] $dayCounter = $startDate

$startRow = 2
$startColumn = 2

$columnCounter = 2
$rowCounter = 2
while ($dayCounter -le $finishDate)
{
  $datasheet.Cells.Item($rowCounter, $columnCounter) = $dayCounter.ToShortDateString()
  $datasheet.Cells.Item($rowCounter+1, $columnCounter) = $columnCounter
  $datasheet.Cells.Item($rowCounter+2, $columnCounter) = 2 * $columnCounter
  $columnCounter++
  $dayCounter = $dayCounter.AddDays(1)
}

$datasheet.Range($rowCounter.ToString() + ":" + $rowCounter.ToString()).NumberFormat = "m/d/yyyy"

$excel.application.DisplayAlerts=$False
$chart = $chartsheet.Shapes.addChart().chart
$chart.hasTitle = $true
$chart.chartTitle.text = "Ramp Example"
$chartType = [Microsoft.Office.Interop.Excel.XlChartType]::xlLine
$chart.chartType = $chartType

$startCell = $datasheet.Cells.Item(3,2).Address($false,$false)
$endCell   = $datasheet.Cells.Item(3,10).Address($false,$false)

$startCell + ", " + $endCell

$datarange = $datasheet.Range($startCell, $endCell)
$chart.SetSourceData($datarange)
$chart.SeriesCollection(1).Name    = "First"
$chart.SeriesCollection(1).XValues = $datasheet.Range("B2", "J2")

$newSeries = $chart.SeriesCollection().NewSeries
$chart.SeriesCollection(2).Values  = $datasheet.Range("B4", "J4")
$chart.SeriesCollection(2).Name    = "Second"
$chart.SeriesCollection(2).XValues = $datasheet.Range("B2", "J2")


$excel.Visible = $True

错误:

异常调用" SeriesCollection"用" 1"参数:"参数无效" 在C:\ localwork \ tfs \ OpenExcel.ps1:49 char:24 + $ chart.SeriesCollection<<<< (2).Values = $ datasheet.Range(" B4"," J4")     + CategoryInfo:NotSpecified:(:) [],MethodInvocationException     + FullyQualifiedErrorId:ComMethodTargetInvocation

异常调用" SeriesCollection"用" 1"参数:"参数无效" 在C:\ localwork \ tfs \ OpenExcel.ps1:50 char:24 + $ chart.SeriesCollection<<<< (2).Name =" Second"     + CategoryInfo:NotSpecified:(:) [],MethodInvocationException     + FullyQualifiedErrorId:ComMethodTargetInvocation

异常调用" SeriesCollection"用" 1"参数:"参数无效" 在C:\ localwork \ tfs \ OpenExcel.ps1:51 char:24 + $ chart.SeriesCollection<<<< (2).XValues = $ datasheet.Range(" B2"," J2")     + CategoryInfo:NotSpecified:(:) [],MethodInvocationException     + FullyQualifiedErrorId:ComMethodTargetInvocation

问题是; 如何使用powershell代码获得额外的SeriesCollection条目?

任何帮助都将得到赞赏

1 个答案:

答案 0 :(得分:1)

问题解决了,不得不在newSeries上调用Invoke() - 所以:

$chart.SeriesCollection().NewSeries.Invoke()

就是这样,我想在我用Excel制作的宏中调用VBA来开始这个开发有一些区别:

ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Sheets("Sheet2").Range("B3:P3")
ActiveChart.SeriesCollection(1).Name = "=""First"""
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(2).Values = "=Sheet2!$B$4:$P$4"
ActiveChart.SeriesCollection(2).Name = "=""Second"""
ActiveChart.SeriesCollection(2).XValues = "=Sheet2!$B$2:$P$2"

要在powershell中调用它,我还没有找到一个很好的例子来动态地将这个系列添加到WEB上的Excel图表中!