【发布时间】:2015-09-30 21:03:27
【问题描述】:
我有一个 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) 吗?
以下是我通过谷歌搜索获得的 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
顺便说一句,我在图表中使用了一个单独的工作簿,以便用户可以创建自己的图表模板,然后我将用数据填充它。
【问题讨论】:
标签: excel vba powershell charts