NativeExcel 3.1在读取空单元格时的一个bug及修改
考虑如下情况:
在Excel某单元格内有如下公式
=IF(A3<>"", B3, C3)
这个公式在Excel内运行无误,但NativeExcel处理时无论A3是否是空,都只能得到B3的值。
经检查源码,发现问题出在单元xlscalc.pas中的TXLSCalculator.CompareVariants方法中。
该方法有如下代码:
……
isnull1 := VarIsNull(value1);
isnull2 := VarIsNull(Value2);
if isnull1 and isnull2 then
begin
Result := 0;
end
else
begin
vType1 := GetRetValueType(value1);
vType2 := GetRetValueType(Value2);
//问题出在下面这几句:强制转换了Variant变量的类型,却
//没有更新vType1或vType2,导致后面判断类型时不相等。
if isnull1 and not(isnull2) then
ConvertValue(value1, vType2)
else if isnull2 and not(isnull1) then
ConvertValue(Value2, vType1);
if vType1 = vType2 then
begin
……
解决方法也很简单,补上更新vType变量的语句即可:
if isnull1 and not(isnull2) then
begin
ConvertValue(value1, vType2);
vType1 := GetRetValueType(value1); //更新vType1
end
else if isnull2 and not(isnull1) then
begin
ConvertValue(Value2, vType1);
vtype2 := getretvaluetype(value2); //更新vType2
end;
至此,重新编译,问题解决了。