PowerShell Excel图表 – 从现有图表获取数据系列

我有一个Powershell脚本,从一个工作簿复制工作表(具有自定义双轴图表)到另一个工作簿,然后用数据填充新的副本。 脚本的这部分工作正常,但我正在尝试更改现有图表中的数据系列,我不知道更改数据系列的字段。

我可以更改现有图表的图表标题和图例标签,没有任何问题。 我已经尝试$ChartTemplate.SeriesCollection(1).Values字段和$ChartTemplate.SeriesCollection(1).XValues$ChartTemplate.SeriesCollection().NewSeries.Invoke()命令和我没有成功。

有没有人知道Powershell的语法来编辑自定义双轴折线图( =SERIES(Template!$G$1,Template!$A$2:$A$112,Template!$G$2:$G$112,4 )?

以下是我从Googlesearch获得的Powershell代码:

 $file1 = $global:ChartTemplateXlsx # source's fullpath $file2 = $Path # destination's fullpath $xl = new-object -c excel.application $xl.Visible = $False # dont display the spreadsheet $xl.displayAlerts = $false # don't prompt the user $wb1 = $xl.workbooks.open($file1, $null, $true) # open source, readonly $wb = $xl.workbooks.open($file2) # open target workbook/worksheet $sh1_wb = $wb.sheets.item(1) # 1st sheet in destination workbook $sheetToCopy = $wb1.sheets.item('Template') # source sheet to copy $sheetToCopy.copy($sh1_wb) # copy source sheet to destination workbook $ws = $wb.ActiveSheet # set the worksheet $ChartTemplate = $ws.chartobjects(1).chart # obtain the existing chart $ChartTemplate.HasTitle = $true # turn on chart title $ChartTemplate.ChartTitle.Text = "Test Chart" # set a new chart title $ChartTemplate.SeriesCollection(1).Name = "=""Test01""" $ChartTemplate.SeriesCollection(2).Name = "=""Test02""" $ChartTemplate.SeriesCollection(3).Name = "=""Test03""" $ChartTemplate.SeriesCollection(4).Name = "=""Test04""" $wb1.close($false) # close source workbook w/o saving $wb.close($true) # close and save destination workbook $xl.quit() spps -n excel 

顺便说一下,我在图表上使用了一个单独的工作簿,以便用户可以创build自己的图表模板,然后用数据填充它。

您正在寻找的属性是FormulaFormulaLocal属性。 它们似乎是彼此重复的。 如果更新一个不起作用,请尝试另一个,或者只设置两者。

 $ChartTemplate.SeriesCollection(1).Formula = '=SERIES(Template!$G$1,Template!$A$2:$A$112,Template!$G$2:$G$112,4)' $ChartTemplate.SeriesCollection(1).FormulaLocal = '=SERIES(Template!$G$1,Template!$A$2:$A$112,Template!$G$2:$G$112,4)'