Skip to content

dev/core#6927 issue with download to excel from search kit - #38019

Merged
colemanw merged 1 commit into
civicrm:masterfrom
jaapjansma:issue_6927
Nov 26, 2026
Merged

colemanw merged 1 commit into
civicrm:masterfrom
jaapjansma:issue_6927

Conversation

@jaapjansma

@jaapjansma jaapjansma commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Overview

When exporting data to excel from a search kit and one of the columns starts with = then the export fails.

In our client database they have phone numbers stored as = secretary 061234 And I know this is not ideal but it is possible. And this bug will probably also happen when another field starts =.

This causes the export think it is an excel formula. This PR sets the column data type to String when the data type of the data is also a String.

This PR also changes the unit test to specifically test for a column starting with a =.

Reproduction steps

  1. Click on Contacts -> New Individual.
  2. Entered First Name and Last Name and clicked Save.
  3. Enter a phone number as '= chairmain 06234'
  4. Create a searchkit. With at least the name of the contact and the primary phone number
  5. Run the search
  6. Export the action

Current behaviour

Export hangs.

The log file reads

Argument #1 ($array) must be of type array, string given" in /home/pdcdeherberg/www/testcrm/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/Calculation.php on line 2049

Expected behaviour

The excel file can be downloaded

Environment information

  • CiviCRM: 6.20.0
  • PHP: 8.4..._
  • CMS: Drupal 11_

Comments

The log file contains:

#0 /home/pdcdeherberg/www/testcrm/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/Calculation.php(2049): array_intersect_key("#NAME?", "#NAME?")
#1 /home/pdcdeherberg/www/testcrm/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/Calculation.php(783): PhpOffice\PhpSpreadsheet\Calculation\Calculation->processTokenStack((Array:3), "C2503", Object(PhpOffice\PhpSpreadsheet\Cell\Cell))
#2 /home/pdcdeherberg/www/testcrm/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/Calculation.php(557): PhpOffice\PhpSpreadsheet\Calculation\Calculation->_calculateFormulaValue("sec diac", "C2503", Object(PhpOffice\PhpSpreadsheet\Cell\Cell))
#3 /home/pdcdeherberg/www/testcrm/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Cell.php(470): PhpOffice\PhpSpreadsheet\Calculation\Calculation->calculateCellValue(Object(PhpOffice\PhpSpreadsheet\Cell\Cell), TRUE)
#4 /home/pdcdeherberg/www/testcrm/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Cell.php(434): PhpOffice\PhpSpreadsheet\Cell\Cell->getCalculatedValue()
#5 /home/pdcdeherberg/www/testcrm/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Worksheet/Worksheet.php(767): PhpOffice\PhpSpreadsheet\Cell\Cell->getCalculatedValueString()
#6 /home/pdcdeherberg/www/testcrm/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php(486): PhpOffice\PhpSpreadsheet\Worksheet\Worksheet->calculateColumnWidths()
#7 /home/pdcdeherberg/www/testcrm/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php(102): PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet->writeCols(Object(PhpOffice\PhpSpreadsheet\Shared\XMLWriter), Object(PhpOffice\PhpSpreadsheet\Worksheet\Worksheet))
#8 /home/pdcdeherberg/www/testcrm/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx.php(417): PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet->writeWorksheet(Object(PhpOffice\PhpSpreadsheet\Worksheet\Worksheet), (Array:39733), FALSE)
#9 /home/pdcdeherberg/www/testcrm/vendor/civicrm/civicrm-core/ext/search_kit/Civi/Api4/Action/SearchDisplay/ResultDataTrait.php(169): PhpOffice\PhpSpreadsheet\Writer\Xlsx->save("php://output")
#10 /home/pdcdeherberg/www/testcrm/vendor/civicrm/civicrm-core/ext/search_kit/Civi/Api4/Action/SearchDisplay/ResultDataTrait.php(86): Civi\Api4\Action\SearchDisplay\Download->outputSpreadsheet((Array:12762), (Array:9))

@civibot

civibot Bot commented Nov 11, 2026

Copy link
Copy Markdown

🤖 Thank you for contributing to CiviCRM! ❤️ We will need to test and review this PR. 👷

Introduction for new contributors...
  • If this is your first PR, an admin will greenlight automated testing with the command ok to test or add to whitelist.
  • A series of tests will automatically run. You can see the results at the bottom of this page (if there are any problems, it will include a link to see what went wrong).
  • A demo site will be built where anyone can try out a version of CiviCRM that includes your changes.
  • If this process needs to be repeated, an admin will issue the command test this please to rerun tests and build a new demo site.
  • Before this PR can be merged, it needs to be reviewed. Please keep in mind that reviewers are volunteers, and their response time can vary from a few hours to a few weeks depending on their availability and their knowledge of this particular part of CiviCRM.
  • A great way to speed up this process is to "trade reviews" with someone - find an open PR that you feel able to review, and leave a comment like "I'm reviewing this now, could you please review mine?" (include a link to yours). You don't have to wait for a response to get started (and you don't have to stop at one!) the more you review, the faster this process goes for everyone 😄
  • To ensure that you are credited properly in the final release notes, please add yourself to contributor-key.yml
  • For more information about contributing, see CONTRIBUTING.md.
PR commands & links...
  • /rebase <?branch-name> will rebase your branch and change the base of the PR (if <branch-name> is specified).
  • /squash will combine all commits (keeping only the first commit messsage).
  • /port <branch-name> will create a copy of this PR against a different branch.
  • /lint will automatically fix linting errors, amending commits as needed.
  • retest this please will rerun the tests and rebuild the demo site.
  • 📖 Review standards
  • 🗒️ Review template (brief or verbose)

➡️ Online demo of this PR 🔗

@civibot civibot Bot added the master label Nov 11, 2026
@civibot

civibot Bot commented Nov 11, 2026

Copy link
Copy Markdown

The issue associated with the Pull Request can be viewed at https://lab.civicrm.org/dev/core/-/issues/6927

@jaapjansma
jaapjansma force-pushed the issue_6927 branch 2 times, most recently from 4038fa6 to 4d53629 Compare November 11, 2026 15:08
@colemanw

Copy link
Copy Markdown
Member

Thanks for this, @jaapjansma

A couple things:

  1. This fixes Text but misses other data types, (Activity details, Notes, textarea custom fields, plus Array and calculated expressions where dataType is null). If any of those start with =, the export will still fail.

  2. Calling $cell->setValue() followed by $cell->setDataType(DataType::TYPE_STRING) sets the value twice on every string cell in the export, and still triggers PhpSpreadsheet's formula parser on strings starting with = before reverting them.

Instead of modifying the type after setting the value, what do you think about setting it explicitly upfront if the formatted value begins with =?

$formattedValue = $this->formatColumnValue($col, $value);

if (is_string($formattedValue) && str_starts_with($formattedValue, '=')) {
  $cell->setValueExplicit($formattedValue, DataType::TYPE_STRING);
}
else {
  $cell->setValue($formattedValue);
}

This:

  • Covers Text, Array, String, rewrites, and expressions equally.
  • Bypasses formula parsing entirely for strings beginning with =.
  • Automatically sets PhpSpreadsheet's quotePrefix so Excel treats it as literal text without formula injection risks.
  • Avoids clearing hyperlinks so the hyperlink block doesn't need to be moved.

@jaapjansma

jaapjansma commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor Author

@colemanw happy to implement your suggestion.

But rather for checking whether the value is a string. We can also check if the value has a data type Int or Float to set it to numeric. And default data type to string. What do you think?

$formattedValue = $this->formatColumnValue($col, $value);
$dataType = DataType::TYPE_STRING;
if ($value['dataType'] === 'Int' || $value['dataType'] == 'Float') {
  $dataType = DataType::TYPE_NUMERIC;
}
$cell->setValueExplicit($formattedValue, $dataType);

@colemanw

Copy link
Copy Markdown
Member

@jaapjansma I think that would break Date, Timestamp, Money, and Boolean data types. Also core doesn't always use 'Int', Api4 uses 'Integer'. I think the simplest fix is to let PhpSpreadsheet handle normal types, and only override when using a string with '=':

$formattedValue = $this->formatColumnValue($col, $value);

if (is_string($formattedValue) && str_starts_with($formattedValue, '=')) {
  $cell->setValueExplicit($formattedValue, DataType::TYPE_STRING);
}
else {
  $cell->setValue($formattedValue);
}

@jaapjansma

Copy link
Copy Markdown
Contributor Author

Makes sense.

@jaapjansma
jaapjansma force-pushed the issue_6927 branch 2 times, most recently from c63f6f4 to 28ad256 Compare November 12, 2026 19:54
@colemanw colemanw added the merge ready PR will be merged after a few days if there are no objections label Nov 12, 2026
@colemanw
colemanw merged commit 3612f78 into civicrm:master Nov 26, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

master merge ready PR will be merged after a few days if there are no objections

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants