Skip to content

How to show hidden columns of a table in "phpoffice/phpword"? #2633

@sergeynilov

Description

@sergeynilov

Describe the bug and add attachments

In laravel 10 app using "phpoffice/phpword": "^1.2" I need to write with several columns with long width
and resulting table in docx file looks not goood, as passing data with 6 columns :

        $tableColumns = [
            ['column_id' => 'doc_code', 'title' => 'Document code', 'width' => 1500, 'align' => 'left'],
            ['column_id' => 'doc_title', 'title' => 'Document title', 'width' => 5000],
            ['column_id' => 'doc_type', 'title' => 'Type', 'width' => 2000],
            ['column_id' => 'doc_status', 'title' => 'Status', 'width' => 2000],
            ['column_id' => 'department_name', 'title' => 'Department', 'width' => 3000],
            ['column_id' => 'applied_from', 'title' => 'applied_from', 'width' => 1500],
        ];
        $this->addTableWithData( // table with employee docs
            dataArray: $docsData,
            columnsArray: $tableColumns,
            titleWordTextLineEnum: 'HeaderText'
        );

Reading here https://phpword.readthedocs.io/en/stable/styles.html#table I did not find if this table hase any horizontal scrolling

    protected function addTableWithData(array $dataArray, array $columnsArray, string $titleWordTextLineEnum, string $tableHeader= ''): void
    {
        //        \Log::info(QuizzesInitFacade::varDump($dataArray, ' -1 addTableWithData $dataArray::'));
        $section = $this->phpWord->addSection(['marginTop' => 50, 'marginLeft' => 50, 'marginRight' => 50,
                                               'marginBottom' => 50, 'breakType' => 'continuous']);
        $fontStyle = $this->setFontStyleByWordTextLineEnum($titleWordTextLineEnum);
        if($tableHeader) {
            $textElement = $section->addText('Answers');
            $textElement->setFontStyle($fontStyle);
        }
    
        $rows = count($dataArray);
        $cols = count($columnsArray);
    
        $table = $section->addTable($this->getTableStyle());
        $table->addRow();
        for ($col = 0; $col < $cols; $col++) {
            $table->addCell($columnsArray[$col]['width'])->addText(Str::headline($columnsArray[$col]['title']), $this->getTableHeaderLineStyle(), array('align' => 'center'));
        }
    
        for ($row = 0; $row < $rows; $row++) {
            $table->addRow();
            for ($col = 0; $col < $cols; $col++) {
                $cellValue = '';
                if(isset($dataArray[$row][$columnsArray[$col]['column_id']])) {
                    $columnId = $columnsArray[$col]['column_id'];
                    $val = $dataArray[$row][$columnId] ?? '';
                    $cellValue = $this->getTableCellValue($val, $columnId, $row, $col);
                }
                $cellAlign = 'left';
                if(!empty($columnsArray[$col]['align'])) {
                    $cellAlign = $columnsArray[$col]['align'];
                }
                $table->addCell($columnsArray[$col]['width'])->addText($cellValue, $this->getTableContentRowLineStyle(), array('align' => $cellAlign)) ;
            }
        }
    }

...

    protected function addTableWithData(array $dataArray, array $columnsArray, string $titleWordTextLineEnum, string $tableHeader= ''): void
    {
        $section = $this->phpWord->addSection(['marginTop' => 600, 'marginLeft' => 600, 'marginRight' => 600,
             'marginBottom' => 600, 'breakType' => 'continuous']); // I tried to set margin props bigger -no effect
    
    
        // $section has no any getSettings function :
        // dd($section->getSettings()); // If to uncomment this line it shows "Null" - so error Call to a member function setMarginLeft() on null on next line
        $section->getSettings()->setMarginLeft(-600);

It seems to me that I create $section in the same way ... Also looking at

https://phpword.readthedocs.io/en/stable/general.html?highlight=getSettings#magnification-setting

I did not any examples that getSettings method in $section object...

?

  1. If there a way somehow show 6 columns with big width ?
  2. In my code above I show columns with width. Which is biggest summary of all columns ?

Expected behavior

actually I see only 4 columns : 1st and 6th columns are not visible

Steps to reproduce

PHPWord version(s) where the bug happened

^1.2

PHP version(s) where the bug happened

8.2

Priority

  • I want to crowdfund the bug fix (with @algora-io) and fund a community developer.
  • I want to pay the bug fix and fund a maintainer for that. (Contact @Progi1984)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions