You are here

function sheetnode_html_import_table in Sheetnode 6

Same name and namespace in other branches
  1. 7.2 modules/sheetnode_html/sheetnode_html.module \sheetnode_html_import_table()
  2. 7 modules/sheetnode_html/sheetnode_html.module \sheetnode_html_import_table()

API function to import a single table.

1 call to sheetnode_html_import_table()
sheetnode_html_import in modules/sheetnode_html/sheetnode_html.module
API function to import a URL.

File

modules/sheetnode_html/sheetnode_html.module, line 99

Code

function sheetnode_html_import_table($table, &$sheet, $options = array()) {
  $cell = $cells = $spans = array();
  $pos = $maxpos = array(
    1,
    @$sheet['attribs']['lastrow'] + 1,
  );

  // col, row
  $rin = $pos[1];

  // input row
  foreach ($table
    ->find('tr') as $row) {
    $pos[0] = 1;
    $found_value = FALSE;
    foreach ($row
      ->find('td,th') as $element) {
      while (isset($spans[socialcalc_cr_to_coord($pos[0], $rin)])) {
        $pos[0]++;
      }
      $value = _sheetnode_html_import_value($element
        ->text());
      $cell = array();
      $cell['pos'] = $pos;
      $cell['datavalue'] = $value;
      $cell['datatype'] = is_numeric($value) ? 'v' : 't';
      $cell['valuetype'] = is_numeric($value) ? 'n' : 'th';
      $colspan = 1;
      if ($element
        ->attr('colspan') > 1) {
        $colspan = $element
          ->attr('colspan');
        if (empty($options['ignore_spans'])) {
          $cell['colspan'] = $colspan;
        }
      }
      $rowspan = 1;
      if ($element
        ->attr('rowspan') > 1) {
        $rowspan = $element
          ->attr('rowspan');
        if (empty($options['ignore_spans'])) {
          $cell['rowspan'] = $rowspan;
        }
      }
      if (!empty($value)) {
        $found_value = TRUE;
        $cells[socialcalc_cr_to_coord($pos[0], $pos[1])] = $cell;
      }
      for ($r = $rin + 1; $r < $rin + $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.
    $rin++;
    if ($found_value || empty($options['skip_empty_rows'])) {
      $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);
}