Powershell从EXCEL创建折线图

Powershell从EXCEL创建折线图,excel,powershell,Excel,Powershell,我的powershell脚本将数据输入EXCEL工作表 我正在尝试创建一个类似的折线图 然而,这是我目前掌握的代码: $xlConditionValues=[Microsoft.Office.Interop.Excel.XLConditionValueTypes] $xlTheme=[Microsoft.Office.Interop.Excel.XLThemeColor] $xlChart=[Microsoft.Office.Interop.Excel.XLChartType] $xlIconS

我的powershell脚本将数据输入EXCEL工作表

我正在尝试创建一个类似的折线图

然而,这是我目前掌握的代码:

$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]

...

$chart=$ws.Shapes.AddChart().Chart
$chart.chartType=$xlChart::xlBarClustered

$start=$ws.range("A1")

$Y=$ws.Range($start,$start.End($xlDirection::xlDown))

$start=$ws.range("B2")

$X=$ws.Range($start,$start.End($xlDirection::xlDown))

$chartdata=$ws.Range("A$($Y.item(1).Row):A$($Y.item($Y.count).Row),B$($X.item(1).Row):B$($X.item($X.count).Row)")
$chart.SetSourceData($chartdata)

#add labels
$chart.seriesCollection(1).Select() | Out-Null
$chart.SeriesCollection(1).ApplyDataLabels() | out-Null

#modify the chart title
$chart.ChartTitle.Text = "Number of Computer"


$ws.shapes.item("Chart 1").top=40
这是它生成的图形


我该如何着手解决这个问题?有什么有用的教程吗?

根据我使用VBA的经验,似乎首先要尝试的是更改图表类型。您有$chart.chartType=$xlChart::XLBAR。基于类似的VBA命令,我将尝试将其更改为$chart.chartType=$xlChart::xlLine。这应该会有很大的不同,让你看看还有什么需要调整。

根据我使用VBA的经验,似乎第一件要尝试的事情就是更改图表类型。您有$chart.chartType=$xlChart::XLBAR。基于类似的VBA命令,我将尝试将其更改为$chart.chartType=$xlChart::xlLine。这应该会有很大的不同,让你看看还有什么需要调整。

Degustaf是100%正确的,他的答案是正确的,但你有很多额外的东西在那里,不需要。我可以复制您想要的结果,包括用您的测试数据填充电子表格,并且比您拥有的更少的行数。在这里,看看这个,你可能会为你未来的努力带来一些建议

#Test Data
$Data=("8/15/2014",3091),("8/14/2014",240),("8/13/2014",519),("8/12/2014",622),("8/11/2014",2132),("8/10/2014",1255),("8/9/2014",3240)|ForEach{[PSCustomObject][Ordered]@{'Date_to_Display'=$_[0];'Number_of_Computers'=$_[1]}}

$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]

$xl = new-object -ComObject Excel.Application
$wb = $xl.workbooks.add()
$ws = $wb.activesheet
$xl.Visible = $true

#Populate test data onto worksheet
$Data |ConvertTo-CSV -NoTypeInformation -Delimiter "`t"| c:\windows\system32\clip.exe
$ws.Range("A1").Select | Out-Null
$ws.paste()
$ws.UsedRange.Columns.item(1).numberformat = "dddd, mmm dd, yyyy"
$ws.UsedRange.Columns.AutoFit() |Out-Null

#Create Chart
$chart=$ws.Shapes.AddChart().Chart
$chart.chartType=$xlChart::xlLine

#modify the chart title
$chart.ChartTitle.Text = "Number of Computers"

$ws.shapes.item("Chart 1").top=40

如果您经常使用Powershell和Excel,您可能会在那里找到$Data | ConvertTo CSV。。。非常有用。

Degustaf是100%正确的,他的答案是正确的,但是你有很多不需要的额外东西。我可以复制您想要的结果,包括用您的测试数据填充电子表格,并且比您拥有的更少的行数。在这里,看看这个,你可能会为你未来的努力带来一些建议

#Test Data
$Data=("8/15/2014",3091),("8/14/2014",240),("8/13/2014",519),("8/12/2014",622),("8/11/2014",2132),("8/10/2014",1255),("8/9/2014",3240)|ForEach{[PSCustomObject][Ordered]@{'Date_to_Display'=$_[0];'Number_of_Computers'=$_[1]}}

$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]

$xl = new-object -ComObject Excel.Application
$wb = $xl.workbooks.add()
$ws = $wb.activesheet
$xl.Visible = $true

#Populate test data onto worksheet
$Data |ConvertTo-CSV -NoTypeInformation -Delimiter "`t"| c:\windows\system32\clip.exe
$ws.Range("A1").Select | Out-Null
$ws.paste()
$ws.UsedRange.Columns.item(1).numberformat = "dddd, mmm dd, yyyy"
$ws.UsedRange.Columns.AutoFit() |Out-Null

#Create Chart
$chart=$ws.Shapes.AddChart().Chart
$chart.chartType=$xlChart::xlLine

#modify the chart title
$chart.ChartTitle.Text = "Number of Computers"

$ws.shapes.item("Chart 1").top=40

如果您经常使用Powershell和Excel,您可能会在那里找到$Data | ConvertTo CSV。。。非常有用。

别出心裁,避免看到不必要的红线异常设置ChartType:Unspecified error over:

$chart.chartType = 4

图表类型的详细列表和此处提供的更多信息:

花哨的东西,以避免看到不必要的红线异常设置ChartType:到处都是未指定的错误:

$chart.chartType = 4

图表类型的详细列表和此处提供的更多信息:

工作表上是否有其他数据?如果不是的话,那么当您可以使用$ws.usedrange时,您正在艰难地选择范围。工作表上还有其他数据吗?如果不是这样的话,那么当我运行原始示例时,您可以使用$ws.usedrange来选择范围是非常困难的。当我添加源数据时,它在$chart.ChartTitle.Text=计算机数量上失败。样品样本中有什么问题?@构造器请不要参与线程克隆。如果你有问题,发表一个问题。如果你想的话,可以参考这个帖子,但是当我运行原始示例时,可以发布你自己的问题供人们回答和帮助。当我添加源数据时,它在$chart.ChartTitle.Text=计算机数量上失败。样品样本中有什么问题?@构造器请不要参与线程克隆。如果你有问题,发表一个问题。如果您愿意,请参考此帖子,但请发布您自己的问题,供人们回答和帮助。