1
0
mirror of https://github.com/limosek/zaf-plugins.git synced 2025-11-01 18:17:37 +01:00

CSV fields are counted from 1

This commit is contained in:
2017-02-14 18:17:46 +01:00
parent c32c2bcca4
commit a41faae3f0
4 changed files with 126 additions and 48 deletions

View File

@@ -1,45 +1,50 @@
#!/usr/bin/php
<?php
require_once(__DIR__ . "/functions.inc.php");
require_once(__DIR__."/functions.inc.php");
$csv = getenv("csv");
$range = parse_colnum(getenv("columns"));
$rows = parse_colnum(getenv("rows"));
$rowsf = array_flip($rows);
$rangef = array_flip($range);
$head = getenv("header");
$delim = getenv("delimiter");
$csv=getenv("csv");
$range=parse_colnum(getenv("columns"));
$rows=parse_colnum(getenv("rows"));
$rowsf=array_flip($rows);
$rangef=array_flip($range);
$head=getenv("header");
$delim=getenv("delimiter");
$c=fopen($csv,"r");
$c = fopen($csv, "r");
$header = Array();
$i = 1;
if ($head) {
$header=fgetcsv($c,false,$delim);
$h = fgetcsv($c, false, $delim);
foreach ($h as $val) {
$header[$i] = $val;
$i++;
}
} else {
$header=Array();
$i=0;
foreach ($range as $r) {
$header[$i]=sprintf("FIELD%d",$i);
$i++;
}
foreach ($range as $r) {
$header[$i] = sprintf("FIELD%d", $i + 1);
$i++;
}
}
json_init();
$line=0;
$last=end($range);
$lastrow=max($rows);
$line = 0;
$last = end($range);
$lastrow = max($rows);
$numrows = intval(exec("wc -l '$csv'"));
while ($row=fgetcsv($c,false,$delim)) {
$line++;
if (!array_key_exists($line,$rowsf)) continue;
json_row();
json_column("ROW","$line");
foreach ($range as $num) {
$col=$num-1;
json_column("COLUMN$col",$header[$col]);
json_column("VALUE$col",addslashes($row[$col]),$last==$num);
}
json_row_end(feof($c)||$line==$lastrow);
while ($row = fgetcsv($c, false, $delim)) {
$line++;
if (!array_key_exists($line, $rowsf))
continue;
json_row();
json_column("ROW", "$line");
foreach ($range as $col) {
json_column("COLUMN$col", $header[$col]);
json_column("VALUE$col", addslashes($row[$col - 1]), $last == $col);
}
echo feof($c);
json_row_end(feof($c) || $line == $lastrow || $line == $numrows-1);
}
json_end();