Skip to content

add custome sheet fonctions #4048

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

Closed
timouchee opened this issue May 28, 2024 · 2 comments
Closed

add custome sheet fonctions #4048

timouchee opened this issue May 28, 2024 · 2 comments

Comments

@timouchee
Copy link

i i try to import a new usable function on the sheet a custome function .
did some function like "addcustomefunction()" existe already in this current project ??

if not can i have a explaination of how to proffesionaly update the code in order to import in the code my custom function ?

thanks

@infojunkie
Copy link
Contributor

infojunkie commented Jun 8, 2024

I don't know about "professionally", but I made a one-character modification in the source code to allow adding custom functions: infojunkie@69ed958

By returning a reference to the list of functions, you can add a custom function or override an existing one in your own code, like this:

$calculation = $spreadsheet->getCalculationEngine();
$functions = &get_class($calculation)::getFunctions();
$functions['CHOOSECOLS'] = [
  'category' => Category::CATEGORY_MATH_AND_TRIG,
  'functionCall' => [Custom::class, 'choosecols'],
  'argumentCount' => '2+',
];

class Custom
{
  /**
   * CHOOSECOLS.
   *
   * Returns the specified columns from an array.
   *
   * @param mixed $cells The cells being searched
   * @param int $cols List of numeric column indexes to extract
   *
   * @return array|string The resulting array, or a string containing an error
   */
  public static function choosecols(mixed $cells, int ...$cols): array|string
  {
    $columns = RowColumnInformation::COLUMNS($cells);
    if (is_string($columns)) {
      return $columns;
    }
    $result = [];
    foreach ($cols as $col) {
      if (!$col || abs($col) > $columns) {
        return ExcelError::VALUE();
      }
      $result[] = array_column($cells, $col > 0 ? $col-1 : $columns-$col);
    }
    return Matrix::transpose($result);
  }
}

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Mar 2, 2025
For an overview of why this is desired (and ways that people have coped with its absence), see issue PHPOffice#2900 and issue PHPOffice#4048; also PR PHPOffice#4043 which will be superseded by this PR.

The list of Excel functions is moved from Calculation/Calculation to its own member. I believe that it is done in a way that will not cause big complications to two experiments from @MarkBaker (PR PHPOffice#2714 and PR PHPOffice#2734). I believe it is also done in such a way that further refactoring of Calculation can follow this model.

Custom functions can be added or removed from the function list. You cannot add a function if it already exists in the list, and you cannot remove a non-custom function from the list. They will, of course, not be understood by Excel if written to a spreadsheet; the use case is mainly using the Calculation engine outside of spreadsheet context.
@oleibman
Copy link
Collaborator

oleibman commented Mar 8, 2025

Closing, this is handled by PR #4390, which is now merged.

@oleibman oleibman closed this as completed Mar 8, 2025
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

3 participants