It's possible to access $sheet in Import class of laravel-excel or get cell style from PHPOffice/PhpSpreadsheet

huangapple go评论96阅读模式
英文:

It's possible to access $sheet in Import class of laravel-excel or get cell style from PHPOffice/PhpSpreadsheet

问题

在导入Excel并使用toCollection方法时,我们需要设置行或单元格的填充颜色。您无法确定如何访问$sheet以获取单元格的填充颜色或其他属性。

无论如何,您可以尝试使用beforeImport事件来访问PHPOffice/PhpSpreadsheet的$currentWorkSheet以了解单元格的颜色,如下所示:

public static function beforeImport(BeforeImport $event)
{
    for ($i = 1; $i < 10; $i++) {
        \Log::info(json_encode($event->getDelegate()->getActiveSheet()->getCell("A$i")->getValue()));
        \Log::info(json_encode($event->getDelegate()->getActiveSheet()->getStyle("A$i")->getFill()->exportArray()));
    }
}

但是对于类似这样的列,您得到的日志显示填充颜色完全为空:

"AAAA"
{"fillType":"none","rotation":0}
"a2"
{"fillType":"none","rotation":0}
"a3"
{"fillType":"none","rotation":0}
"a4"
{"fillType":"none","rotation":0}
"a5"
{"fillType":"none","rotation":0}
"a6"
{"fillType":"none","rotation":0}
"a7"
{"fillType":"none","rotation":0}
"a8"
{"fillType":"none","rotation":0}
"a9"
{"fillType":"none","rotation":0}

有人能给我一些关于为什么PHPOffice无法读取单元格属性的想法吗?

英文:

Imagine we are importing a excel, and using toCollection method.

And we need fill color of row or cell we using.

I can't figure how to access $sheet to get fill color or other properties of cell.

Anyway, i tried to use beforeImport event to access $currentWorkSheet of PHPOffice/PhpSpreadsheet to know what is color of cell

public static function beforeImport(BeforeImport $event)
    {
        for ($i = 1; $i &lt; 10; $i++) {
            \Log::info(json_encode($event-&gt;getDelegate()-&gt;getActiveSheet()-&gt;getCell(&quot;A$i&quot;)-&gt;getValue()));
            \Log::info(json_encode($event-&gt;getDelegate()-&gt;getActiveSheet()-&gt;getStyle(&quot;A$i&quot;)-&gt;getFill()-&gt;exportArray()));
        }
    }

But for a column like this

It's possible to access $sheet in Import class of laravel-excel or get cell style from PHPOffice/PhpSpreadsheet

I get this log that showing fill color is empty at all...

&quot;AAAA&quot;
{&quot;fillType&quot;:&quot;none&quot;,&quot;rotation&quot;:0}
&quot;a2&quot;
{&quot;fillType&quot;:&quot;none&quot;,&quot;rotation&quot;:0}
&quot;a3&quot;
{&quot;fillType&quot;:&quot;none&quot;,&quot;rotation&quot;:0}
&quot;a4&quot;
{&quot;fillType&quot;:&quot;none&quot;,&quot;rotation&quot;:0}
&quot;a5&quot;
{&quot;fillType&quot;:&quot;none&quot;,&quot;rotation&quot;:0}
&quot;a6&quot;
{&quot;fillType&quot;:&quot;none&quot;,&quot;rotation&quot;:0}
&quot;a7&quot;
{&quot;fillType&quot;:&quot;none&quot;,&quot;rotation&quot;:0}
&quot;a8&quot;
{&quot;fillType&quot;:&quot;none&quot;,&quot;rotation&quot;:0}
&quot;a9&quot;
{&quot;fillType&quot;:&quot;none&quot;,&quot;rotation&quot;:0}

Can anyone give me some idea why PHPOffice can't read cell properties?

答案1

得分: 0

关于格式化单元格 部分,你可以按如下方式导出样式为一个数组:

$styleArray = $spreadsheet->getActiveSheet()->getStyle('A3')->exportArray();

而不是

$styleArray = $spreadsheet->getActiveSheet()->getStyle('A3')->getFill()->exportArray();

你可以尝试一下,希望能帮助到你!

英文:

Referring to Formatting cells section, you can export style as an array as follows:

$styleArray = $spreadsheet-&gt;getActiveSheet()-&gt;getStyle(&#39;A3&#39;)-&gt;exportArray();

Rather than

$styleArray = $spreadsheet-&gt;getActiveSheet()-&gt;getStyle(&#39;A3&#39;)-&gt;getFill()-&gt;exportArray();

You could give it a try, hope it helps!

huangapple
  • 本文由 发表于 2023年2月6日 16:10:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定