首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby WIN32OLE excel图表系列集合值

Ruby WIN32OLE excel图表系列集合值
EN

Stack Overflow用户
提问于 2013-01-29 23:41:13
回答 1查看 379关注 0票数 1

我正在尝试更改Excel (实际上是PowerPoint)图表的值。我试着通过传递一个数组来做到这一点,但它似乎不起作用。尽管如本页所述,它应该可以工作...:http://msdn.microsoft.com/en-us/library/office/ff746833.aspx

那么现在我的代码是什么样子的呢?

代码语言:javascript
复制
require 'win32ole'
mspp_app = WIN32OLE.new("Powerpoint.Application")
mspp     = mspp_app.Presentations.Open(pathToFile)

slide         = mspp.Slides(1)
shapes        = slide.shapes
chartshape    = shapes(3) #the chart happens to be shape n°3
chart         = chartshape.chart

# now get the seriescollection
sc  = chart.SeriesCollection
sc3 = sc.Item(3)

values = sc3.values #returns the current values as an array example: [1.0, 1.0, 5.0, 2.0]

# now set the values
sc3.values = [2.0, 2.0, 5.0, 1.0] # returns no error

# see if the values are set
values = sc3.values # returns an empty Array [] 

以前有人试过这个吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-19 21:22:37

为了操作图表数据,您必须更改底层工作表:

代码语言:javascript
复制
ws = myChart.Chart.ChartData.Workbook.Worksheets(1)

ws.Range("A2").Value = "Coffee"
ws.Range("A3").Value = "Soda"
ws.Range("A4").Value = "Tea"
ws.Range("A5").Value = "Water"
ws.Range("B1").Value = "Amount"  # used as label for legend
ws.Range("B2").Value = "1000"
ws.Range("B3").Value = "2500"
ws.Range("B4").Value = "4000"
ws.Range("B5").Value = "3000"

如果维度已更改,则更改SourceData-Range非常重要。注意从Excel中知道的不同概念:"=Tabelle1!A1:B5“而不是"A1:B5”。

对于英文办公版本,请将"Tabelle1“更改为"Sheet1”

代码语言:javascript
复制
myChart.Chart.SetSourceData("=Tabelle1!A1:B5")
myChart.Chart.ChartData.Workbook.Close

之后别忘了关闭工作表。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14586645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档