You are here

function ExcelRange::range_expression_to_range_array in Feeds Excel 6

Same name and namespace in other branches
  1. 7 includes/ExcelRange.inc \ExcelRange::range_expression_to_range_array()

Convert a single range expression to a range array

1 call to ExcelRange::range_expression_to_range_array()
ExcelRange::expression_to_ranges in includes/ExcelRange.inc
Convert a string expression to a ranges array.

File

includes/ExcelRange.inc, line 105

Class

ExcelRange

Code

function range_expression_to_range_array($range_expression) {

  // R1C1:R2C5
  $regexp1 = '&R([1-9][0-9]*)C([1-9][0-9]*)(' . EXCELRANGE_RANGE . 'R([1-9][0-9]*)C([1-9][0-9]*))?&is';

  // A1:E2
  $regexp2 = '&([A-Z]+)([1-9][0-9]*)(' . EXCELRANGE_RANGE . '([A-Z]+)([1-9][0-9]*))?&is';

  // A:E
  $regexp3 = '&([A-Z]+)(' . EXCELRANGE_RANGE . '([A-Z]+))?&is';

  // 1:2
  $regexp4 = '&([1-9][0-9]*)(' . EXCELRANGE_RANGE . '([1-9][0-9]*))?&is';
  $matches = array();

  // Row-Colum-style
  if (preg_match($regexp1, $range_expression, $matches)) {

    // Found a "to expression"
    if (isset($matches[3])) {
      $range = array(
        'rows' => array(
          $matches[1],
          $matches[4],
        ),
        'cols' => array(
          $matches[2],
          $matches[5],
        ),
      );
    }
    else {
      $range = array(
        'rows' => array(
          $matches[1],
          $matches[1],
        ),
        'cols' => array(
          $matches[2],
          $matches[2],
        ),
      );
    }
  }
  elseif (preg_match($regexp2, $range_expression, $matches)) {
    if (isset($matches[3])) {
      $range = array(
        'rows' => array(
          $matches[2],
          $matches[5],
        ),
        'cols' => array(
          $this
            ->alpha2num($matches[1]),
          $this
            ->alpha2num($matches[4]),
        ),
      );
    }
    else {
      $alpha = $this
        ->alpha2num($matches[1]);
      $range = array(
        'rows' => array(
          $matches[2],
          $matches[2],
        ),
        'cols' => array(
          $alpha,
          $alpha,
        ),
      );
    }
  }
  elseif (preg_match($regexp3, $range_expression, $matches)) {
    if (isset($matches[2])) {
      $range = array(
        'cols' => array(
          $this
            ->alpha2num($matches[1]),
          $this
            ->alpha2num($matches[3]),
        ),
      );
    }
    else {
      $alpha = $this
        ->alpha2num($matches[1]);
      $range = array(
        'cols' => array(
          $alpha,
          $alpha,
        ),
      );
    }
  }
  elseif (preg_match($regexp4, $range_expression, $matches)) {
    if (isset($matches[2])) {
      $range = array(
        'rows' => array(
          $matches[1],
          $matches[3],
        ),
      );
    }
    else {
      $range = array(
        'rows' => array(
          $matches[1],
          $matches[1],
        ),
      );
    }
  }

  // Check max rows and columns restictriction
  if (is_array($range)) {

    // max rows
    if ($this->max_rows) {

      // restric row to max rows count
      if (!isset($range['rows'])) {
        $range['rows'] = array(
          1,
          $this->max_rows,
        );
      }
      elseif ($range['rows'][0] > $this->max_rows) {
        return NULL;
      }
      elseif ($range['rows'][1] > $this->max_rows) {
        $range['rows'][1] = $this->max_rows;
      }
    }

    // max columns
    if ($this->max_cols) {

      // restric row to max rows count
      if (!isset($range['cols'])) {
        $range['cols'] = array(
          1,
          $this->max_cols,
        );
      }
      elseif ($range['cols'][0] > $this->max_cols) {
        return NULL;
      }
      elseif ($range['cols'][1] > $this->max_cols) {
        $range['cols'][1] = $this->max_cols;
      }
    }
  }
  return $range;
}