How do I make automatic height row based on conten

2020-02-15 15:12发布

问题:

I had search reference and the reference say to try like this :

<?php   
...
class ReportExport implements ShouldAutoSize, FromView, WithColumnFormatting, WithEvents
{
    ...
    public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function(AfterSheet $event) {
                ...
                $event->sheet->getDelegate()->getRowDimension(37)->setRowHeight(-1);
                $event->sheet->getDelegate()->getStyle('R37:Z37')->getAlignment()->setWrapText(true);
            },
        ];
    }
}

I try like that, but the result like this :

Should the height of row automatic added based content/text. But it does not added

How can I solve this problem?

Update :

Seems this script : $event->sheet->getDelegate()->getRowDimension(37)->setRowHeight(-1); is not working in the table

I tried the script outside the table, it worked. So the script only work outside table tag

My table like this :

<table>
    ....
    @php ($group = 'A')
    @php ($number = 0)
    @foreach($values as $item)
    @if($number==0 || $group!=$item['group'])
    <tr>
        <td colspan="9">Kelompok {{$item['group']}}</td>
        <td colspan="2"></td>
        <td colspan="3"></td>
        <td colspan="3"></td>
        <td colspan="9"></td>
        <td colspan="2"></td>
        <td colspan="3"></td>
        <td colspan="3"></td>
        <td colspan="9"></td>
    </tr>
    @php ($number = 0)
    @endif
    <tr>
        <td style="text-align:center;" colspan="2">{{++$number}}</td>
        <td colspan="7">{{$item['lesson_name']}}</td>
        <td style="text-align:center;" colspan="2">{{$item['kb_pengetahuan']}}</td>
        <td style="text-align:center;" colspan="3">{{$item['nilai_pengetahuan']}}</td>
        <td style="text-align:center;" colspan="3">{{$item['predikat_pengetahuan']}}</td>
        <td colspan="9">{{$item['deskripsi_pengetahuan']}}</td>
        <td style="text-align:center;" colspan="2">{{$item['kb_keterampilan']}}</td>
        <td style="text-align:center;" colspan="3">{{$item['nilai_keterampilan']}}</td>
        <td style="text-align:center;" colspan="3">{{$item['predikat_keterampilan']}}</td>
        <td colspan="9">{{$item['deskripsi_keterampilan']}}</td>
    </tr>
    @php ($group = $item['group'])
    @endforeach
</table>

Please help me. I need support for PhpSpreadsheet functionality

回答1:

public function registerEvents(): array
    {
        return [
            AfterSheet::class    => function(AfterSheet $event) {
                ...
                $event->sheet->getDelegate()
                    ->getStyle('R37:Z37')
                    ->applyFromArray([ 'alignment' => ['wrapText' => true]])
            },
        ];
    }