diff --git a/plugins/sources/spreadsheet.inc b/plugins/sources/spreadsheet.inc index d25692f..b7b6de7 100644 --- a/plugins/sources/spreadsheet.inc +++ b/plugins/sources/spreadsheet.inc @@ -25,6 +25,13 @@ class MigrateSourceSpreadsheet extends MigrateSource { protected $workbook; /** + * PHPExcel object for storing the worksheet data. + * + * @var PHPExcel_Worksheet + */ + protected $worksheet; + + /** * The name of the worksheet that will be processed. * * @var string @@ -76,15 +83,13 @@ class MigrateSourceSpreadsheet extends MigrateSource { // Load the workbook. if ($this->load()) { - $worksheet = $this->workbook->getSheet(); - // Get the dimensions of the worksheet. - $this->rows = $worksheet->getHighestDataRow(); - $this->cols = PHPExcel_Cell::columnIndexFromString($worksheet->getHighestDataColumn()); + $this->rows = $this->worksheet->getHighestDataRow(); + $this->cols = PHPExcel_Cell::columnIndexFromString($this->worksheet->getHighestDataColumn()); // Map field names to their column index. for ($col = 0; $col < $this->cols; ++$col) { - $this->fields[$col] = trim($worksheet->getCellByColumnAndRow($col, 1)->getValue()); + $this->fields[$col] = trim($this->worksheet->getCellByColumnAndRow($col, 1)->getValue()); } $this->unload(); @@ -128,6 +133,7 @@ class MigrateSourceSpreadsheet extends MigrateSource { $reader->setLoadSheetsOnly($this->sheetName); // Load the source file. $this->workbook = $reader->load($this->file); + $this->worksheet = $this->workbook->getSheet(); } catch (Exception $e) { Migration::displayMessage(t('Error loading file: %message', array('%message' => $e->getMessage()))); @@ -199,14 +205,17 @@ class MigrateSourceSpreadsheet extends MigrateSource { * @return null|object */ public function getNextRow() { + migrate_instrument_start('MigrateSourceSpreadsheet::next'); ++$this->rowNumber; + $source_keys = array_keys($this->activeMap->getSourceKey()); + if ($this->rowNumber <= $this->rows) { - $worksheet = $this->workbook->getSheet(); $row_values = array(); - for ($col = 0; $col < $this->cols; ++$col) { - $row_values[$this->fields[$col]] = trim($worksheet->getCellByColumnAndRow($col, $this->rowNumber)->getValue()); + if (in_array($this->fields[$col], $source_keys)) { + $row_values[$this->fields[$col]] = trim($this->worksheet->getCellByColumnAndRow($col, $this->rowNumber)->getValue()); + } } return (object) $row_values; @@ -214,7 +223,10 @@ class MigrateSourceSpreadsheet extends MigrateSource { else { // EOF, close the workbook. $this->unload(); + + migrate_instrument_stop('MigrateSourceSpreadsheet::next'); return NULL; } } + }