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

Fix for #1533 #1534

Merged
merged 4 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ private function writeCellFormula(XMLWriter $objWriter, string $cellValue, Cell
{
$calculatedValue = $this->getParentWriter()->getPreCalculateFormulas() ? $pCell->getCalculatedValue() : $cellValue;
if (is_string($calculatedValue)) {
if (substr($calculatedValue, 0, 1) === '#') {
if (\PhpOffice\PhpSpreadsheet\Calculation\Functions::isError($calculatedValue)) {
$this->writeCellError($objWriter, 'e', $cellValue, $calculatedValue);

return;
Expand Down
62 changes: 62 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Xlsx/StartsWithHashTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;

use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as Reader;
use PhpOffice\PhpSpreadsheet\Settings;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as Writer;
use PHPUnit\Framework\TestCase;
use ZipArchive;

class StartsWithHashTest extends TestCase
{
public function testStartWithHash(): void
{
$outputFilename = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test');
Settings::setLibXmlLoaderOptions(null);
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValueExplicit('A1', '#define M', DataType::TYPE_STRING);
$sheet->setCellValue('A2', '=A1');
$sheet->setCellValue('A3', '=UNKNOWNFUNC()');

$writer = new Writer($spreadsheet);
$writer->save($outputFilename);

$reader = new Reader();
$sheet = $reader->load($outputFilename);
unlink($outputFilename);

self::assertSame('#define M', $sheet->getActiveSheet()->getCell('A1')->getValue());
self::assertSame('#define M', $sheet->getActiveSheet()->getCell('A2')->getCalculatedValue());
self::assertSame('f', $sheet->getActiveSheet()->getCell('A3')->getDataType());
self::assertSame('#NAME?', $sheet->getActiveSheet()->getCell('A3')->getCalculatedValue());
self::assertSame('f', $sheet->getActiveSheet()->getCell('A3')->getDataType());
}

public function testStartWithHashReadRaw(): void
{
// Make sure raw data indicates A3 is an error, but A2 isn't.
$outputFilename = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test');
Settings::setLibXmlLoaderOptions(null);
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValueExplicit('A1', '#define M', DataType::TYPE_STRING);
$sheet->setCellValue('A2', '=A1');
$sheet->setCellValue('A3', '=UNKNOWNFUNC()');

$writer = new Writer($spreadsheet);
$writer->save($outputFilename);
$zip = new ZipArchive();
$zip->open($outputFilename);
$resultSheet1Raw = $zip->getFromName('xl/worksheets/sheet1.xml');
$zip->close();
unlink($outputFilename);

self::assertStringContainsString('<c r="A3" t="e">', $resultSheet1Raw);
self::assertStringContainsString('<c r="A2" t="str">', $resultSheet1Raw);
}
}