PowerShell无法向Excel图表添加超过一个图例项(系列)。

3
我在使用PowerShell为Excel的图表对象的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

错误信息:

使用1个参数调用“SeriesCollection”时出错:“无效的参数” 位于 C:\localwork\tfs\OpenExcel.ps1 的第49行第24列 + $chart.SeriesCollection <<<< (2).Values = $datasheet.Range("B4", "J4") + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation

使用1个参数调用“SeriesCollection”时出错:“无效的参数” 位于 C:\localwork\tfs\OpenExcel.ps1 的第50行第24列 + $chart.SeriesCollection <<<< (2).Name = "Second" + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation

使用1个参数调用“SeriesCollection”时出错:“无效的参数” 位于 C:\localwork\tfs\OpenExcel.ps1 的第51行第24列 + $chart.SeriesCollection <<<< (2).XValues = $datasheet.Range("B2", "J2") + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ComMethodTargetInvocation

问题是:如何使用powershell代码向SeriesCollection中添加额外的条目?

任何帮助都将不胜感激。


你可以回答自己的问题。甚至可以接受它(尽管你必须等一段时间才能接受)。这是推荐的做法,因为它比评论更好地记录了问题和答案,而且不会让问题无人回答。如果人们给你的答案点赞,你甚至可能会获得一些声望。 - DWright
顺便说一句,请务必回答您自己的问题。我注意到在SO上,与其他一些语言相比,Powershell相关内容的质量令人不安。其中一个方面是,我经常遇到没有任何答案被发布的问题,尽管评论表明有某种类型的答案存在。 - DWright
1个回答

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图表!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接