简体   繁体   English

间歇性的“ InteropServices.COMException / ForwardCallToInvokeMember”在范围单元格上访问Value2

[英]Intermittent “InteropServices.COMException / ForwardCallToInvokeMember” accessing Value2 on Range cell

In my last question: ExcelDNA throwing exception accessing Range.Value2 在我的最后一个问题中: ExcelDNA引发访问Range.Value2的异常

I blamed ExcelDNA for the reason why Value2 was throwing a COMException. 我将Excel2归咎于Value2引发COMException的原因。 But I'm not sure this is the case any longer. 但是我不确定情况是否会如此。

I disables the IsMacroType flag which completely stops the COMException from happening and noticed that sometimes Range.Value2 doesn't throw an exception at all. 我禁用了IsMacroType标志,该标志完全阻止了COMException的发生,并注意到有时Range.Value2根本不会引发异常。

So sometimes it works, and sometimes it doesn't. 因此有时它起作用,有时却不起作用。 My question is, what would cause range.Value2 to throw intermittent COMExceptions? 我的问题是,什么会导致range.Value2引发间歇性的COMException?

It's annoying because the stacktrace gives me no useful information, and IsMacroType fixes the problem entirely. 这很烦人,因为stacktrace没有提供有用的信息,而IsMacroType完全解决了该问题。

My suspicion is if a cell is constantly changing, by the time Value2 is accessed the cell might get invalidated, but it's a guess and I'm not sure how excel works. 我的怀疑是,如果单元格在不断变化,那么在访问Value2时,该单元格可能会失效,但这只是一个猜测,我不确定excel的工作方式。

But also, it doesn't make sense as there aren't multiple threads in the code. 但是,这也没有意义,因为代码中没有多个线程。

Have you encountered this problem? 你遇到这个问题了吗?

This is the code: 这是代码:

var valueCell = (Range)row.Cells[1, 30];
if (valueCell .Value2 != null)
{
     //do something
}

Seriously, Value2 fails to evaluate on the if statement 严重地,Value2无法评估if语句

I'm a bit surprised it ever works without IsMacroType=true . 我有点惊讶,如果没有IsMacroType=true它就可以工作。 It might depend on whether the cell has been calculated or not. 这可能取决于该单元格是否已计算。

Excel normally prevents a UDF from reading other parts of the sheet, unless the UDF is registered as a "macro sheet equivalent" (with a # in the registration string). Excel通常会阻止UDF读取工作表的其他部分,除非UDF被注册为“等效的宏工作表”(在注册字符串中带有#)。

Functions that are registered as "macro sheet equivalents" have the following behaviour (see towards the bottom of the documentation from xlfRegister ): 被注册为“等效宏”的函数具有以下行为(请参阅xlfRegister的文档底部):

Placing a # character after the last parameter code in pxTypeText gives the function the same calling permissions as functions on a macro sheet. 在pxTypeText中的最后一个参数代码之后放置#字符,可使该函数具有与宏工作表上的函数相同的调用权限。 These are as follows: 这些如下:

  • The function can retrieve the values of cells that have not yet been calculated in this recalculation cycle. 该函数可以检索在此重新计算循环中尚未计算的单元格的值。

  • The function can call any of the XLM information (Class 2) functions, for example, xlfGetCell. 该函数可以调用任何XLM信息(第2类)函数,例如xlfGetCell。

  • If the number sign (#) is not present: evaluating an uncalculated cell results in an xlretUncalced error, and the current function is called again once the cell has been calculated; 如果不存在数字符号(#):评估未计算的像元将导致xlretUncalced错误,并且一旦计算了像元,将再次调用当前函数;否则,将返回当前函数。 calling any XLM information function other than xlfCaller results in an xlretInvXlfn error. 调用xlfCaller以外的任何XLM信息函数都会导致xlretInvXlfn错误。

Your error in the case without IsMacroType=true might be the last one of these - you're reading an uncalculated cell hence getting an error. 在没有IsMacroType=true的情况下,您的错误可能是其中的最后一个-您正在读取未计算的单元格,因此出现错误。

The side-effects of setting IsMacroType=true for a UDF is not entirely clear. 对于UDF设置IsMacroType=true的副作用并不完全清楚。 One effect is that functions registered as IsMacroType=true and having a parameter that is marked as AllowReference=true will automatically be considered volatile (even if registered with IsVolatile=false ). 一种效果是,注册为IsMacroType=true并具有标记为AllowReference=true的参数的函数将自动被视为易失性的(即使已注册为IsVolatile=false )。 Another side effect is that the recalculation sequence is affected - particularly if you are reading uncalculated cells from inside your UDF. 另一个副作用是重新计算顺序会受到影响-尤其是当您从UDF内部读取未计算的单元格时。

You also have to be really careful in reading other cells from a UDF, regarding your expectation of what should recalculate when, since you are kind of undermining the calculation dependency tree. 您还必须非常谨慎地从UDF读取其他单元格,因为您对何时应该重新计算的期望值很高,因为这有损于计算依赖性树。 In your example your UDF is reading cell A30 , but will changes to cell A30 automatically cause your function to recalculate? 在您的示例中,UDF正在读取单元格A30 ,但是更改为单元格A30会自动导致您的函数重新计算吗? Certainly you can't use the Excel dependency tracking tools to understand that your cell depends on A30 . 当然,您不能使用Excel依赖关系跟踪工具来了解您的单元格取决于A30 Really you rather want to have a function that takes the explicit parameter, and is called as =DoSomething(A30) making everything clear and avoiding all these problems. 确实,您实际上想拥有一个带有显式参数的函数,并称为=DoSomething(A30)使所有内容清晰并避免所有这些问题。

One reason you might be trying to read Value2 is to determine the formatting of the cell instead of the underlying value that Excel stores, but that's really dangerous since it is not part of the recalculation and dependency tree. 您可能试图读取Value2是确定单元格的格式,而不是确定Excel存储的基础值,但这确实很危险,因为它不是重新计算和依赖关系树的一部分。

So I would say the fact that you are seeing some unexpected behaviour is a sign that you are going in a direction that Excel does not like. 因此,我想说您看到一些意外行为的事实表明您正在朝Excel不喜欢的方向前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么使用此代码会得到“ InteropServices.COMException / ForwardCallToInvokeMember”? - Why would I get, “InteropServices.COMException / ForwardCallToInvokeMember” with this code? InteropServices.COMException - InteropServices.COMException 将项目添加到 ObservableCollection 时出现 InteropServices.COMException - InteropServices.COMException when adding item to ObservableCollection C# AutoIt InteropServices.COMException 错误 - C# AutoIt InteropServices.COMException error com 异常 excel InteropServices.COMException - c# - com exception excel InteropServices.COMException - c# 如何在不安装Excel的情况下修复InteropServices.COMException? - How can I fix InteropServices.COMException without installing Excel? 从ASP MVC控制器运行Word时保持获取InteropServices.COMException - Keep Getting InteropServices.COMException when running Word from ASP MVC Controller InteropServices.COMException(0x800A1066):在Teamcity下命令失败 - InteropServices.COMException (0x800A1066): Command failed under Teamcity 为什么我在尝试从 C# 启动 QTP 时收到 InteropServices.COMException? - Why do I get an InteropServices.COMException when attempting to launch QTP from C#? 从页面调用静态异步方法导致 InteropServices.COMException 错误 - Calling a static async method from a Page results in InteropServices.COMException error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM