Skip to content

The exact autosize does not work correctly #3626

Closed
@ivanwonders

Description

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?

The size of the columns is adjusted exactly to the text as in Excel in the columns B, C, and D.

What is the current behavior?

Despite setting the exact calculation method and the setAutoSize to true, there is still a lot of margin on the sides.

What are the steps to reproduce?

You can use the attached code to check it

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';

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

// add code that show the issue here...
$col = 1;
$row = 1;

$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$spreadsheet->getDefaultStyle()->getFont()->setName('Calibri');
$spreadsheet->getDefaultStyle()->getFont()->setSize(11);
$sheet = $spreadsheet->getActiveSheet();
$sheet->getColumnDimensionByColumn($col++)->setWidth(120, \PhpOffice\PhpSpreadsheet\Helper\Dimension::UOM_PIXELS);
$sheet->setCellValueByColumnAndRow($col++, $row, 'Column2');
$sheet->getStyle("B1")
    ->getFont()
    // ->setName('Arial')
    ->setSize(20);
$sheet->setCellValueByColumnAndRow($col++, $row, 'Col3');
$sheet->setCellValueByColumnAndRow($col++, $row, 'Big Column in 4 position');
$row++;
$sheet->getStyle('A1:' . $sheet->getColumnDimensionByColumn($col)->getColumnIndex() . $row)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);

//As the server is in a debian environment, we tried to copy the ttf from the calibri font. This gave better results but they are still bad.
\PhpOffice\PhpSpreadsheet\Shared\Font::setTrueTypeFontPath(__DIR__."/fonts/");

\PhpOffice\PhpSpreadsheet\Shared\Font::setAutoSizeMethod(\PhpOffice\PhpSpreadsheet\Shared\Font::AUTOSIZE_METHOD_EXACT);

foreach (range('B', $sheet->getHighestColumn()) as $col) {
    $sheet->getColumnDimension($col)->setAutoSize(true);
}

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$file= date('Y-m-d_His').'.xlsx';
$writer->save($file);

If this is an issue with reading a specific spreadsheet file, then it may be appropriate to provide a sample file that demonstrates the problem; but please keep it as small as possible, and sanitize any confidential information before uploading.

What features do you think are causing the issue

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

Does an issue affect all spreadsheet file formats? If not, which formats are affected?

Which versions of PhpSpreadsheet and PHP are affected?

PHP Version 7.4.33
PhpSpreadsheet 1.29.0

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions