【发布时间】:2021-01-14 10:19:54
【问题描述】:
我有一个包含多个工作表的 Excel 表,我想获取当前活动的工作表的名称
【问题讨论】:
我有一个包含多个工作表的 Excel 表,我想获取当前活动的工作表的名称
【问题讨论】:
我认为$event->getSheet()->getTitle() 应该可以。您也可以使用事件。
class HImport1 implements ToCollection, WithHeadingRow, WithEvents
{
public $sheetNames;
public $sheetData;
public function __construct(){
$this->sheetNames = [];
$this->sheetData = [];
}
public function collection(Collection $collection)
{
$this->sheetData[] = $collection;
}
public function registerEvents(): array
{
return [
BeforeSheet::class => function(BeforeSheet $event) {
$this->sheetNames[] = $event->getSheet()->getTitle();
}
];
}
}
【讨论】:
在 3.1 版中,API 略有变化。您必须先致电getDelegate(),然后才能获得标题。以@Satendra Rawat 回答为基础...
public function registerEvents(): array
{
return [
BeforeSheet::class => function (BeforeSheet $event) {
$this->sheetNames[] = $event->getSheet()->getDelegate()->getTitle();
}
];
}
【讨论】: