Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
sergeynilov opened this issue Jul 20, 2024 · 5 comments
Open
2 tasks done

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

sergeynilov opened this issue Jul 20, 2024 · 5 comments
Labels
Change Request Open XML (Word 2007+) Status: Waiting for feedback Question has been asked, waiting for response from PR author

Comments

@sergeynilov
Copy link

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)
@Progi1984
Copy link
Member

@sergeynilov Hi have you got a sample file with scrolling bar in table, please ?

@Progi1984 Progi1984 added the Status: Waiting for feedback Question has been asked, waiting for response from PR author label Aug 16, 2024
@sergeynilov
Copy link
Author

@Progi1984, have I to check this feature implemented ? Which version of PHPOffice/PHPWord must I use for checkin ?

@Progi1984
Copy link
Member

@sergeynilov No. I Need a sample file for checking how to implement it.

@sergeynilov
Copy link
Author

If you mean an example of resulting docx file I attach it:

I created 6 columns with structure

$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],
];

but only 4 are visible .

employee-card--27-of-the-app-2024-08-19 16:27:31.docx

@Progi1984
Copy link
Member

@sergeynilov I want a docx file with a table with scrolling bar as expected result. That allows me to analyze the Docx file structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Change Request Open XML (Word 2007+) Status: Waiting for feedback Question has been asked, waiting for response from PR author
Development

No branches or pull requests

2 participants