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

Can't write in an existing xlsx file: "Cell coordinate can not be zero-length string" #2848

Closed
2 tasks done
verng95 opened this issue May 23, 2022 · 9 comments · Fixed by #2849
Closed
2 tasks done

Comments

@verng95
Copy link

verng95 commented May 23, 2022

This is:

  • a bug report

What is the current behavior?

PhpOffice\PhpSpreadsheet\Exception:
Cell coordinate can not be zero-length string

  at \vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Cell\Coordinate.php:39
  at PhpOffice\PhpSpreadsheet\Cell\Coordinate::coordinateFromString()
     (\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Cell\Coordinate.php:125)
  at PhpOffice\PhpSpreadsheet\Cell\Coordinate::absoluteCoordinate()
     (\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Cell\Coordinate.php:102)
  at PhpOffice\PhpSpreadsheet\Cell\Coordinate::absoluteReference()
     (\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx\DefinedNames.php:181)
  at PhpOffice\PhpSpreadsheet\Writer\Xlsx\DefinedNames->writeNamedRangeForPrintArea()
     (\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx\DefinedNames.php:48)
  at PhpOffice\PhpSpreadsheet\Writer\Xlsx\DefinedNames->write()
     (\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx\Workbook.php:56)
  at PhpOffice\PhpSpreadsheet\Writer\Xlsx\Workbook->writeWorkbook()
     (\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx.php:386)
  at PhpOffice\PhpSpreadsheet\Writer\Xlsx->save()
     (\src\Module\Container\Controller\Staff\ContainerMovementController.php:205)
  at App\Module\Container\Controller\Staff\ContainerMovementController->print()
     (\vendor\symfony\http-kernel\HttpKernel.php:152)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw()
     (\vendor\symfony\http-kernel\HttpKernel.php:74)
  at Symfony\Component\HttpKernel\HttpKernel->handle()
     (\vendor\symfony\http-kernel\Kernel.php:202)
  at Symfony\Component\HttpKernel\Kernel->handle()
     (\vendor\symfony\runtime\Runner\Symfony\HttpKernelRunner.php:35)
  at Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner->run()
     (\vendor\autoload_runtime.php:29)
  at require_once('\\vendor\\autoload_runtime.php')
     (\public\index.php:29)    

What are the steps to reproduce?

<?php

$reader = new Xlsx();
$xlsx = $reader->load('path to xlsx');

(new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($xlsx))->save('php://output');

What features do you think are causing the issue

  • Writer

Which versions of PhpSpreadsheet and PHP are affected?

Version PHPSpreadsheet - 1.23.0. PHP - 8.0.1

File:
example.xlsx

@MarkBaker
Copy link
Member

Thanks for the report, and for providing the example file.

The issue is actually in the Reader, which loads an invalid print area range that the writer then cannot save

<definedName name="_xlnm.Print_Area" localSheetId="3">'0_Stammdaten'!#REF!</definedName>

@verng95
Copy link
Author

verng95 commented May 23, 2022

The issue is actually in the Reader, which loads an invalid print area range that the writer then cannot save

Can you tell me how to solve this problem? I tried setPrintArea, but it didn't help me.

@MarkBaker
Copy link
Member

I'm working on the fix now; if it's an invalid range for the print area then that data won't get loaded to PhpSpreadsheet, so it won't corrupt our internal data structures

@MarkBaker
Copy link
Member

Although as a temporary measure, you should be able to call:

$xlsx->getSheetByName('0_Stammdaten')->getPageSetup()->clearPrintArea();

before saving

@verng95
Copy link
Author

verng95 commented May 23, 2022

Although as a temporary measure, you should be able to call:

I was in a hurry. Unfortunately, it didn't help. A broken file is recorded that does not open.

@MarkBaker
Copy link
Member

$inputFileType = 'Xlsx';
$fileBaseName = 'printAreaExample';
$inputFileName = __DIR__ . "/../{$fileBaseName}.xlsx";

$reader = IOFactory::createReader($inputFileType);
$spreadsheet = $reader->load($inputFileName);

$spreadsheet->getSheetByName('0_Stammdaten')->getPageSetup()->clearPrintArea();

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save("{$fileBaseName}Write.xlsx");

creates a valid file for me; what else might you be doing? And exactly what error is MS Excel giving when you try to open the saved file?

@verng95
Copy link
Author

verng95 commented May 23, 2022

creates a valid file for me; what else might you be doing? And exactly what error is MS Excel giving when you try to open the saved file?

Error: The error may occur when an Excel file becomes corrupt. You may try repairing the corrupted Excel file manually by using the built-in 'Open and Repair' feature.

Attach excel:
4-10-2021.xlsx

@MarkBaker
Copy link
Member

That's a totally unrelated problem. That's because you're sending a lot of html markup and javascript to the same output stream, beginning with:

<!-- The controller must return a &quot;Symfony\Component\HttpFoundation\Response&quot; object but it returned null. Did you forget to add a return statement somewhere in your controller? (500 Internal Server Error) -->

If I manually edit the resulting output to separate the two blocks (the PhpSpreadsheet stream and the Markup), I get a perfectly ok Excel file, and the following:

image

@verng95
Copy link
Author

verng95 commented May 23, 2022

If I manually edit the resulting output to separate the two blocks (the PhpSpreadsheet stream and the Markup), I get a perfectly ok Excel file, and the following:

Hm.... Thanks.

MarkBaker added a commit that referenced this issue Jul 9, 2022
Note that this will be the last 1.x branch release before the 2.x release. We will maintain both branches in parallel for a time; but users are requested to update to version 2.0 once that is fully available.

### Added

- Added `removeComment()` method for Worksheet [PR #2875](https://github.com/PHPOffice/PhpSpreadsheet/pull/2875/files)
- Add point size option for scatter charts [Issue #2298](#2298) [PR #2801](#2801)
- Basic support for Xlsx reading/writing Chart Sheets [PR #2830](#2830)

  Note that a ChartSheet is still only written as a normal Worksheet containing a single chart, not as an actual ChartSheet.

- Added Worksheet visibility in Ods Reader [PR #2851](#2851) and Gnumeric Reader [PR #2853](#2853)
- Added Worksheet visibility in Ods Writer [PR #2850](#2850)
- Allow Csv Reader to treat string as contents of file [Issue #1285](#1285) [PR #2792](#2792)
- Allow Csv Reader to store null string rather than leave cell empty [Issue #2840](#2840) [PR #2842](#2842)
- Provide new Worksheet methods to identify if a row or column is "empty", making allowance for different definitions of "empty":
  - Treat rows/columns containing no cell records as empty (default)
  - Treat cells containing a null value as empty
  - Treat cells containing an empty string as empty

### Changed

- Modify `rangeBoundaries()`, `rangeDimension()` and `getRangeBoundaries()` Coordinate methods to work with row/column ranges as well as with cell ranges and cells [PR #2926](#2926)
- Better enforcement of value modification to match specified datatype when using `setValueExplicit()`
- Relax validation of merge cells to allow merge for a single cell reference [Issue #2776](#2776)
- Memory and speed improvements, particularly for the Cell Collection, and the Writers.

  See [the Discussion section on github](#2821) for details of performance across versions
- Improved performance for removing rows/columns from a worksheet

### Deprecated

- Nothing

### Removed

- Nothing

### Fixed

- Xls Reader resolving absolute named ranges to relative ranges [Issue #2826](#2826) [PR #2827](#2827)
- Null value handling in the Excel Math/Trig PRODUCT() function [Issue #2833](#2833) [PR #2834](#2834)
- Invalid Print Area defined in Xlsx corrupts internal storage of print area [Issue #2848](#2848) [PR #2849](#2849)
- Time interval formatting [Issue #2768](#2768) [PR #2772](#2772)
- Copy from Xls(x) to Html/Pdf loses drawings [PR #2788](#2788)
- Html Reader converting cell containing 0 to null string [Issue #2810](#2810) [PR #2813](#2813)
- Many fixes for Charts, especially, but not limited to, Scatter, Bubble, and Surface charts. [Issue #2762](#2762) [Issue #2299](#2299) [Issue #2700](#2700) [Issue #2817](#2817) [Issue #2763](#2763) [Issue #2219](#2219) [Issue #2863](#2863) [PR #2828](#2828) [PR #2841](#2841) [PR #2846](#2846) [PR #2852](#2852) [PR #2856](#2856) [PR #2865](#2865) [PR #2872](#2872) [PR #2879](#2879) [PR #2898](#2898) [PR #2906](#2906) [PR #2922](#2922) [PR #2923](#2923)
- Adjust both coordinates for two-cell anchors when rows/columns are added/deleted. [Issue #2908](#2908) [PR #2909](#2909)
- Keep calculated string results below 32K. [PR #2921](#2921)
- Filter out illegal Unicode char values FFFE/FFFF. [Issue #2897](#2897) [PR #2910](#2910)
- Better handling of REF errors and propagation of all errors in Calculation engine. [PR #2902](#2902)
- Calculating Engine regexp for Column/Row references when there are multiple quoted worksheet references in the formula [Issue #2874](#2874) [PR #2899](#2899)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants