Why no "search" like function?

Topics: User Forum
Feb 24, 2011 at 1:20 PM

I'm wondering why no "search" style function is implemented? I quickly threw this together for the WorkSheet class just to try it and wonder if it's way to resource intensive when the worksheet grows?

    /**
     *
     * Search for a cell or a collections of cells given it's content
     * @param string $contentString
     */
    public function getColumnByContent($contentString, $mutipleMatches = false) {
    foreach($this->getRowIterator() as $row){
      $cellIterator = $row->getCellIterator();
      foreach($cellIterator as $cell){
        if(strcmp($cell->getValue(), $contentString) == 0) {
          if($mutipleMatches) {
            $cells[] = $cell;
          }
          else {
            return $cell;
          }
        }
      }
    }
    return $cells;
    }

With this I can now use it a bit like a template system and do
$pSheet->getColumnByContent('#EMAIL_ADDRESS#')->setValue('foobar@hotmail.com');

Coordinator
Feb 24, 2011 at 9:05 PM

The row and cell iterators are quite processor intensive.

foreach($this->getCellCollection(false) as $cell) {
   ...
}

might be more efficient than using the iterators.

The argument passed to getCellCollection() indicates whether the returned list of cell addresses should be sorted or not... sorting will add overhead.

 

Feb 25, 2011 at 6:24 AM

Since getCellCollection seems to return an array of cell coordinates I guess you would have to do something like this?

 

foreach($this->getCellCollection(false) as $cellCoordinate) {
      $cell = $this->getCell($cellCoordinate);
...

Coordinator
Feb 25, 2011 at 2:19 PM

That's right. getCellCollection() only returns the cell addresses, so you'll need to manually fetch each cell rather than doing so through the iterators.

I've not tested the code, but I think it should be faster than the iterators, despite the manual $cell = $this->getCell($cellCoordinate);

Oct 16, 2011 at 6:14 PM
Edited Oct 16, 2011 at 6:19 PM

Where i can insert this code? I've tries to insert it in Worksheet.php but no luck

Fatal error: Call to a member function setValue() on a non-object in ...

Aug 15, 2014 at 1:28 AM
I used what you guys were speaking about to write a helper function.

See: https://phpexcel.codeplex.com/discussions/204003