You are here

function sheetnode_text_import_table in Sheetnode 7.2

Same name and namespace in other branches
  1. 6 modules/sheetnode_text/sheetnode_text.module \sheetnode_text_import_table()
  2. 7 modules/sheetnode_text/sheetnode_text.module \sheetnode_text_import_table()

API function to import a single table.

1 call to sheetnode_text_import_table()
sheetnode_text_import in modules/sheetnode_text/sheetnode_text.module
API function to import a URL.

File

modules/sheetnode_text/sheetnode_text.module, line 101
Module file for the sheetnode_text module.

Code

function sheetnode_text_import_table($table, &$sheet, $options = array()) {

  // 1. Split text into lines->ranges.
  // Consider row with maximum number of ranges = header.
  $rows = array();
  $maxcols = 0;
  foreach (preg_split("/(\r?\n)|<br\\s*\\/>/", $table) as $line) {
    $elements = preg_split("/\\s{2,}|\\|/", $line, -1, PREG_SPLIT_OFFSET_CAPTURE | PREG_SPLIT_NO_EMPTY);
    $row = array();
    foreach ($elements as $i => $element) {
      $text = $element[0];
      $start = $element[1];
      $row[] = array(
        'start' => $start,
        'end' => isset($elements[$i + 1]) ? $elements[$i + 1][1] - 1 : mb_strlen($line) - 1,
        'text' => $text,
      );
    }
    if (count($row) > $maxcols) {
      $maxcols = count($row);
      $header = $row;
    }
    $rows[] = $row;
  }

  // 2. Match ranges to cells.
  // col, row.
  $pos = $maxpos = array(
    1,
    @$sheet['attribs']['lastrow'] + 1,
  );
  $cell = $cells = $spans = array();
  foreach ($rows as $row) {
    foreach ($row as $range) {

      // Find column of this range.
      $pos[0] = count($header) - 1;
      foreach ($header as $c => $h) {
        if ($range['start'] <= $h['end']) {
          $pos[0] = $c + 1;
          break;
        }
      }

      // Find colspan of this range.
      $colspan = 1;
      for ($i = $c; $i < count($header); $i++) {
        $h = $i < count($header) - 1 ? $header[$i + 1] : NULL;
        if (empty($h)) {
          break;
        }
        if ($range['end'] < $h['start']) {
          break;
        }
        $colspan++;
      }
      $colspan = min($colspan, count($header));

      // TODO: Find rowspan of this range.
      $rowspan = 1;

      // Create cell.
      $value = _sheetnode_text_import_value($range['text']);
      if (!empty($options['process_dividers']) && preg_match('/^-+$/', $value)) {
        $value = NULL;
      }
      $cell = array();
      $cell['pos'] = $pos;
      $cell['datavalue'] = $value;
      $cell['datatype'] = is_numeric($value) ? 'v' : 't';
      $cell['valuetype'] = is_numeric($value) ? 'n' : 'th';
      if ($colspan > 1 && empty($options['ignore_spans'])) {
        $cell['colspan'] = $colspan;
      }
      if (!empty($value)) {
        $cells[socialcalc_cr_to_coord($pos[0], $pos[1])] = $cell;
      }
      for ($r = $pos[1]; $r < $pos[1] + $rowspan; $r++) {
        $spans[socialcalc_cr_to_coord($pos[0], $r)] = TRUE;
      }
      $pos[0] += $colspan;
      $maxpos[0] = max($maxpos[0], $pos[0]);
    }

    // Advance to next row.
    if ($pos[0] > 1) {
      $pos[1]++;
    }
    $maxpos[1] = max($maxpos[1], $pos[1]);
  }
  $sheet['cells'] = isset($sheet['cells']) ? $sheet['cells'] + $cells : $cells;
  $sheet['attribs']['lastcol'] = max(@$sheet['attribs']['lastcol'], $maxpos[0] - 1);
  $sheet['attribs']['lastrow'] = max(@$sheet['attribs']['lastrow'], $maxpos[1] - 1);
}