rev |
line source |
pascal@200
|
1 plugin="Calc"
|
pascal@203
|
2 description_fr="Feuille de calcul au format CSV"
|
pascal@200
|
3 description="CSV format spreadsheet"
|
pascal@200
|
4 help_fr="AideCalc"
|
pascal@200
|
5 help="HelpCalc"
|
pascal@200
|
6
|
pascal@200
|
7 case "$1" in
|
pascal@200
|
8 showhead) cat <<EOT
|
pascal@200
|
9 <!-- Based on http://jsfiddle.net/ondras/hYfN3/ by Ondřej Žára -->
|
pascal@200
|
10 <script type="text/javascript">
|
pascal@201
|
11 <!--
|
pascal@200
|
12 function csv(id,rows,cols) {
|
pascal@200
|
13 var data = "";
|
pascal@200
|
14 for (var i=1; i<=rows; i++) {
|
pascal@200
|
15 for (var j=1; j<=cols; j++) {
|
pascal@200
|
16 var letter = String.fromCharCode("A".charCodeAt(0)+j-1);
|
pascal@200
|
17 data += document.getElementById(id+letter+i).title+';';
|
pascal@200
|
18 }
|
pascal@249
|
19 data += "\\n";
|
pascal@200
|
20 }
|
pascal@200
|
21 alert(data);
|
pascal@200
|
22 }
|
pascal@200
|
23
|
pascal@238
|
24 function cnt(from,to) {
|
pascal@238
|
25 return (to.charCodeAt(0) - from.charCodeAt(0) + 1) *
|
pascal@238
|
26 (parseInt(to.substring(1)) - parseInt(from.substring(1)) + 1)
|
pascal@238
|
27 }
|
pascal@238
|
28
|
pascal@238
|
29 function zone(id,from,to,init,func) {
|
pascal@238
|
30 var result=init
|
pascal@237
|
31 for (var l=from.charCodeAt(0);;l++) {
|
pascal@237
|
32 for (var n=parseInt(from.substring(1));
|
pascal@237
|
33 n <= parseInt(to.substring(1));n++) {
|
pascal@237
|
34 var e=document.getElementById(id+String.fromCharCode(l)+n)
|
pascal@238
|
35 result=func(result,parseFloat(e.value))
|
pascal@237
|
36 }
|
pascal@237
|
37 if (l == to.charCodeAt(0)) break
|
pascal@237
|
38 }
|
pascal@237
|
39 return result;
|
pascal@237
|
40 }
|
pascal@238
|
41
|
pascal@238
|
42 var DATA={};
|
pascal@200
|
43 function buildCalc(id, rows, cols) {
|
pascal@200
|
44 DATA[id] = {};
|
pascal@203
|
45 var maths = [ "abs", "acos", "asin", "atan", "atan2", "ceil", "cos", "exp",
|
pascal@240
|
46 "floor", "log", "pow", "random", "round", "sin",
|
pascal@203
|
47 "tan", "sqrt", "PI", "E" ];
|
pascal@203
|
48 for (var i=0; v = maths[i]; i++)
|
pascal@203
|
49 eval("DATA[id]."+v+" = DATA[id]."+v.toUpperCase()+" = Math."+v);
|
pascal@203
|
50 DATA[id].rand = DATA[id].RAND = Math.random;
|
pascal@203
|
51 DATA[id].ln = DATA[id].LN = Math.log;
|
pascal@203
|
52 DATA[id].log10= DATA[id].LOG10= function(n){return Math.log(n)/Math.LN10;};
|
pascal@203
|
53 DATA[id].log2 = DATA[id].LOG2 = function(n){return Math.log(n)/Math.LN2;};
|
pascal@203
|
54 DATA[id].fact = DATA[id].FACT =
|
pascal@204
|
55 function(n){var x=1;while(n>1)x*=n--;return x;};
|
pascal@203
|
56 DATA[id].fib = DATA[id].FIB =
|
pascal@203
|
57 function(n){var c=0,p=1;while(n-->0){var x=c;c+=p;p=x};return c;};
|
pascal@238
|
58 DATA[id].sum = DATA[id].SUM =
|
pascal@238
|
59 function(a,b){return zone(id,a,b,0,function(a,b){return a+b});};
|
pascal@238
|
60 DATA[id].min = DATA[id].MIN =
|
pascal@238
|
61 function(a,b){return zone(id,a,b,Number.MAX_VALUE,Math.min);};
|
pascal@238
|
62 DATA[id].max = DATA[id].MAX =
|
pascal@238
|
63 function(a,b){return zone(id,a,b,Number.MIN_VALUE,Math.max);};
|
pascal@238
|
64 DATA[id].cnt = DATA[id].CNT = cnt
|
pascal@200
|
65 for (var i=0; i<=rows; i++) {
|
pascal@200
|
66 var row = document.getElementById(id).insertRow(-1);
|
pascal@200
|
67 for (var j=0; j<=cols && j<=26; j++) {
|
pascal@200
|
68 var letter = String.fromCharCode("A".charCodeAt(0)+j-1);
|
pascal@200
|
69 var cell = row.insertCell(-1);
|
pascal@200
|
70 if (i&&j) {
|
pascal@200
|
71 cell.className = "cellcalc";
|
pascal@200
|
72 cell.innerHTML = "<input id='"+ id+letter+i +"' class='inputcalc'/>";
|
pascal@200
|
73 }
|
pascal@200
|
74 else {
|
pascal@200
|
75 cell.className = "bordercalc";
|
pascal@200
|
76 cell.title = "Show CSV";
|
pascal@200
|
77 cell.onclick = function(){csv(id,rows,cols);};
|
pascal@200
|
78 cell.innerHTML = (i||j) ? i||letter : "√";
|
pascal@200
|
79 }
|
pascal@200
|
80 }
|
pascal@200
|
81 }
|
pascal@200
|
82 }
|
pascal@200
|
83
|
pascal@200
|
84 function getWidth(s)
|
pascal@200
|
85 {
|
pascal@200
|
86 var e = document.getElementById("widthcalc");
|
pascal@236
|
87 e.innerHTML = s;
|
pascal@240
|
88 return (e.offsetWidth < e.minWidth || s.charAt(0) == "=") ?
|
pascal@240
|
89 e.minWidth : e.offsetWidth;
|
pascal@200
|
90 }
|
pascal@200
|
91
|
pascal@200
|
92 function setCell(e, v)
|
pascal@200
|
93 {
|
pascal@200
|
94 e.style.width = getWidth(v)+"px";
|
pascal@200
|
95 e.style.textAlign =
|
pascal@200
|
96 (isNaN(parseFloat(v)) && v.charAt(0) != "=") ? "left" : "right";
|
pascal@200
|
97 e.title = v;
|
pascal@200
|
98 }
|
pascal@201
|
99 //-->
|
pascal@200
|
100 </script>
|
pascal@200
|
101 <span id="widthcalc" class="cellcalc" style="visibility:hidden;"></span>
|
pascal@200
|
102 EOT
|
pascal@200
|
103 exit 0 ;;
|
pascal@200
|
104 showtail) cat <<EOT
|
pascal@200
|
105 <script type="text/javascript">
|
pascal@201
|
106 <!--
|
pascal@200
|
107 var INPUTS=[].slice.call(document.getElementsByClassName("inputcalc"));
|
pascal@200
|
108 INPUTS.forEach(function(elm) {
|
pascal@200
|
109 elm.onfocus = function(e) {
|
pascal@200
|
110 e.target.value = e.target.title || "";
|
pascal@200
|
111 };
|
pascal@200
|
112 elm.onblur = function(e) {
|
pascal@200
|
113 setCell(e.target, e.target.value);
|
pascal@200
|
114 computeAll();
|
pascal@200
|
115 };
|
pascal@200
|
116 var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4);
|
pascal@200
|
117 var getter = function() {
|
pascal@200
|
118 var value = elm.title || "";
|
pascal@200
|
119 if (value.charAt(0) == "=")
|
pascal@200
|
120 with (DATA[calcid]) return eval(value.substring(1));
|
pascal@236
|
121 else return (value == "" || isNaN(value)) ? value : parseFloat(value);
|
pascal@200
|
122 };
|
pascal@200
|
123 Object.defineProperty(DATA[calcid], cellid, {get:getter});
|
pascal@200
|
124 Object.defineProperty(DATA[calcid], cellid.toLowerCase(), {get:getter});
|
pascal@200
|
125 });
|
pascal@200
|
126 (window.computeAll = function() {
|
pascal@200
|
127 INPUTS.forEach(function(elm) {
|
pascal@200
|
128 var calcid = elm.id.substring(0,4), cellid = elm.id.substring(4);
|
pascal@200
|
129 try { elm.value = DATA[calcid][cellid]; } catch(e) {} });
|
pascal@200
|
130 })();
|
pascal@201
|
131 //-->
|
pascal@200
|
132 </script>
|
pascal@200
|
133 EOT
|
pascal@200
|
134 exit 0 ;;
|
pascal@200
|
135 esac
|
pascal@200
|
136
|
pascal@200
|
137 formatEnd()
|
pascal@200
|
138 {
|
pascal@200
|
139 CONTENT=$(awk -v prg=$plugins_dir/wkp_$plugin.sh '
|
pascal@200
|
140 function showcalc()
|
pascal@200
|
141 {
|
pascal@200
|
142 if (lines > 1 && rows > 1) {
|
pascal@200
|
143 id="C" (100+cnt++)
|
pascal@203
|
144 print "<noscript><a href=\"http://www.enable-javascript.com/\" target=\"_blank\">Enable javascript to see the spreadsheet " id "</a></noscript>"
|
pascal@200
|
145 print "<table id=\"" id "\" class=\"tablecalc\"></table>"
|
pascal@200
|
146 print "<script type=\"text/javascript\">"
|
pascal@201
|
147 print "<!--"
|
pascal@200
|
148 print "buildCalc(\"" id "\"," lines "," rows ");"
|
pascal@200
|
149 for (i = 1; i <= lines; i++) {
|
pascal@200
|
150 gsub("<","<",line[i])
|
pascal@200
|
151 for (j = 1; j < split(line[i],tmp,";"); j++) {
|
pascal@200
|
152 if (tmp[j] == "") continue
|
pascal@251
|
153 gsub("\"","\\\"",tmp[j])
|
pascal@200
|
154 s = "setCell(document.getElementById(\"" id
|
pascal@200
|
155 c = substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",j,1)
|
pascal@200
|
156 print s c i "\"), \"" tmp[j] "\")";
|
pascal@200
|
157 }
|
pascal@200
|
158 }
|
pascal@201
|
159 print "//-->"
|
pascal@200
|
160 print "</script>"
|
pascal@200
|
161 }
|
pascal@200
|
162 else for (i = 1; i <= lines; i++) print line[i]
|
pascal@200
|
163 rows = lines = gotcalc = 0
|
pascal@200
|
164 }
|
pascal@200
|
165 {
|
pascal@200
|
166 if (/;<br \/>$/) {
|
pascal@200
|
167 gotcalc = 1
|
pascal@200
|
168 if (!headdone) {
|
pascal@200
|
169 headdone = 1
|
pascal@200
|
170 showtail = 1
|
pascal@200
|
171 system("/bin/sh " prg " showhead")
|
pascal@200
|
172 }
|
pascal@200
|
173 line[++lines] = $0
|
pascal@200
|
174 gsub("<","<",$0)
|
pascal@200
|
175 i = split($0,tmp,";")-1
|
pascal@200
|
176 if (lines == 1) rows = i
|
pascal@200
|
177 if (i != rows) rows = -1
|
pascal@200
|
178 }
|
pascal@200
|
179 else {
|
pascal@200
|
180 if (gotcalc) showcalc()
|
pascal@200
|
181 print
|
pascal@200
|
182 }
|
pascal@200
|
183 }
|
pascal@200
|
184 END {
|
pascal@200
|
185 if (gotcalc) showcalc()
|
pascal@200
|
186 if (showtail) system("/bin/sh " prg " showtail")
|
pascal@200
|
187 }
|
pascal@200
|
188 ' <<EOT
|
pascal@200
|
189 $CONTENT
|
pascal@200
|
190 EOT
|
pascal@200
|
191 )
|
pascal@200
|
192 }
|
pascal@200
|
193
|
pascal@200
|
194 template()
|
pascal@200
|
195 {
|
pascal@200
|
196 html=$(sed 's|</head>|\t<style type="text/css"> @import "plugins/wkp_Calc.css"; </style>\n&|' <<EOT
|
pascal@200
|
197 $html
|
pascal@200
|
198 EOT
|
pascal@200
|
199 )
|
pascal@200
|
200 }
|