viest/php-ext-xlswriter

Worksheet row or column index out of range & mergeCells

Closed this issue · 1 comments

环境

$ php -v
PHP 8.1.8 (cli) (built: Jul 14 2022 14:51:59) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.8, Copyright (c) Zend Technologies

$ php --ri xlswriter

xlswriter

xlswriter support => enabled
Version => 1.5.4
bundled libxlsxwriter version => 1.1.3
bundled libxlsxio version => 0.2.27

示例一

$excel = new \Vtiful\Kernel\Excel(['path' => './runtime']);

$excel->constMemory("test.xlsx", null, false);

$headerDefaultFormat = new \Vtiful\Kernel\Format($excel->getHandle());
$headerDefaultStyle = $headerDefaultFormat->fontColor(\Vtiful\Kernel\Format::COLOR_BLACK)
                                          ->border(\Vtiful\Kernel\Format::BORDER_THIN)
                                          ->align(\Vtiful\Kernel\Format::FORMAT_ALIGN_CENTER, \Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_CENTER)
                                          ->background(0x4ac1ff)
                                          ->toResource();
//$excel->mergeCells('A1:B3', 'Merge cells', $headerDefaultStyle);//横向合并正确
$excel->mergeCells('A1:A3', 'Merge cells', $headerDefaultStyle);//内存模式下:纵向合并报错  非内存模式正确
$excel->output();
内存模式下纵向合并报错:
PHP Fatal error:  Uncaught Vtiful\Kernel\Exception: Worksheet row or column index out of range. in 
~/xlswriter/excel.php:233
Stack trace:
#0 ~/xlswriter/excel.php(233): Vtiful\Kernel\Excel->mergeCells('A1:A3', 'Merge cells', Resource id #5)
#1 {main}
  thrown in ~/xlswriter/excel.php on line 233

示例二

背景:自定义多级表头
$excel = new \Vtiful\Kernel\Excel(['path' => './runtime']);

$excel->fileName("test.xlsx");

$headerDefaultFormat = new \Vtiful\Kernel\Format($excel->getHandle());
$test = clone $headerDefaultFormat;
$headerDefaultStyle = $headerDefaultFormat->fontColor(\Vtiful\Kernel\Format::COLOR_BLACK)
                                          ->border(\Vtiful\Kernel\Format::BORDER_THIN)
                                          ->align(\Vtiful\Kernel\Format::FORMAT_ALIGN_CENTER, \Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_CENTER)
                                          ->background(0x4ac1ff)
                                          ->toResource();
$excel->mergeCells('A1:A2', 'Merge cells', $headerDefaultStyle);//纵向合并两行
$excel->mergeCells('B1:C1', 'parent', $headerDefaultStyle);//横向合并两列
$excel->insertText(2,1, 'c1');//这里有疑问
$excel->insertText(2,2, 'c2');
$excel->output();
结果如下:合并后使用insertText插入末级表头,行号错位

image

//继续使用合并填充值
$excel->mergeCells('B2:B2', 'c1', $headerDefaultStyle);
$excel->mergeCells('C2:C2', 'c2', $headerDefaultStyle);

image

viest commented

Memory mode is row-by-row flushing, so merging cells above and below has already exceeded the current row.