Skip to content

[Bug] Percentages stored as strings are not converted during formula calculations as they are in Excel #3155

Closed
@fdjohnston

Description

@fdjohnston

This is:

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

What is the expected behavior?

When converting a spreadsheet to an array using toArray(), formulas that reference a cell that contains a percentage stored as a string should still calculate, like they do in Excel.

What is the current behavior?

PHPSpreadsheet returns a #VALUE! error instead of converting the string to a float.

What are the steps to reproduce?

<?php

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

//Create reader
$xlsxReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$xlsxReader->setReadDataOnly(true);

//Load worksheet
$workbook = $xlsxReader->load('Book1.xlsx');
$worksheet = $workbook->getSheetByName('Sheet1');

//Convert to array
$data = $worksheet->toArray(null, true, false, false);

var_dump($data);
die();

Output:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(7) "#VALUE!"
    [1]=>
    NULL
  }
  [1]=>
  array(2) {
    [0]=>
    int(100)
    [1]=>
    string(2) "2%"
  }
}

Expected output:

array(2) {
  [0]=>
    [0]=>
    float(2)
    [1]=>
    NULL
  }
  [1]=>
  array(2) {
    [0]=>
    int(100)
    [1]=>
    string(2) "2%"
  }
}

I have attached an XLSX test file.
Book1.xlsx

What features do you think are causing the issue

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

The validateBinaryOperand() function in src/PhpSpreadsheet/Calculation/Calculation.php already supports converting fractions stored as strings to numbers. I think adding a function here to convert percentages stored as strings to numbers could solve this problem.

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

Only tested on XLSX files, but based on my reading of the code this will likely affect all spreadsheet formats.

Which versions of PhpSpreadsheet and PHP are affected?

PHPSpreadsheet 1.25.2

I would be happy to submit a PR to address this issue if the maintainers agree it is a bug and should be fixed.

Metadata

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