Solution for #2501147: Issues with Empty cells

Problem/Motivation

It would be great if a user could choose whether or not $cells->setIteratorOnlyExistingCells was set to TRUE or FALSE in a parameter for phpexcel_import() in phpexcel.inc.

With $cells->setIteratorOnlyExistingCells(TRUE), users are severely limited to the excel sheets they can import.

Take this sheet for example:

key | value | description
aaa |       | sfs
    |  9.8  | fdg
bbb |  8.1  | kjl

Currently it would be read in a way that it would get output into an array like this:

[0] => Array
                (
                    [key] => aaaa
                    [value] => sfs
                )
[1] => Array
                (
                    [key] => 9.8
                    [value] => fdg
                )
[2] => Array
                (
                    [key] => bbb
                    [value] => 8.1
                    [description] => kjl
                )

But if the user could set IteratorOnlyExistingCells to false, they could get an output like this:

[0] => Array
                (
                    [key] => aaaa
                    [value] => 
                    [description] => sfs
                )
[1] => Array
                (
                    [key] => 
                    [value] => 9.8
                    [description] => fdg
                )
[2] => Array
                (
                    [key] => bbb
                    [value] => 8.1
                    [description] => kjl
                )

In this case, the second output is ideal.

Proposed resolution

  • Add $iterate_only_existing_cells = TRUE as a parameter to phpexcel_import()
  • Change $cells->setIterateOnlyExistingCells(TRUE) to $cells->setIterateOnlyExistingCells($iterate_only_existing_cells);
  • Fix documentation to reflect change
CommentFileSizeAuthor
#1 add_parameter_to-2508650-1.patch1.31 KBcbanman

Comments

cbanman’s picture

StatusFileSize
new1.31 KB

This is how I propose it be done

cbanman’s picture

Issue summary: View changes
Related issues: +#2501147: Issues with Empty cells
wadmiraal’s picture

Status: Active » Fixed

This is fixed in the last dev release. Up until now, it was not possible to alter this by calling setIterateOnlyExistingCells() using hook_phpexcel_import() ($op = 'row'), as it was set after calling the hook. Now the hook implementations are invoked after setting setIterateOnlyExistingCells(FALSE), so it is possible to override it like so:

function MYMODULE_phpexcel_import($op, &$data, $phpexcel, $options, $column = NULL, $row = NULL) {
  switch ($op) {
    case 'row':
      $cells = $phpexcel->getCellIterator();
      $cells->setIterateOnlyExistingCells(TRUE);
      break;
  }
}

Side note: default is FALSE again, which reverts the change made in 3.10 (which set it to TRUE).

I will publish a new release as soon as #2501147: Issues with Empty cells is fixed.

  • 2e6438e committed on 7.x-3.x
    Issue #2508650: Allow import hooks to alter the way empty cells are...
cbanman’s picture

Ok, sounds great! Thanks for looking into this and fixing it.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.