You are here

public function MigrateSourceSpreadsheet::load in Migrate 7.2

Loads the workbook.

Return value

bool Returns true if the workbook was successfully loaded, otherwise false.

2 calls to MigrateSourceSpreadsheet::load()
MigrateSourceSpreadsheet::performRewind in plugins/sources/spreadsheet.inc
Implements MigrateSource::performRewind().
MigrateSourceSpreadsheet::__construct in plugins/sources/spreadsheet.inc
Simple initialization.

File

plugins/sources/spreadsheet.inc, line 125
Define a MigrateSource for importing from spreadsheet files.

Class

MigrateSourceSpreadsheet
Implements MigrateSource, to handle imports from XLS files.

Code

public function load() {

  // Check that the file exists.
  if (!file_exists($this->file)) {
    Migration::displayMessage(t('The file !filename does not exist.', array(
      '!filename' => $this->file,
    )));
    return FALSE;
  }

  // Check that required modules are enabled.
  if (!module_exists('libraries')) {
    Migration::displayMessage(t('The Libraries API module is not enabled.'));
    return FALSE;
  }
  if (!module_exists('phpexcel')) {
    Migration::displayMessage(t('The PHPExcel module is not enabled.'));
    return FALSE;
  }
  $library = libraries_load('PHPExcel');
  if (empty($library['loaded'])) {
    Migration::displayMessage(t('The PHPExcel library could not be found.'));
    return FALSE;
  }

  // Load the workbook.
  try {

    // Identify the type of the input file.
    $type = PHPExcel_IOFactory::identify($this->file);

    // Create a new Reader of the file type.
    $reader = PHPExcel_IOFactory::createReader($type);

    // Advise the Reader that we only want to load cell data.
    $reader
      ->setReadDataOnly(TRUE);

    // Advise the Reader of which worksheet we want to load.
    $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(),
    )));
    return FALSE;
  }
  return TRUE;
}