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

calculate error for CONCATENATE #4061

Open
1 of 8 tasks
hbh112233abc opened this issue Jun 5, 2024 · 3 comments
Open
1 of 8 tasks

calculate error for CONCATENATE #4061

hbh112233abc opened this issue Jun 5, 2024 · 3 comments

Comments

@hbh112233abc
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?

test.xlsx
the column B set =CONCATENATE(@A:A,"-",@C:C) and the table is like below:

A B C
a a-1 1
b b-2 2
c c-3 3

What is the current behavior?

read the test.xlsx and the active sheet toArray, B column error:

image

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

$file        = __DIR__ . '/test.xlsx';
$reader      = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load($file);
$sheetData   = $spreadsheet->getActiveSheet()->toArray();
var_dump($sheetData);

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?

test.xlsx save as test.xls, read by PhpOffice\PhpSpreadsheet\Reader\Xls use 3min (too long) return the sheet data, the sheet data is the same with error calculate.

Which versions of PhpSpreadsheet and PHP are affected?

PhpSpreadsheet 1.29.0
PHP 8.0.30

@oleibman
Copy link
Collaborator

oleibman commented Jun 5, 2024

This is a variation on a theme. The at-sign doesn't actually exist in the formula - Excel is adding it. For an explanation of what it does, see #3659 (comment). I still have been unable to figure out why it does or doesn't add it. This has come up several times (see issue #3901 or issue #3708 among others). Yours would be the first report where somebody likes this "feature". At any rate, it looks like PhpSpreadsheet is trying to calculate the formula that's actually there. and doesn't wind up with the correct result; and it also appears that it doesn't handle the case where a leading at-sign is actually there (Calculation engine throws exception). I'll look into it, but I wouldn't expect a quick resolution.

In case it helps, you can reformulate your B cells as the following to get the same result in Excel, and PhpSpreadsheet will handle it correctly:

=CONCAT(INDIRECT("A"&ROW()), "-", INDIRECT("C"&ROW()))

@oleibman
Copy link
Collaborator

I've stumbled upon an interesting result. Microsoft provides the following note about CONCATENATE - "In Excel 2016, Excel Mobile, and Excel for the web, this function has been replaced with the CONCAT function. Although the CONCATENATE function is still available for backward compatibility, you should consider using CONCAT from now on. This is because CONCATENATE may not be available in future versions of Excel." Like Microsoft, we will continue to support it.

Now for the interesting part. The CONCATENATE function (and, it would appear, the ampersand concatenate operator) gives the result you see. OTOH, the CONCAT function gives the same result as PhpSpreadsheet. So, intentionally or not, CONCATENATE and CONCAT are not the same function in Excel. PhpSpreadsheet currently executes the same code for both. PhpSpreadsheet does seem to get the correct result for the concatenate operator when results are returned as arrays.

@oleibman
Copy link
Collaborator

The difference between CONCATENATE and CONCAT is how they handle arrays (usually in the form of cell ranges). I have reworked CONCATENATE as part of PR #3962 with other array-related changes. The PR is still in draft status, but I hope to "make it real" within a week or two.

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

No branches or pull requests

2 participants