是否可以将$sheets传递给BeforeSheet事件?如果是,如何进行?我已经尝试过了,但是$sheets返回空数组,但是当在BeforeWriting事件中dd $sheets时,我得到了所有的工作表。
<?php
namespace Modules\CustomerTransactionSummaries\Exports;
class AccountabilityMultiSheetExport implements WithEvents
{
use Exportable, RegistersEventListeners;
protected $dateFrom, $dateTo, $customers, $farm;
public function __construct(string $dateFrom, string $dateTo, array $customers, Model $farm)
{
$this->dateFrom = $dateFrom;
$this->dateTo = $dateTo;
$this->customers = $customers;
$this->farm = $farm;
}
public function registerEvents(): array
{
$sheets = [];
return [
BeforeWriting::class => function (BeforeWriting $event) use (&$sheets) {
$templateFile = new LocalTemporaryFile(storage_path('app/utilities/accountability.xlsx'));
$event->writer->reopen($templateFile, \Maatwebsite\Excel\Excel::XLSX);
// * Pass the $sheets to the BeforeSheet
$sheets = $event->writer->getSheetNames();
foreach ($sheets as $key => $value) {
$event->writer->getSheetByIndex($key)->export($event->getConcernable());
}
return $event->getWriter();
},
BeforeSheet::class => function (BeforeSheet $excel) use (&$sheets) {
$sheet = $excel->sheet;
$summary = new Summaries($this->dateFrom, $this->dateTo, $this->customers, $this->farm);
$summary->printExcel($sheet->getTitle(), $sheet);
},
];
}
}
1条答案
按热度按时间czq61nw11#
不需要传递变量,因为您可以从这两个事件访问基础
Spreadsheet
类。此外,事件函数是闭包,它们的结果不会被使用。(本质上它们是
void
),所以return语句不应该做任何事情。您还应该能够从
BeforeSheet
事件访问PhpOffice\PhpSpreadsheet\Spreadsheet
类。laravel-excel文档简要介绍了这一点:
| 有效载荷| Payload |
| --| ------------ |
|
$event->writer
:Maatwebsite\Excel\Writer
|$event->writer
:Maatwebsite\Excel\Writer
||
$event->writer
:Maatwebsite\Excel\Writer
|$event->writer
:Maatwebsite\Excel\Writer
||
$event->sheet
:|Maatwebsite\Excel\Sheet
Maatwebsite\Excel\Sheet
||
$event->sheet
:|Maatwebsite\Excel\Sheet
Maatwebsite\Excel\Sheet
|这两个类
Maatwebsite\Excel\Sheet
和Maatwebsite\Excel\Writer
使用traitMaatwebsite\Excel\DelegatedMacroable
来 * 将 * 函数调用委托给它们的底层phpspreadsheet类。Writer
的委托类是PhpOffice\PhpSpreadsheet\Spreadsheet
。Sheet
的委托类是PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
。碰巧,
PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
类有一个方法(getParent()
),它返回底层的PhpOffice\PhpSpreadsheet\Spreadsheet
类。