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

Column setAutoSize and Table #3356

Closed
1 of 8 tasks
QuentinPELLERIN opened this issue Feb 9, 2023 · 2 comments
Closed
1 of 8 tasks

Column setAutoSize and Table #3356

QuentinPELLERIN opened this issue Feb 9, 2023 · 2 comments

Comments

@QuentinPELLERIN
Copy link

This is:

- [x] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

Column width calculated with setAutoSize should work well with tables.

What is the current behavior?

It does not seem to take into account the filter icon shown in the column added by default when using a table.

I'm assuming this is beacause there is a check in Font::calculateColumnWidth() when you use setAutoSize to see if you have any filter and adjust if necessary but it don't look for tables AutoFilter.

What are the steps to reproduce?

Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:

<?php

require __DIR__ . '/vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
use PhpOffice\PhpSpreadsheet\Worksheet\Table\TableStyle;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$sheet->setCellValue('A1', 'YEAR')
    ->setCellValue('B1', 'QUARTER')
    ->setCellValue('C1', 'COUNTRY')
    ->setCellValue('D1', 'SALES');
$dataArray = [
    ['10', 'Q1', 'United States', 790],
    ['10', 'Q2', 'United States', 730],
    ['10', 'Q3', 'United States', 860],
    ['10', 'Q4', 'United States', 850]
];
$sheet->fromArray($dataArray, null, 'A2');

$table = new Table('A1:D5', 'Sales_Data');$tableStyle = new TableStyle();
$tableStyle->setTheme(TableStyle::TABLE_STYLE_MEDIUM2);
$table->setStyle($tableStyle);
$sheet->addTable($table);

for($i = 'A'; $i <= $sheet->getHighestColumn(); $i++) {
    $sheet->getColumnDimension($i)->setAutoSize(true);
}

What features do you think are causing the issue

  • Reader
  • Writer
  • Styles
  • Data Validations
  • Formula Calculations
  • Charts
  • AutoFilter
  • Form Elements

Which versions of PhpSpreadsheet and PHP are affected?

PhpSpreadsheet 1.27.1
PHP 8.1.4
(Xlsx writer)

@MarkBaker
Copy link
Member

Yes, previously with AutoFilter, it was only possible to have a single AutoFilter per Worksheet. But now as Tables can have their own AutoFilter, it's possible to have multiple AutoFilter ranges per Worksheet; and I'd forgotten about handling AutoFit for Tables. The same issue will also apply when I eventually add support for Pivot Tables.

It isn't so easy to add this to the existing code for calculating a column width based on its content; so I'll probably need to do some refactoring of that part of the codebase, and that means it won't be as quick to resolve as I'd like.

@kczx3
Copy link

kczx3 commented Jul 23, 2024

I still have problems with using setAutoSize where the columns are sometimes too small and other times they are too large. I assume that it is impossible to get this exact without recreating the logic that Excel uses (or any other application for that matter). Is that correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants