PHPOffice/PhpSpreadsheet解析excel文件时,VLOOKUP函数出错

使用PHPOffice/PhpSpreadsheet包进行excel文件解析时,发现有一列数据解析出错

发现是VLOOKUP函数解析出错,

excel中设置的函数为 VLOOKUP(A5,Sheet2!$A$1:$AG$50,{12,13,12},FALSE)

跟踪代码后发现是在{12,13,12}这一段时出错,

需要修改PhpOffice\PhpSpreadsheet\Calculation\LookupRef\LookupBase.php的函数validateIndexLookup

在函数最前面加入代码

if (is_array($index_number)) {
    $index_number = $index_number[0];
}

原代码:

    protected static function validateIndexLookup(array $lookup_array, $index_number): int
    {
        // index_number must be a number greater than or equal to 1.
        // Excel results are inconsistent when index is non-numeric.
        // VLOOKUP(whatever, whatever, SQRT(-1)) yields NUM error, but
        // VLOOKUP(whatever, whatever, cellref) yields REF error
        //   when cellref is '=SQRT(-1)'. So just try our best here.
        // Similar results if string (literal yields VALUE, cellRef REF).
        if (!is_numeric($index_number)) {
            throw new Exception(ExcelError::throwError($index_number));
        }
        if ($index_number < 1) {
            throw new Exception(ExcelError::VALUE());
        }

        // index_number must be less than or equal to the number of columns in lookup_array
        if (empty($lookup_array)) {
            throw new Exception(ExcelError::REF());
        }

        return (int) $index_number;
    }

修改后代码

    protected static function validateIndexLookup(array $lookup_array, $index_number): int
    {
        if (is_array($index_number)) {
            $index_number = $index_number[0];
        }
        // index_number must be a number greater than or equal to 1.
        // Excel results are inconsistent when index is non-numeric.
        // VLOOKUP(whatever, whatever, SQRT(-1)) yields NUM error, but
        // VLOOKUP(whatever, whatever, cellref) yields REF error
        //   when cellref is '=SQRT(-1)'. So just try our best here.
        // Similar results if string (literal yields VALUE, cellRef REF).
        if (!is_numeric($index_number)) {
            throw new Exception(ExcelError::throwError($index_number));
        }
        if ($index_number < 1) {
            throw new Exception(ExcelError::VALUE());
        }

        // index_number must be less than or equal to the number of columns in lookup_array
        if (empty($lookup_array)) {
            throw new Exception(ExcelError::REF());
        }

        return (int) $index_number;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zsh0409

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值