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

add custome sheet fonctions #4048

Open
timouchee opened this issue May 28, 2024 · 1 comment
Open

add custome sheet fonctions #4048

timouchee opened this issue May 28, 2024 · 1 comment

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);
  }
}

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