簡體   English   中英

Ruby WIN32OLE Excel圖表系列集合值

[英]Ruby WIN32OLE excel chart seriescollection values

我正在嘗試更改Excel(實際上是PowerPoint)圖表的值。 我嘗試通過傳遞數組來執行此操作,但它似乎不起作用。 盡管如本頁面所述,它應該可以工作...: http : //msdn.microsoft.com/zh-cn/library/office/ff746833.aspx

所以我的代碼現在看起來如何:

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

有人嘗試過嗎?

要處理圖數據,您必須更改基礎工作表:

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”

myChart.Chart.SetSourceData("=Tabelle1!A1:B5")
myChart.Chart.ChartData.Workbook.Close

不要忘了之后關閉工作表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM