电子表格中的所有工作表似乎都是对工作表0的引用

问题描述 投票:0回答:2

我正在编写一个从Excel电子表格读取的价格导入脚本。

该电子表格是使用Office 365 Excel生成的,但是我正在Ubuntu 18.04上使用LibreOffice Calc在开发过程中查看它-此处没有问题。

我正在使用phpoffice/phpspreadsheet版本的1.10.1

        "name": "phpoffice/phpspreadsheet",
        "version": "1.10.1",
        "source": {
            "type": "git",
            "url": "https://github.com/PHPOffice/PhpSpreadsheet.git",
            "reference": "1648dc9ebef6ebe0c5a172e16cf66732918416e0"
        },

我正在尝试将电子表格中每个工作表的数据转换为数组。

有3个工作表,每个工作表代表“区域”-区域1,区域2和区域3。

我似乎在区域2和区域3中获得与区域1相同的数据-正确返回了工作表标题,但是工作表之间的数据没有改变。

   /**
     * @param Spreadsheet $spreadsheet
     *
     * @return array
     */
    private function parseZones(Spreadsheet $spreadsheet): array
    {
        $zones = [];

        foreach ([0, 1, 2] as $sheetIndex) {
            $sheet = $spreadsheet->getSheet($sheetIndex);
            // this is correctly reporting 'Zone 1', 'Zone 2' and 'Zone 3' - sheet title is accurate
            $sheetName = $sheet->getTitle();

            // sheet 0 is accurate
            $sheetData = $sheet->toArray();

            // on sheet index 1 and 2 - $sheetData is identical to that of sheet index 0
            // the XLSX file in OpenOffice / Excel has distinctly different row data - 50% less rows in both cases
            // feels like a memory cache issue / some mis-referencing?
        }

        // retrieving rows using this approach yields the same result:
        foreach ($spreadsheet->getAllSheets() as $sheet) {
            // this is correctly reporting 'Zone 1', 'Zone 2' and 'Zone 3' - sheet title is accurate
            $sheetName = $sheet->getTitle();
            // on sheet index 1 and 2 - $sheetData is identical to that of sheet index 0
            $sheetData = $sheet->toArray();
        }

        return $zones;
    }

有什么想法吗?谢谢

phpexcel phpoffice
2个回答
0
投票

我很傻-完全无法查看/检查电子表格中的行过滤。

正在返回正确的数据。

无问题,对不起!

enter image description here

此后,我开始研究如何在遵循电子表格中嵌入的过滤器的同时阅读工作表,并且看来Worksheet::toArray()不会自动考虑过滤器-也不手动迭代列和行,请参阅:

https://phpspreadsheet.readthedocs.io/en/latest/topics/autofilters/

enter image description here

您必须根据文档手动测试行的可见性设置。

希望这会有所帮助!


0
投票

尝试读取前仅更改当前活动表。

$spreadsheet->setActiveSheetIndex($sheetIndex);
$sheet = $spreadsheet->getActiveSheet();

$dataArray = $sheet
 ->rangeToArray(
     'A4:O07',    // The worksheet range that we want to retrieve
     NULL,        // Value that should be returned for empty cells
     TRUE,        // Should formulas be calculated (the equivalent of getCalculatedValue() for each cell)
     TRUE,        // Should values be formatted (the equivalent of getFormattedValue() for each cell)
     TRUE         // Should the array be indexed by cell row and cell column
 );

PhpSpreadsheet

© www.soinside.com 2019 - 2024. All rights reserved.