Excel更改图表公式
我目前正在使用 Office 对象模型生成 Excel 文档。我在编辑图表时遇到问题。 在模板文件中,我得到了一个使用以下来源的条形图:
2008 2009 2010
A 10% 25% 15%
B 20% 25% 35%
C 30% 25% 45%
D 40% 25% 5%
该图表具有以下公式: =sheet2!$A$1:$D$5
例如,当“2009”列为空时,我不想在图表中显示该条形。所以我想把公式改成这样: =sheet2!A$1:D$5;sheet2!C$1:C$5
我知道有一个 setSourceData 方法,但我需要先获取当前公式或范围。
我的问题是;如何获得图表公式?或者也许还有另一种方法可以做我想做的事?
我还在 Excel 中尝试了动态范围,但这似乎只适用于从范围末尾添加或删除的列,而不是像列“2009”那样位于中间的列。
I'm currently generating Excel-documents using the Office Object Model. I have a problem with editing charts.
In a template file I got a bar-chart which uses the following source:
2008 2009 2010
A 10% 25% 15%
B 20% 25% 35%
C 30% 25% 45%
D 40% 25% 5%
The chart has the following formula:
=sheet2!$A$1:$D$5
When for example the column '2009' is empty, I don't want to show the bar in the chart. So I want to change the formula to something like:
=sheet2!A$1:D$5;sheet2!C$1:C$5
I know there is a method setSourceData, but I need to get the current formula or range first.
My question is; How can I get the chart formula? Or maybe there is another way to do what I want?
I also tried something with dynamic ranges in Excel, but this seems to only work with columns that are added or removed from the end of the range, not in the middle like column '2009'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我编写了以下代码来解决我的问题。它重建了所有现有的系列公式。这并不适用于所有可能的图表,但适用于我目前拥有的图表。将来我可能会再次查看并尝试改进它。欢迎对下面的代码提出建议。
(抱歉缺少代码注释)
I made the following code to solve my problem. It rebuilds all the existings series formulas. This won't work for all possible charts, but it does for the ones I currently have. In the future I'll probably look at it again and try to improve it. Suggestions to the code below are welcome.
(sorry for the lack of code comments)
没有一个属性可以保存图表的完整数据范围。然而,每个系列都包含有关其范围的信息。
下面的代码将列出所有系列,然后删除第二个系列。
根据您的示例数据,代码将输出此
=SERIES(Sheet1!$B$1,Sheet1!$A$2:$A$5,Sheet1!$B$2:$B$5,1)
=系列(表1!$C$1,表1!$A$2:$A$5,表1!$C$2:$C$5,2)
=SERIES(Sheet1!$D$1,Sheet1!$A$2:$A$5,Sheet1!$D$2:$D$5,3)
Series 由四个参数组成:(
系列名称、XValues、值、绘图顺序)
There isn't a property that holds the full data range of a chart. However each Series holds information about it's range.
The code below will list all series and then delete the second one.
Based on your example data, the code will output this
=SERIES(Sheet1!$B$1,Sheet1!$A$2:$A$5,Sheet1!$B$2:$B$5,1)
=SERIES(Sheet1!$C$1,Sheet1!$A$2:$A$5,Sheet1!$C$2:$C$5,2)
=SERIES(Sheet1!$D$1,Sheet1!$A$2:$A$5,Sheet1!$D$2:$D$5,3)
The Series is made up from four arguments:
(Series Name, XValues, Values, Plot Order)