You are here

function socialcalc_parse_csv in Sheetnode 6

Same name and namespace in other branches
  1. 7.2 socialcalc.inc \socialcalc_parse_csv()
  2. 7 socialcalc.inc \socialcalc_parse_csv()

File

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

Code

function socialcalc_parse_csv($file) {
  $sc = array(
    'sheet' => array(),
    'edit' => socialcalc_default_edit(),
    'audit' => socialcalc_default_audit(),
  );
  if ($handle = fopen($file, 'r')) {
    $r = 1;
    while ($row = fgetcsv($handle)) {
      $c = 1;
      foreach ($row as $value) {
        $cell = array(
          'pos' => array(
            $c,
            $r,
          ),
          'datatype' => is_numeric($value) ? 'v' : 't',
          'valuetype' => is_numeric($value) ? 'n' : 't',
          // TODO: Can do better at inferring valuetypes
          'datavalue' => $value,
        );
        $sc['sheet']['cells'][] = $cell;
        $c++;
      }
      $r++;
    }
    fclose($handle);
  }
  return $sc;
}