Range.Formula 返回一个使用单元格引用的字符串,为什么?
我在 Range.Formula
方面遇到问题。代码片段如下(假设 Range 对象已创建且不为空)
string formula;
formula = Range.Formula;//it returns whatever is written in the formula, even the cell reference
假设我有这样一个公式 =MyCalc(41, A1)
,它使用相对单元格引用。在单元格 A1 中,有一个值 100。
因此字符串变量 formula
的值始终为 =MyCalc(41, A1)
而不是 =MyCalc (41, 100)
这就是我想要的结果。
我想知道是否有办法让 Range.Formula 避免返回单元格引用。有什么想法吗?谢谢
I am having a problem with Range.Formula
. The code snippet is below (assume Range object has been created and is not null)
string formula;
formula = Range.Formula;//it returns whatever is written in the formula, even the cell reference
Let's say I have such a formula, =MyCalc(41, A1)
, that uses the relative cell reference. And in cell A1, there's a value, 100.
So the value of the string variable formula
, is always =MyCalc(41, A1)
instead of =MyCalc(41, 100)
which is the result I wanted.
I wonder if there's a way to let Range.Formula avoid returning cell reference. Any ideas? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
公式的全部要点是准确地为您提供其中输入的内容。您可以编写一个方法,该方法通过公式并递归地获取该单元格范围的值。
不为您完成此操作的原因是它可能会导致大量递归。如果单元格 A1(在您的示例中)actaull 引用单元格 B2,而单元格 B2 引用 C100...等等。
我会编写一个函数,它接受单元格引用并获取值(getCellValue)(而不是公式)。然后,我将编写一个获取公式的函数,使用正则表达式检查单元格引用,并对所有引用调用 getCellValue。
The whole point of the formula is to give you exactly what was typed in there. You could write a method which goes through a formula and recursively gets the values for that cell range.
The reason why this is not done for you is it could incur a lot of recursion. What if Cell A1 (in your example) actaull references cell B2, which references C100... and so on.
I would write a function that takes a cell reference and gets the VALUE (getCellValue) (not formula). Then I would write a function that gets the Formula, check for cell references using a regex, and call the getCellValue on all the references.