You are here

function socialcalc_parse_sheet in Sheetnode 6

Same name and namespace in other branches
  1. 5 socialcalc.inc \socialcalc_parse_sheet()
  2. 7.2 socialcalc.inc \socialcalc_parse_sheet()
  3. 7 socialcalc.inc \socialcalc_parse_sheet()
3 calls to socialcalc_parse_sheet()
sheetnode_handler_field_named_range::render in views/sheetnode_handler_field_named_range.inc
sheetnode_handler_field_range::render in views/sheetnode_handler_field_range.inc
sheetnode_update_6002 in ./sheetnode.install
Implementation of hook_update_N().

File

./socialcalc.inc, line 139
SocialCalc manipulation functions Translated from socialcalc-3.js and companion files

Code

function socialcalc_parse_sheet($data) {
  $line = strtok($data, "\n");
  $sheet = array();
  while ($line !== FALSE) {
    $line = rtrim($line);
    $parts = explode(':', $line);
    switch ($parts[0]) {
      case 'cell':
        $coord = $parts[1];
        $cell = isset($sheet['cells'][$coord]) ? $sheet['cells'][$coord] : array(
          'pos' => socialcalc_coord_to_cr($coord),
        );
        $cell += socialcalc_parse_cell($parts, 2);
        $sheet['cells'][$coord] = $cell;
        break;
      case "col":
        $coord = $parts[1];
        $pos = socialcalc_coord_to_cr($coord . '1');

        // convert to col number
        $j = 2;
        while ($t = @$parts[$j++]) {
          switch ($t) {
            case "w":
              $sheet['colattribs']['width'][$pos[0]] = strval($parts[$j++]);

              // must be text - could be auto or %, etc.
              break;
            case "hide":
              $sheet['colattribs']['hide'][$pos[0]] = $parts[$j++];
              break;
          }
        }
        break;
      case "row":
        $coord = intval($parts[1]);
        $j = 2;
        while ($t = @$parts[$j++]) {
          switch ($t) {
            case "h":
              $sheet['rowattribs']['height'][$coord] = intval($parts[$j++]);
              break;
            case "hide":
              $sheet['rowattribs']['hide'][$coord] = $parts[$j++];
              break;
          }
        }
        break;
      case "sheet":
        $j = 1;
        while ($t = @$parts[$j++]) {
          switch ($t) {
            case "c":
              $sheet['attribs']['lastcol'] = intval($parts[$j++]);
              break;
            case "r":
              $sheet['attribs']['lastrow'] = intval($parts[$j++]);
              break;
            case "w":
              $sheet['attribs']['defaultcolwidth'] = intval($parts[$j++]);
              break;
            case "h":
              $sheet['attribs']['defaultrowheight'] = intval($parts[$j++]);
              break;
            case "tf":
              $sheet['attribs']['defaulttextformat'] = intval($parts[$j++]);
              break;
            case "ntf":
              $sheet['attribs']['defaultnontextformat'] = intval($parts[$j++]);
              break;
            case "layout":
              $sheet['attribs']['defaultlayout'] = intval($parts[$j++]);
              break;
            case "font":
              $sheet['attribs']['defaultfont'] = intval($parts[$j++]);
              break;
            case "tvf":
              $sheet['attribs']['defaulttextvalueformat'] = intval($parts[$j++]);
              break;
            case "ntvf":
              $sheet['attribs']['defaultnontextvalueformat'] = intval($parts[$j++]);
              break;
            case "color":
              $sheet['attribs']['defaultcolor'] = intval($parts[$j++]);
              break;
            case "bgcolor":
              $sheet['attribs']['defaultbgcolor'] = intval($parts[$j++]);
              break;
            case "circularreferencecell":
              $sheet['attribs']['circularreferencecell'] = $parts[$j++];
              break;
            case "recalc":
              $sheet['attribs']['recalc'] = $parts[$j++];
              break;
            case "needsrecalc":
              $sheet['attribs']['needsrecalc'] = $parts[$j++];
              break;
            case "usermaxcol":
              $sheet['attribs']['usermaxcol'] = intval($parts[$j++]);
              break;
            case "usermaxrow":
              $sheet['attribs']['usermaxrow'] = intval($parts[$j++]);
              break;
            default:
              $j += 1;
              break;
          }
        }
        break;
      case "name":
        $name = strtoupper(socialcalc_decode_value($parts[1]));
        $sheet['names'][$name] = array(
          'desc' => socialcalc_decode_value($parts[2]),
          'definition' => socialcalc_decode_value($parts[3]),
        );
        break;
      case "layout":
        $parts = array();
        preg_match('/^layout\\:(\\d+)\\:(.+)$/', $line, $parts);

        // layouts can have ":" in them
        $sheet['layouts'][intval($parts[1])] = $parts[2];
        $sheet['layouthash'][$parts[2]] = intval($parts[1]);
        break;
      case "font":
        $sheet['fonts'][intval($parts[1])] = $parts[2];
        $sheet['fonthash'][$parts[2]] = intval($parts[1]);
        break;
      case "color":
        $sheet['colors'][intval($parts[1])] = $parts[2];
        $sheet['colorhash'][$parts[2]] = intval($parts[1]);
        break;
      case "border":
        $sheet['borderstyles'][intval($parts[1])] = $parts[2];
        $sheet['borderstylehash'][$parts[2]] = intval($parts[1]);
        break;
      case "cellformat":
        $sheet['cellformats'][intval($parts[1])] = $parts[2];
        $sheet['cellformathash'][$parts[2]] = intval($parts[1]);
        break;
      case "valueformat":
        $v = socialcalc_decode_value($parts[2]);
        $sheet['valueformats'][intval($parts[1])] = $v;
        $sheet['valueformathash'][$v] = intval($parts[1]);
        break;
      case "version":
        break;
      case "copiedfrom":
        $sheet['copiedfrom'] = $parts[1] . ':' . $parts[2];
        break;
      case "clipboardrange":

      // in save versions up to 1.3. Ignored.
      case "clipboard":
        break;
      case "":
        break;
      default:
        break;
    }
    $line = strtok("\n");
  }
  return $sheet;
}