PhpSpreadsheet icon indicating copy to clipboard operation
PhpSpreadsheet copied to clipboard

Can we add a argument to set keys to custom value instead of A-Z or 0-x

Open duprasf opened this issue 3 years ago • 0 comments

This is:

- [ ] a bug report
- [X] 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?

I would like to be able to pass an argument to Worksheet::rangeToArray() that would replace the index of the returned array for columns instead of the columns indexed by number counting from zero or the columns indexed by their actual row and column IDs

What is the current behavior?

The argument $returnCellRef only allow users to choose columns indexed by number counting or their actual row and column IDs

<?php

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

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

// add code that show the issue here...
foreach($spreadsheet->getActiveSheet()->rangeToRowGenerator(range:'A2:F1300', calculateFormulas:false, formatData: true, returnCellRef:false) as $row) {
    foreach($row as $key=>$value) {
        // $keys are number based from 0-5
        // if argument $returnCellRef is set to true, the $key are A-F
    }
    // Do something...
}

What features do you think are causing the issue

  • [X] 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?

I only tested xlsx

Which versions of PhpSpreadsheet and PHP are affected?

PHP 8.1 and phpspreadsheet 1.25 (from what I can see in composer.json)

How should this be fixed

This could be fixed by adding a $map argument that would update the $cRef variable in such a way:

public function rangeToArray($range, $nullValue = null, $calculateFormulas = true, $formatData = true, $returnCellRef = false, array $map = [])
{
    //[...]
    $cRef = $returnCellRef ? $col : ++$c;
    if (isset($map[$cRef])) {
        $cRef = $map[$cRef];
    }
    //[...]
}

I will submit a pull request soon that will hopefully be approved for this issue.

duprasf avatar Nov 17 '22 22:11 duprasf