将列单元格值设置为动态范围 Maatwebsite/Excel 2.1.0 中的公式
Set column cell value as formula in dynamic range Maatwebsite/Excel 2.1.0
我在这里被难住了,希望有人能给出答案或者能给我指明正确的方向!
使用:Laravel 5.7 | Maatwebsite/Excel 2.1.0
我想要完成的事情:
我想在导出时跨动态行数将一个基本公式插入到列中。我知道我可以使用
$sheet->setCellValue('H2','=SUM(G2*F2)');
设置特定的单元格值。我假设答案是somewhere here
$sheet->cells('H2:H'.$numRows, function($cells) {
// manipulate the range of cells
});
但我们都知道当你假设某事时会发生什么。有没有人对此有任何见解?提前致谢!
您可以遍历找到的范围内的每个单元格,并将其值设置为您想要的公式。
$sheet->cells('H2:H'.$numRows, function($cells) {
foreach($cells as $cell) {
$cell->setValue('=SUM(G2*F2)');
}
});
或者,如果您根据 Maatwebsite 使用的 PhpSpreadSheet 文档为范围内的某些单元格设置了自定义公式,您可以查看如何从数组设置单元格范围 here。
因此对于您的情况,您可以构建一个一维数组并将其作为这样的列放置:
$rowArray = [];
for($i = 0; $i <= $numOfRows; $i++) {
$rowArray[] = '=SUM(G2*F2)'; // customize the formula if needed for specific row
}
$columnArray = array_chunk($rowArray, 1);
$sheet->fromArray(
$columnArray, // The data to set
NULL, // Array values with this value will not be set
'H2' // Top left coordinate of the worksheet range where
// we want to set these values (default is A1)
);
这可能对您有所帮助
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
Class ExportData implements FromCollection, WithHeadings, ShouldAutoSize, WithEvents
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$event->sheet->setCellValue('E'. ($event->sheet->getHighestRow()+1), '=SUM(E2:E'.$event->sheet->getHighestRow().')');
}
];
}
...
"Your code to export"
...
"Note:
1.in my case the data I want to get the SUM is in "E" Column you can change it as you want
2.You only need to implement "WithEvents" and use "AfterSheet" other things that I implemented is for other purposes
"
我在这里被难住了,希望有人能给出答案或者能给我指明正确的方向!
使用:Laravel 5.7 | Maatwebsite/Excel 2.1.0
我想要完成的事情:
我想在导出时跨动态行数将一个基本公式插入到列中。我知道我可以使用
$sheet->setCellValue('H2','=SUM(G2*F2)');
设置特定的单元格值。我假设答案是somewhere here
$sheet->cells('H2:H'.$numRows, function($cells) {
// manipulate the range of cells
});
但我们都知道当你假设某事时会发生什么。有没有人对此有任何见解?提前致谢!
您可以遍历找到的范围内的每个单元格,并将其值设置为您想要的公式。
$sheet->cells('H2:H'.$numRows, function($cells) {
foreach($cells as $cell) {
$cell->setValue('=SUM(G2*F2)');
}
});
或者,如果您根据 Maatwebsite 使用的 PhpSpreadSheet 文档为范围内的某些单元格设置了自定义公式,您可以查看如何从数组设置单元格范围 here。
因此对于您的情况,您可以构建一个一维数组并将其作为这样的列放置:
$rowArray = [];
for($i = 0; $i <= $numOfRows; $i++) {
$rowArray[] = '=SUM(G2*F2)'; // customize the formula if needed for specific row
}
$columnArray = array_chunk($rowArray, 1);
$sheet->fromArray(
$columnArray, // The data to set
NULL, // Array values with this value will not be set
'H2' // Top left coordinate of the worksheet range where
// we want to set these values (default is A1)
);
这可能对您有所帮助
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
Class ExportData implements FromCollection, WithHeadings, ShouldAutoSize, WithEvents
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$event->sheet->setCellValue('E'. ($event->sheet->getHighestRow()+1), '=SUM(E2:E'.$event->sheet->getHighestRow().')');
}
];
}
...
"Your code to export"
...
"Note:
1.in my case the data I want to get the SUM is in "E" Column you can change it as you want
2.You only need to implement "WithEvents" and use "AfterSheet" other things that I implemented is for other purposes
"