gpt4 book ai didi

excel - Powershell从EXCEL创建折线图

转载 作者:行者123 更新时间:2023-12-03 00:29:52 28 4
gpt4 key购买 nike

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

enter image description here

我正在尝试创建一个类似于

enter image description here

但是,这是我到目前为止的代码:

$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

这是它生成的图表

enter image description here

我什至如何开始解决这个问题?有什么有用的教程吗?

最佳答案

Degustaf 是 100% 正确的,他的答案是正确的,但是你有很多多余的东西是不需要的。我可以复制您想要的结果,包括用您的测试数据填充电子表格,并以比您拥有的更少的行数来完成。在这里,检查一下,您可能会为您 future 的努力提供一些建议。

#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...非常有用。

关于excel - Powershell从EXCEL创建折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25331407/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com