ssf.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /* ssf.js (C) 2013-2014 SheetJS -- http://sheetjs.com */
  2. /*jshint -W041 */
  3. var SSF = {};
  4. var make_ssf = function make_ssf(SSF){
  5. SSF.version = '0.8.2';
  6. function _strrev(x) { var o = "", i = x.length-1; while(i>=0) o += x.charAt(i--); return o; }
  7. function fill(c,l) { var o = ""; while(o.length < l) o+=c; return o; }
  8. function pad0(v,d){var t=""+v; return t.length>=d?t:fill('0',d-t.length)+t;}
  9. function pad_(v,d){var t=""+v;return t.length>=d?t:fill(' ',d-t.length)+t;}
  10. function rpad_(v,d){var t=""+v; return t.length>=d?t:t+fill(' ',d-t.length);}
  11. function pad0r1(v,d){var t=""+Math.round(v); return t.length>=d?t:fill('0',d-t.length)+t;}
  12. function pad0r2(v,d){var t=""+v; return t.length>=d?t:fill('0',d-t.length)+t;}
  13. var p2_32 = Math.pow(2,32);
  14. function pad0r(v,d){if(v>p2_32||v<-p2_32) return pad0r1(v,d); var i = Math.round(v); return pad0r2(i,d); }
  15. function isgeneral(s, i) { return s.length >= 7 + i && (s.charCodeAt(i)|32) === 103 && (s.charCodeAt(i+1)|32) === 101 && (s.charCodeAt(i+2)|32) === 110 && (s.charCodeAt(i+3)|32) === 101 && (s.charCodeAt(i+4)|32) === 114 && (s.charCodeAt(i+5)|32) === 97 && (s.charCodeAt(i+6)|32) === 108; }
  16. /* Options */
  17. var opts_fmt = [
  18. ["date1904", 0],
  19. ["output", ""],
  20. ["WTF", false]
  21. ];
  22. function fixopts(o){
  23. for(var y = 0; y != opts_fmt.length; ++y) if(o[opts_fmt[y][0]]===undefined) o[opts_fmt[y][0]]=opts_fmt[y][1];
  24. }
  25. SSF.opts = opts_fmt;
  26. var table_fmt = {
  27. 0: 'General',
  28. 1: '0',
  29. 2: '0.00',
  30. 3: '#,##0',
  31. 4: '#,##0.00',
  32. 9: '0%',
  33. 10: '0.00%',
  34. 11: '0.00E+00',
  35. 12: '# ?/?',
  36. 13: '# ??/??',
  37. 14: 'm/d/yy',
  38. 15: 'd-mmm-yy',
  39. 16: 'd-mmm',
  40. 17: 'mmm-yy',
  41. 18: 'h:mm AM/PM',
  42. 19: 'h:mm:ss AM/PM',
  43. 20: 'h:mm',
  44. 21: 'h:mm:ss',
  45. 22: 'm/d/yy h:mm',
  46. 37: '#,##0 ;(#,##0)',
  47. 38: '#,##0 ;[Red](#,##0)',
  48. 39: '#,##0.00;(#,##0.00)',
  49. 40: '#,##0.00;[Red](#,##0.00)',
  50. 45: 'mm:ss',
  51. 46: '[h]:mm:ss',
  52. 47: 'mmss.0',
  53. 48: '##0.0E+0',
  54. 49: '@',
  55. 56: '"上午/下午 "hh"時"mm"分"ss"秒 "',
  56. 65535: 'General'
  57. };
  58. var days = [
  59. ['Sun', 'Sunday'],
  60. ['Mon', 'Monday'],
  61. ['Tue', 'Tuesday'],
  62. ['Wed', 'Wednesday'],
  63. ['Thu', 'Thursday'],
  64. ['Fri', 'Friday'],
  65. ['Sat', 'Saturday']
  66. ];
  67. var months = [
  68. ['J', 'Jan', 'January'],
  69. ['F', 'Feb', 'February'],
  70. ['M', 'Mar', 'March'],
  71. ['A', 'Apr', 'April'],
  72. ['M', 'May', 'May'],
  73. ['J', 'Jun', 'June'],
  74. ['J', 'Jul', 'July'],
  75. ['A', 'Aug', 'August'],
  76. ['S', 'Sep', 'September'],
  77. ['O', 'Oct', 'October'],
  78. ['N', 'Nov', 'November'],
  79. ['D', 'Dec', 'December']
  80. ];
  81. function frac(x, D, mixed) {
  82. var sgn = x < 0 ? -1 : 1;
  83. var B = x * sgn;
  84. var P_2 = 0, P_1 = 1, P = 0;
  85. var Q_2 = 1, Q_1 = 0, Q = 0;
  86. var A = Math.floor(B);
  87. while(Q_1 < D) {
  88. A = Math.floor(B);
  89. P = A * P_1 + P_2;
  90. Q = A * Q_1 + Q_2;
  91. if((B - A) < 0.0000000005) break;
  92. B = 1 / (B - A);
  93. P_2 = P_1; P_1 = P;
  94. Q_2 = Q_1; Q_1 = Q;
  95. }
  96. if(Q > D) { Q = Q_1; P = P_1; }
  97. if(Q > D) { Q = Q_2; P = P_2; }
  98. if(!mixed) return [0, sgn * P, Q];
  99. if(Q===0) throw "Unexpected state: "+P+" "+P_1+" "+P_2+" "+Q+" "+Q_1+" "+Q_2;
  100. var q = Math.floor(sgn * P/Q);
  101. return [q, sgn*P - q*Q, Q];
  102. }
  103. function general_fmt_int(v, opts) { return ""+v; }
  104. SSF._general_int = general_fmt_int;
  105. var general_fmt_num = (function make_general_fmt_num() {
  106. var gnr1 = /\.(\d*[1-9])0+$/, gnr2 = /\.0*$/, gnr4 = /\.(\d*[1-9])0+/, gnr5 = /\.0*[Ee]/, gnr6 = /(E[+-])(\d)$/;
  107. function gfn2(v) {
  108. var w = (v<0?12:11);
  109. var o = gfn5(v.toFixed(12)); if(o.length <= w) return o;
  110. o = v.toPrecision(10); if(o.length <= w) return o;
  111. return v.toExponential(5);
  112. }
  113. function gfn3(v) {
  114. var o = v.toFixed(11).replace(gnr1,".$1");
  115. if(o.length > (v<0?12:11)) o = v.toPrecision(6);
  116. return o;
  117. }
  118. function gfn4(o) {
  119. for(var i = 0; i != o.length; ++i) if((o.charCodeAt(i) | 0x20) === 101) return o.replace(gnr4,".$1").replace(gnr5,"E").replace("e","E").replace(gnr6,"$10$2");
  120. return o;
  121. }
  122. function gfn5(o) {
  123. //for(var i = 0; i != o.length; ++i) if(o.charCodeAt(i) === 46) return o.replace(gnr2,"").replace(gnr1,".$1");
  124. //return o;
  125. return o.indexOf(".") > -1 ? o.replace(gnr2,"").replace(gnr1,".$1") : o;
  126. }
  127. return function general_fmt_num(v, opts) {
  128. var V = Math.floor(Math.log(Math.abs(v))*Math.LOG10E), o;
  129. if(V >= -4 && V <= -1) o = v.toPrecision(10+V);
  130. else if(Math.abs(V) <= 9) o = gfn2(v);
  131. else if(V === 10) o = v.toFixed(10).substr(0,12);
  132. else o = gfn3(v);
  133. return gfn5(gfn4(o));
  134. };})();
  135. SSF._general_num = general_fmt_num;
  136. function general_fmt(v, opts) {
  137. switch(typeof v) {
  138. case 'string': return v;
  139. case 'boolean': return v ? "TRUE" : "FALSE";
  140. case 'number': return (v|0) === v ? general_fmt_int(v, opts) : general_fmt_num(v, opts);
  141. }
  142. throw new Error("unsupported value in General format: " + v);
  143. }
  144. SSF._general = general_fmt;
  145. function fix_hijri(date, o) { return 0; }
  146. function parse_date_code(v,opts,b2) {
  147. if(v > 2958465 || v < 0) return null;
  148. var date = (v|0), time = Math.floor(86400 * (v - date)), dow=0;
  149. var dout=[];
  150. var out={D:date, T:time, u:86400*(v-date)-time,y:0,m:0,d:0,H:0,M:0,S:0,q:0};
  151. if(Math.abs(out.u) < 1e-6) out.u = 0;
  152. fixopts(opts != null ? opts : (opts=[]));
  153. if(opts.date1904) date += 1462;
  154. if(out.u > 0.999) {
  155. out.u = 0;
  156. if(++time == 86400) { time = 0; ++date; }
  157. }
  158. if(date === 60) {dout = b2 ? [1317,10,29] : [1900,2,29]; dow=3;}
  159. else if(date === 0) {dout = b2 ? [1317,8,29] : [1900,1,0]; dow=6;}
  160. else {
  161. if(date > 60) --date;
  162. /* 1 = Jan 1 1900 */
  163. var d = new Date(1900,0,1);
  164. d.setDate(d.getDate() + date - 1);
  165. dout = [d.getFullYear(), d.getMonth()+1,d.getDate()];
  166. dow = d.getDay();
  167. if(date < 60) dow = (dow + 6) % 7;
  168. if(b2) dow = fix_hijri(d, dout);
  169. }
  170. out.y = dout[0]; out.m = dout[1]; out.d = dout[2];
  171. out.S = time % 60; time = Math.floor(time / 60);
  172. out.M = time % 60; time = Math.floor(time / 60);
  173. out.H = time;
  174. out.q = dow;
  175. return out;
  176. }
  177. SSF.parse_date_code = parse_date_code;
  178. /*jshint -W086 */
  179. function write_date(type, fmt, val, ss0) {
  180. var o="", ss=0, tt=0, y = val.y, out, outl = 0;
  181. switch(type) {
  182. case 98: /* 'b' buddhist year */
  183. y = val.y + 543;
  184. /* falls through */
  185. case 121: /* 'y' year */
  186. switch(fmt.length) {
  187. case 1: case 2: out = y % 100; outl = 2; break;
  188. default: out = y % 10000; outl = 4; break;
  189. } break;
  190. case 109: /* 'm' month */
  191. switch(fmt.length) {
  192. case 1: case 2: out = val.m; outl = fmt.length; break;
  193. case 3: return months[val.m-1][1];
  194. case 5: return months[val.m-1][0];
  195. default: return months[val.m-1][2];
  196. } break;
  197. case 100: /* 'd' day */
  198. switch(fmt.length) {
  199. case 1: case 2: out = val.d; outl = fmt.length; break;
  200. case 3: return days[val.q][0];
  201. default: return days[val.q][1];
  202. } break;
  203. case 104: /* 'h' 12-hour */
  204. switch(fmt.length) {
  205. case 1: case 2: out = 1+(val.H+11)%12; outl = fmt.length; break;
  206. default: throw 'bad hour format: ' + fmt;
  207. } break;
  208. case 72: /* 'H' 24-hour */
  209. switch(fmt.length) {
  210. case 1: case 2: out = val.H; outl = fmt.length; break;
  211. default: throw 'bad hour format: ' + fmt;
  212. } break;
  213. case 77: /* 'M' minutes */
  214. switch(fmt.length) {
  215. case 1: case 2: out = val.M; outl = fmt.length; break;
  216. default: throw 'bad minute format: ' + fmt;
  217. } break;
  218. case 115: /* 's' seconds */
  219. if(val.u === 0) switch(fmt) {
  220. case 's': case 'ss': return pad0(val.S, fmt.length);
  221. case '.0': case '.00': case '.000':
  222. }
  223. switch(fmt) {
  224. case 's': case 'ss': case '.0': case '.00': case '.000':
  225. if(ss0 >= 2) tt = ss0 === 3 ? 1000 : 100;
  226. else tt = ss0 === 1 ? 10 : 1;
  227. ss = Math.round((tt)*(val.S + val.u));
  228. if(ss >= 60*tt) ss = 0;
  229. if(fmt === 's') return ss === 0 ? "0" : ""+ss/tt;
  230. o = pad0(ss,2 + ss0);
  231. if(fmt === 'ss') return o.substr(0,2);
  232. return "." + o.substr(2,fmt.length-1);
  233. default: throw 'bad second format: ' + fmt;
  234. }
  235. case 90: /* 'Z' absolute time */
  236. switch(fmt) {
  237. case '[h]': case '[hh]': out = val.D*24+val.H; break;
  238. case '[m]': case '[mm]': out = (val.D*24+val.H)*60+val.M; break;
  239. case '[s]': case '[ss]': out = ((val.D*24+val.H)*60+val.M)*60+Math.round(val.S+val.u); break;
  240. default: throw 'bad abstime format: ' + fmt;
  241. } outl = fmt.length === 3 ? 1 : 2; break;
  242. case 101: /* 'e' era */
  243. out = y; outl = 1;
  244. }
  245. if(outl > 0) return pad0(out, outl); else return "";
  246. }
  247. /*jshint +W086 */
  248. function commaify(s) {
  249. if(s.length <= 3) return s;
  250. var j = (s.length % 3), o = s.substr(0,j);
  251. for(; j!=s.length; j+=3) o+=(o.length > 0 ? "," : "") + s.substr(j,3);
  252. return o;
  253. }
  254. var write_num = (function make_write_num(){
  255. var pct1 = /%/g;
  256. function write_num_pct(type, fmt, val){
  257. var sfmt = fmt.replace(pct1,""), mul = fmt.length - sfmt.length;
  258. return write_num(type, sfmt, val * Math.pow(10,2*mul)) + fill("%",mul);
  259. }
  260. function write_num_cm(type, fmt, val){
  261. var idx = fmt.length - 1;
  262. while(fmt.charCodeAt(idx-1) === 44) --idx;
  263. return write_num(type, fmt.substr(0,idx), val / Math.pow(10,3*(fmt.length-idx)));
  264. }
  265. function write_num_exp(fmt, val){
  266. var o;
  267. var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1;
  268. if(fmt.match(/^#+0.0E\+0$/)) {
  269. var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E');
  270. var ee = Math.floor(Math.log(Math.abs(val))*Math.LOG10E)%period;
  271. if(ee < 0) ee += period;
  272. o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period);
  273. if(o.indexOf("e") === -1) {
  274. var fakee = Math.floor(Math.log(Math.abs(val))*Math.LOG10E);
  275. if(o.indexOf(".") === -1) o = o[0] + "." + o.substr(1) + "E+" + (fakee - o.length+ee);
  276. else o += "E+" + (fakee - ee);
  277. while(o.substr(0,2) === "0.") {
  278. o = o[0] + o.substr(2,period) + "." + o.substr(2+period);
  279. o = o.replace(/^0+([1-9])/,"$1").replace(/^0+\./,"0.");
  280. }
  281. o = o.replace(/\+-/,"-");
  282. }
  283. o = o.replace(/^([+-]?)(\d*)\.(\d*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; });
  284. } else o = val.toExponential(idx);
  285. if(fmt.match(/E\+00$/) && o.match(/e[+-]\d$/)) o = o.substr(0,o.length-1) + "0" + o[o.length-1];
  286. if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e");
  287. return o.replace("e","E");
  288. }
  289. var frac1 = /# (\?+)( ?)\/( ?)(\d+)/;
  290. function write_num_f1(r, aval, sign) {
  291. var den = parseInt(r[4]), rr = Math.round(aval * den), base = Math.floor(rr/den);
  292. var myn = (rr - base*den), myd = den;
  293. return sign + (base === 0 ? "" : ""+base) + " " + (myn === 0 ? fill(" ", r[1].length + 1 + r[4].length) : pad_(myn,r[1].length) + r[2] + "/" + r[3] + pad0(myd,r[4].length));
  294. }
  295. function write_num_f2(r, aval, sign) {
  296. return sign + (aval === 0 ? "" : ""+aval) + fill(" ", r[1].length + 2 + r[4].length);
  297. }
  298. var dec1 = /^#*0*\.(0+)/;
  299. var closeparen = /\).*[0#]/;
  300. var phone = /\(###\) ###\\?-####/;
  301. function hashq(str) {
  302. var o = "", cc;
  303. for(var i = 0; i != str.length; ++i) switch((cc=str.charCodeAt(i))) {
  304. case 35: break;
  305. case 63: o+= " "; break;
  306. case 48: o+= "0"; break;
  307. default: o+= String.fromCharCode(cc);
  308. }
  309. return o;
  310. }
  311. function rnd(val, d) { var dd = Math.pow(10,d); return ""+(Math.round(val * dd)/dd); }
  312. function dec(val, d) { return Math.round((val-Math.floor(val))*Math.pow(10,d)); }
  313. function flr(val) { if(val < 2147483647 && val > -2147483648) return ""+(val >= 0 ? (val|0) : (val-1|0)); return ""+Math.floor(val); }
  314. function write_num_flt(type, fmt, val) {
  315. if(type.charCodeAt(0) === 40 && !fmt.match(closeparen)) {
  316. var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,"");
  317. if(val >= 0) return write_num_flt('n', ffmt, val);
  318. return '(' + write_num_flt('n', ffmt, -val) + ')';
  319. }
  320. if(fmt.charCodeAt(fmt.length - 1) === 44) return write_num_cm(type, fmt, val);
  321. if(fmt.indexOf('%') !== -1) return write_num_pct(type, fmt, val);
  322. if(fmt.indexOf('E') !== -1) return write_num_exp(fmt, val);
  323. if(fmt.charCodeAt(0) === 36) return "$"+write_num_flt(type,fmt.substr(fmt[1]==' '?2:1),val);
  324. var o, oo;
  325. var r, ri, ff, aval = Math.abs(val), sign = val < 0 ? "-" : "";
  326. if(fmt.match(/^00+$/)) return sign + pad0r(aval,fmt.length);
  327. if(fmt.match(/^[#?]+$/)) {
  328. o = pad0r(val,0); if(o === "0") o = "";
  329. return o.length > fmt.length ? o : hashq(fmt.substr(0,fmt.length-o.length)) + o;
  330. }
  331. if((r = fmt.match(frac1)) !== null) return write_num_f1(r, aval, sign);
  332. if(fmt.match(/^#+0+$/) !== null) return sign + pad0r(aval,fmt.length - fmt.indexOf("0"));
  333. if((r = fmt.match(dec1)) !== null) {
  334. o = rnd(val, r[1].length).replace(/^([^\.]+)$/,"$1."+r[1]).replace(/\.$/,"."+r[1]).replace(/\.(\d*)$/,function($$, $1) { return "." + $1 + fill("0", r[1].length-$1.length); });
  335. return fmt.indexOf("0.") !== -1 ? o : o.replace(/^0\./,".");
  336. }
  337. fmt = fmt.replace(/^#+([0.])/, "$1");
  338. if((r = fmt.match(/^(0*)\.(#*)$/)) !== null) {
  339. return sign + rnd(aval, r[2].length).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^(-?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":".");
  340. }
  341. if((r = fmt.match(/^#,##0(\.?)$/)) !== null) return sign + commaify(pad0r(aval,0));
  342. if((r = fmt.match(/^#,##0\.([#0]*0)$/)) !== null) {
  343. return val < 0 ? "-" + write_num_flt(type, fmt, -val) : commaify(""+(Math.floor(val))) + "." + pad0(dec(val, r[1].length),r[1].length);
  344. }
  345. if((r = fmt.match(/^#,#*,#0/)) !== null) return write_num_flt(type,fmt.replace(/^#,#*,/,""),val);
  346. if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/)) !== null) {
  347. o = _strrev(write_num_flt(type, fmt.replace(/[\\-]/g,""), val));
  348. ri = 0;
  349. return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return ri<o.length?o[ri++]:x==='0'?'0':"";}));
  350. }
  351. if(fmt.match(phone) !== null) {
  352. o = write_num_flt(type, "##########", val);
  353. return "(" + o.substr(0,3) + ") " + o.substr(3, 3) + "-" + o.substr(6);
  354. }
  355. var oa = "";
  356. if((r = fmt.match(/^([#0?]+)( ?)\/( ?)([#0?]+)/)) !== null) {
  357. ri = Math.min(r[4].length,7);
  358. ff = frac(aval, Math.pow(10,ri)-1, false);
  359. o = "" + sign;
  360. oa = write_num("n", r[1], ff[1]);
  361. if(oa[oa.length-1] == " ") oa = oa.substr(0,oa.length-1) + "0";
  362. o += oa + r[2] + "/" + r[3];
  363. oa = rpad_(ff[2],ri);
  364. if(oa.length < r[4].length) oa = hashq(r[4].substr(r[4].length-oa.length)) + oa;
  365. o += oa;
  366. return o;
  367. }
  368. if((r = fmt.match(/^# ([#0?]+)( ?)\/( ?)([#0?]+)/)) !== null) {
  369. ri = Math.min(Math.max(r[1].length, r[4].length),7);
  370. ff = frac(aval, Math.pow(10,ri)-1, true);
  371. return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad_(ff[1],ri) + r[2] + "/" + r[3] + rpad_(ff[2],ri): fill(" ", 2*ri+1 + r[2].length + r[3].length));
  372. }
  373. if((r = fmt.match(/^[#0?]+$/)) !== null) {
  374. o = pad0r(val, 0);
  375. if(fmt.length <= o.length) return o;
  376. return hashq(fmt.substr(0,fmt.length-o.length)) + o;
  377. }
  378. if((r = fmt.match(/^([#0?]+)\.([#0]+)$/)) !== null) {
  379. o = "" + val.toFixed(Math.min(r[2].length,10)).replace(/([^0])0+$/,"$1");
  380. ri = o.indexOf(".");
  381. var lres = fmt.indexOf(".") - ri, rres = fmt.length - o.length - lres;
  382. return hashq(fmt.substr(0,lres) + o + fmt.substr(fmt.length-rres));
  383. }
  384. if((r = fmt.match(/^00,000\.([#0]*0)$/)) !== null) {
  385. ri = dec(val, r[1].length);
  386. return val < 0 ? "-" + write_num_flt(type, fmt, -val) : commaify(flr(val)).replace(/^\d,\d{3}$/,"0$&").replace(/^\d*$/,function($$) { return "00," + ($$.length < 3 ? pad0(0,3-$$.length) : "") + $$; }) + "." + pad0(ri,r[1].length);
  387. }
  388. switch(fmt) {
  389. case "#,###": var x = commaify(pad0r(aval,0)); return x !== "0" ? sign + x : "";
  390. default:
  391. }
  392. throw new Error("unsupported format |" + fmt + "|");
  393. }
  394. function write_num_cm2(type, fmt, val){
  395. var idx = fmt.length - 1;
  396. while(fmt.charCodeAt(idx-1) === 44) --idx;
  397. return write_num(type, fmt.substr(0,idx), val / Math.pow(10,3*(fmt.length-idx)));
  398. }
  399. function write_num_pct2(type, fmt, val){
  400. var sfmt = fmt.replace(pct1,""), mul = fmt.length - sfmt.length;
  401. return write_num(type, sfmt, val * Math.pow(10,2*mul)) + fill("%",mul);
  402. }
  403. function write_num_exp2(fmt, val){
  404. var o;
  405. var idx = fmt.indexOf("E") - fmt.indexOf(".") - 1;
  406. if(fmt.match(/^#+0.0E\+0$/)) {
  407. var period = fmt.indexOf("."); if(period === -1) period=fmt.indexOf('E');
  408. var ee = Math.floor(Math.log(Math.abs(val))*Math.LOG10E)%period;
  409. if(ee < 0) ee += period;
  410. o = (val/Math.pow(10,ee)).toPrecision(idx+1+(period+ee)%period);
  411. if(!o.match(/[Ee]/)) {
  412. var fakee = Math.floor(Math.log(Math.abs(val))*Math.LOG10E);
  413. if(o.indexOf(".") === -1) o = o[0] + "." + o.substr(1) + "E+" + (fakee - o.length+ee);
  414. else o += "E+" + (fakee - ee);
  415. o = o.replace(/\+-/,"-");
  416. }
  417. o = o.replace(/^([+-]?)(\d*)\.(\d*)[Ee]/,function($$,$1,$2,$3) { return $1 + $2 + $3.substr(0,(period+ee)%period) + "." + $3.substr(ee) + "E"; });
  418. } else o = val.toExponential(idx);
  419. if(fmt.match(/E\+00$/) && o.match(/e[+-]\d$/)) o = o.substr(0,o.length-1) + "0" + o[o.length-1];
  420. if(fmt.match(/E\-/) && o.match(/e\+/)) o = o.replace(/e\+/,"e");
  421. return o.replace("e","E");
  422. }
  423. function write_num_int(type, fmt, val) {
  424. if(type.charCodeAt(0) === 40 && !fmt.match(closeparen)) {
  425. var ffmt = fmt.replace(/\( */,"").replace(/ \)/,"").replace(/\)/,"");
  426. if(val >= 0) return write_num_int('n', ffmt, val);
  427. return '(' + write_num_int('n', ffmt, -val) + ')';
  428. }
  429. if(fmt.charCodeAt(fmt.length - 1) === 44) return write_num_cm2(type, fmt, val);
  430. if(fmt.indexOf('%') !== -1) return write_num_pct2(type, fmt, val);
  431. if(fmt.indexOf('E') !== -1) return write_num_exp2(fmt, val);
  432. if(fmt.charCodeAt(0) === 36) return "$"+write_num_int(type,fmt.substr(fmt[1]==' '?2:1),val);
  433. var o;
  434. var r, ri, ff, aval = Math.abs(val), sign = val < 0 ? "-" : "";
  435. if(fmt.match(/^00+$/)) return sign + pad0(aval,fmt.length);
  436. if(fmt.match(/^[#?]+$/)) {
  437. o = (""+val); if(val === 0) o = "";
  438. return o.length > fmt.length ? o : hashq(fmt.substr(0,fmt.length-o.length)) + o;
  439. }
  440. if((r = fmt.match(frac1)) !== null) return write_num_f2(r, aval, sign);
  441. if(fmt.match(/^#+0+$/) !== null) return sign + pad0(aval,fmt.length - fmt.indexOf("0"));
  442. if((r = fmt.match(dec1)) !== null) {
  443. o = (""+val).replace(/^([^\.]+)$/,"$1."+r[1]).replace(/\.$/,"."+r[1]).replace(/\.(\d*)$/,function($$, $1) { return "." + $1 + fill("0", r[1].length-$1.length); });
  444. return fmt.indexOf("0.") !== -1 ? o : o.replace(/^0\./,".");
  445. }
  446. fmt = fmt.replace(/^#+([0.])/, "$1");
  447. if((r = fmt.match(/^(0*)\.(#*)$/)) !== null) {
  448. return sign + (""+aval).replace(/\.(\d*[1-9])0*$/,".$1").replace(/^(-?\d*)$/,"$1.").replace(/^0\./,r[1].length?"0.":".");
  449. }
  450. if((r = fmt.match(/^#,##0(\.?)$/)) !== null) return sign + commaify((""+aval));
  451. if((r = fmt.match(/^#,##0\.([#0]*0)$/)) !== null) {
  452. return val < 0 ? "-" + write_num_int(type, fmt, -val) : commaify((""+val)) + "." + fill('0',r[1].length);
  453. }
  454. if((r = fmt.match(/^#,#*,#0/)) !== null) return write_num_int(type,fmt.replace(/^#,#*,/,""),val);
  455. if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/)) !== null) {
  456. o = _strrev(write_num_int(type, fmt.replace(/[\\-]/g,""), val));
  457. ri = 0;
  458. return _strrev(_strrev(fmt.replace(/\\/g,"")).replace(/[0#]/g,function(x){return ri<o.length?o[ri++]:x==='0'?'0':"";}));
  459. }
  460. if(fmt.match(phone) !== null) {
  461. o = write_num_int(type, "##########", val);
  462. return "(" + o.substr(0,3) + ") " + o.substr(3, 3) + "-" + o.substr(6);
  463. }
  464. var oa = "";
  465. if((r = fmt.match(/^([#0?]+)( ?)\/( ?)([#0?]+)/)) !== null) {
  466. ri = Math.min(r[4].length,7);
  467. ff = frac(aval, Math.pow(10,ri)-1, false);
  468. o = "" + sign;
  469. oa = write_num("n", r[1], ff[1]);
  470. if(oa[oa.length-1] == " ") oa = oa.substr(0,oa.length-1) + "0";
  471. o += oa + r[2] + "/" + r[3];
  472. oa = rpad_(ff[2],ri);
  473. if(oa.length < r[4].length) oa = hashq(r[4].substr(r[4].length-oa.length)) + oa;
  474. o += oa;
  475. return o;
  476. }
  477. if((r = fmt.match(/^# ([#0?]+)( ?)\/( ?)([#0?]+)/)) !== null) {
  478. ri = Math.min(Math.max(r[1].length, r[4].length),7);
  479. ff = frac(aval, Math.pow(10,ri)-1, true);
  480. return sign + (ff[0]||(ff[1] ? "" : "0")) + " " + (ff[1] ? pad_(ff[1],ri) + r[2] + "/" + r[3] + rpad_(ff[2],ri): fill(" ", 2*ri+1 + r[2].length + r[3].length));
  481. }
  482. if((r = fmt.match(/^[#0?]+$/)) !== null) {
  483. o = "" + val;
  484. if(fmt.length <= o.length) return o;
  485. return hashq(fmt.substr(0,fmt.length-o.length)) + o;
  486. }
  487. if((r = fmt.match(/^([#0]+)\.([#0]+)$/)) !== null) {
  488. o = "" + val.toFixed(Math.min(r[2].length,10)).replace(/([^0])0+$/,"$1");
  489. ri = o.indexOf(".");
  490. var lres = fmt.indexOf(".") - ri, rres = fmt.length - o.length - lres;
  491. return hashq(fmt.substr(0,lres) + o + fmt.substr(fmt.length-rres));
  492. }
  493. if((r = fmt.match(/^00,000\.([#0]*0)$/)) !== null) {
  494. return val < 0 ? "-" + write_num_int(type, fmt, -val) : commaify(""+val).replace(/^\d,\d{3}$/,"0$&").replace(/^\d*$/,function($$) { return "00," + ($$.length < 3 ? pad0(0,3-$$.length) : "") + $$; }) + "." + pad0(0,r[1].length);
  495. }
  496. switch(fmt) {
  497. case "#,###": var x = commaify(""+aval); return x !== "0" ? sign + x : "";
  498. default:
  499. }
  500. throw new Error("unsupported format |" + fmt + "|");
  501. }
  502. return function write_num(type, fmt, val) {
  503. return (val|0) === val ? write_num_int(type, fmt, val) : write_num_flt(type, fmt, val);
  504. };})();
  505. function split_fmt(fmt) {
  506. var out = [];
  507. var in_str = false, cc;
  508. for(var i = 0, j = 0; i < fmt.length; ++i) switch((cc=fmt.charCodeAt(i))) {
  509. case 34: /* '"' */
  510. in_str = !in_str; break;
  511. case 95: case 42: case 92: /* '_' '*' '\\' */
  512. ++i; break;
  513. case 59: /* ';' */
  514. out[out.length] = fmt.substr(j,i-j);
  515. j = i+1;
  516. }
  517. out[out.length] = fmt.substr(j);
  518. if(in_str === true) throw new Error("Format |" + fmt + "| unterminated string ");
  519. return out;
  520. }
  521. SSF._split = split_fmt;
  522. var abstime = /\[[HhMmSs]*\]/;
  523. function eval_fmt(fmt, v, opts, flen) {
  524. var out = [], o = "", i = 0, c = "", lst='t', q, dt, j, cc;
  525. var hr='H';
  526. /* Tokenize */
  527. while(i < fmt.length) {
  528. switch((c = fmt[i])) {
  529. case 'G': /* General */
  530. if(!isgeneral(fmt, i)) throw new Error('unrecognized character ' + c + ' in ' +fmt);
  531. out[out.length] = {t:'G', v:'General'}; i+=7; break;
  532. case '"': /* Literal text */
  533. for(o="";(cc=fmt.charCodeAt(++i)) !== 34 && i < fmt.length;) o += String.fromCharCode(cc);
  534. out[out.length] = {t:'t', v:o}; ++i; break;
  535. case '\\': var w = fmt[++i], t = (w === "(" || w === ")") ? w : 't';
  536. out[out.length] = {t:t, v:w}; ++i; break;
  537. case '_': out[out.length] = {t:'t', v:" "}; i+=2; break;
  538. case '@': /* Text Placeholder */
  539. out[out.length] = {t:'T', v:v}; ++i; break;
  540. case 'B': case 'b':
  541. if(fmt[i+1] === "1" || fmt[i+1] === "2") {
  542. if(dt==null) { dt=parse_date_code(v, opts, fmt[i+1] === "2"); if(dt==null) return ""; }
  543. out[out.length] = {t:'X', v:fmt.substr(i,2)}; lst = c; i+=2; break;
  544. }
  545. /* falls through */
  546. case 'M': case 'D': case 'Y': case 'H': case 'S': case 'E':
  547. c = c.toLowerCase();
  548. /* falls through */
  549. case 'm': case 'd': case 'y': case 'h': case 's': case 'e': case 'g':
  550. if(v < 0) return "";
  551. if(dt==null) { dt=parse_date_code(v, opts); if(dt==null) return ""; }
  552. o = c; while(++i<fmt.length && fmt[i].toLowerCase() === c) o+=c;
  553. if(c === 'm' && lst.toLowerCase() === 'h') c = 'M'; /* m = minute */
  554. if(c === 'h') c = hr;
  555. out[out.length] = {t:c, v:o}; lst = c; break;
  556. case 'A':
  557. q={t:c, v:"A"};
  558. if(dt==null) dt=parse_date_code(v, opts);
  559. if(fmt.substr(i, 3) === "A/P") { if(dt!=null) q.v = dt.H >= 12 ? "P" : "A"; q.t = 'T'; hr='h';i+=3;}
  560. else if(fmt.substr(i,5) === "AM/PM") { if(dt!=null) q.v = dt.H >= 12 ? "PM" : "AM"; q.t = 'T'; i+=5; hr='h'; }
  561. else { q.t = "t"; ++i; }
  562. if(dt==null && q.t === 'T') return "";
  563. out[out.length] = q; lst = c; break;
  564. case '[':
  565. o = c;
  566. while(fmt[i++] !== ']' && i < fmt.length) o += fmt[i];
  567. if(o.substr(-1) !== ']') throw 'unterminated "[" block: |' + o + '|';
  568. if(o.match(abstime)) {
  569. if(dt==null) { dt=parse_date_code(v, opts); if(dt==null) return ""; }
  570. out[out.length] = {t:'Z', v:o.toLowerCase()};
  571. } else { o=""; }
  572. break;
  573. /* Numbers */
  574. case '.':
  575. if(dt != null) {
  576. o = c; while((c=fmt[++i]) === "0") o += c;
  577. out[out.length] = {t:'s', v:o}; break;
  578. }
  579. /* falls through */
  580. case '0': case '#':
  581. o = c; while("0#?.,E+-%".indexOf(c=fmt[++i]) > -1 || c=='\\' && fmt[i+1] == "-" && "0#".indexOf(fmt[i+2])>-1) o += c;
  582. out[out.length] = {t:'n', v:o}; break;
  583. case '?':
  584. o = c; while(fmt[++i] === c) o+=c;
  585. q={t:c, v:o}; out[out.length] = q; lst = c; break;
  586. case '*': ++i; if(fmt[i] == ' ' || fmt[i] == '*') ++i; break; // **
  587. case '(': case ')': out[out.length] = {t:(flen===1?'t':c), v:c}; ++i; break;
  588. case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
  589. o = c; while("0123456789".indexOf(fmt[++i]) > -1) o+=fmt[i];
  590. out[out.length] = {t:'D', v:o}; break;
  591. case ' ': out[out.length] = {t:c, v:c}; ++i; break;
  592. default:
  593. if(",$-+/():!^&'~{}<>=€acfijklopqrtuvwxz".indexOf(c) === -1) throw new Error('unrecognized character ' + c + ' in ' + fmt);
  594. out[out.length] = {t:'t', v:c}; ++i; break;
  595. }
  596. }
  597. var bt = 0, ss0 = 0, ssm;
  598. for(i=out.length-1, lst='t'; i >= 0; --i) {
  599. switch(out[i].t) {
  600. case 'h': case 'H': out[i].t = hr; lst='h'; if(bt < 1) bt = 1; break;
  601. case 's':
  602. if((ssm=out[i].v.match(/\.0+$/))) ss0=Math.max(ss0,ssm[0].length-1);
  603. if(bt < 3) bt = 3;
  604. /* falls through */
  605. case 'd': case 'y': case 'M': case 'e': lst=out[i].t; break;
  606. case 'm': if(lst === 's') { out[i].t = 'M'; if(bt < 2) bt = 2; } break;
  607. case 'X': if(out[i].v === "B2");
  608. break;
  609. case 'Z':
  610. if(bt < 1 && out[i].v.match(/[Hh]/)) bt = 1;
  611. if(bt < 2 && out[i].v.match(/[Mm]/)) bt = 2;
  612. if(bt < 3 && out[i].v.match(/[Ss]/)) bt = 3;
  613. }
  614. }
  615. switch(bt) {
  616. case 0: break;
  617. case 1:
  618. if(dt.u >= 0.5) { dt.u = 0; ++dt.S; }
  619. if(dt.S >= 60) { dt.S = 0; ++dt.M; }
  620. if(dt.M >= 60) { dt.M = 0; ++dt.H; }
  621. break;
  622. case 2:
  623. if(dt.u >= 0.5) { dt.u = 0; ++dt.S; }
  624. if(dt.S >= 60) { dt.S = 0; ++dt.M; }
  625. break;
  626. }
  627. /* replace fields */
  628. var nstr = "", jj;
  629. for(i=0; i < out.length; ++i) {
  630. switch(out[i].t) {
  631. case 't': case 'T': case ' ': case 'D': break;
  632. case 'X': out[i] = undefined; break;
  633. case 'd': case 'm': case 'y': case 'h': case 'H': case 'M': case 's': case 'e': case 'b': case 'Z':
  634. out[i].v = write_date(out[i].t.charCodeAt(0), out[i].v, dt, ss0);
  635. out[i].t = 't'; break;
  636. case 'n': case '(': case '?':
  637. jj = i+1;
  638. while(out[jj] != null && (
  639. (c=out[jj].t) === "?" || c === "D" ||
  640. (c === " " || c === "t") && out[jj+1] != null && (out[jj+1].t === '?' || out[jj+1].t === "t" && out[jj+1].v === '/') ||
  641. out[i].t === '(' && (c === ' ' || c === 'n' || c === ')') ||
  642. c === 't' && (out[jj].v === '/' || '$€'.indexOf(out[jj].v) > -1 || out[jj].v === ' ' && out[jj+1] != null && out[jj+1].t == '?')
  643. )) {
  644. out[i].v += out[jj].v;
  645. out[jj] = undefined; ++jj;
  646. }
  647. nstr += out[i].v;
  648. i = jj-1; break;
  649. case 'G': out[i].t = 't'; out[i].v = general_fmt(v,opts); break;
  650. }
  651. }
  652. var vv = "", myv, ostr;
  653. if(nstr.length > 0) {
  654. myv = (v<0&&nstr.charCodeAt(0) === 45 ? -v : v); /* '-' */
  655. ostr = write_num(nstr.charCodeAt(0) === 40 ? '(' : 'n', nstr, myv); /* '(' */
  656. jj=ostr.length-1;
  657. var decpt = out.length;
  658. for(i=0; i < out.length; ++i) if(out[i] != null && out[i].v.indexOf(".") > -1) { decpt = i; break; }
  659. var lasti=out.length;
  660. if(decpt === out.length && ostr.indexOf("E") === -1) {
  661. for(i=out.length-1; i>= 0;--i) {
  662. if(out[i] == null || 'n?('.indexOf(out[i].t) === -1) continue;
  663. if(jj>=out[i].v.length-1) { jj -= out[i].v.length; out[i].v = ostr.substr(jj+1, out[i].v.length); }
  664. else if(jj < 0) out[i].v = "";
  665. else { out[i].v = ostr.substr(0, jj+1); jj = -1; }
  666. out[i].t = 't';
  667. lasti = i;
  668. }
  669. if(jj>=0 && lasti<out.length) out[lasti].v = ostr.substr(0,jj+1) + out[lasti].v;
  670. }
  671. else if(decpt !== out.length && ostr.indexOf("E") === -1) {
  672. jj = ostr.indexOf(".")-1;
  673. for(i=decpt; i>= 0; --i) {
  674. if(out[i] == null || 'n?('.indexOf(out[i].t) === -1) continue;
  675. j=out[i].v.indexOf(".")>-1&&i===decpt?out[i].v.indexOf(".")-1:out[i].v.length-1;
  676. vv = out[i].v.substr(j+1);
  677. for(; j>=0; --j) {
  678. if(jj>=0 && (out[i].v[j] === "0" || out[i].v[j] === "#")) vv = ostr[jj--] + vv;
  679. }
  680. out[i].v = vv;
  681. out[i].t = 't';
  682. lasti = i;
  683. }
  684. if(jj>=0 && lasti<out.length) out[lasti].v = ostr.substr(0,jj+1) + out[lasti].v;
  685. jj = ostr.indexOf(".")+1;
  686. for(i=decpt; i<out.length; ++i) {
  687. if(out[i] == null || 'n?('.indexOf(out[i].t) === -1 && i !== decpt ) continue;
  688. j=out[i].v.indexOf(".")>-1&&i===decpt?out[i].v.indexOf(".")+1:0;
  689. vv = out[i].v.substr(0,j);
  690. for(; j<out[i].v.length; ++j) {
  691. if(jj<ostr.length) vv += ostr[jj++];
  692. }
  693. out[i].v = vv;
  694. out[i].t = 't';
  695. lasti = i;
  696. }
  697. }
  698. }
  699. for(i=0; i<out.length; ++i) if(out[i] != null && 'n(?'.indexOf(out[i].t)>-1) {
  700. myv = (flen >1 && v < 0 && i>0 && out[i-1].v === "-" ? -v:v);
  701. out[i].v = write_num(out[i].t, out[i].v, myv);
  702. out[i].t = 't';
  703. }
  704. var retval = "";
  705. for(i=0; i !== out.length; ++i) if(out[i] != null) retval += out[i].v;
  706. return retval;
  707. }
  708. SSF._eval = eval_fmt;
  709. var cfregex = /\[[=<>]/;
  710. var cfregex2 = /\[([=<>]*)(-?\d+\.?\d*)\]/;
  711. function chkcond(v, rr) {
  712. if(rr == null) return false;
  713. var thresh = parseFloat(rr[2]);
  714. switch(rr[1]) {
  715. case "=": if(v == thresh) return true; break;
  716. case ">": if(v > thresh) return true; break;
  717. case "<": if(v < thresh) return true; break;
  718. case "<>": if(v != thresh) return true; break;
  719. case ">=": if(v >= thresh) return true; break;
  720. case "<=": if(v <= thresh) return true; break;
  721. }
  722. return false;
  723. }
  724. function choose_fmt(f, v) {
  725. var fmt = split_fmt(f);
  726. var l = fmt.length, lat = fmt[l-1].indexOf("@");
  727. if(l<4 && lat>-1) --l;
  728. if(fmt.length > 4) throw "cannot find right format for |" + fmt + "|";
  729. if(typeof v !== "number") return [4, fmt.length === 4 || lat>-1?fmt[fmt.length-1]:"@"];
  730. switch(fmt.length) {
  731. case 1: fmt = lat>-1 ? ["General", "General", "General", fmt[0]] : [fmt[0], fmt[0], fmt[0], "@"]; break;
  732. case 2: fmt = lat>-1 ? [fmt[0], fmt[0], fmt[0], fmt[1]] : [fmt[0], fmt[1], fmt[0], "@"]; break;
  733. case 3: fmt = lat>-1 ? [fmt[0], fmt[1], fmt[0], fmt[2]] : [fmt[0], fmt[1], fmt[2], "@"]; break;
  734. case 4: break;
  735. }
  736. var ff = v > 0 ? fmt[0] : v < 0 ? fmt[1] : fmt[2];
  737. if(fmt[0].indexOf("[") === -1 && fmt[1].indexOf("[") === -1) return [l, ff];
  738. if(fmt[0].match(cfregex) != null || fmt[1].match(cfregex) != null) {
  739. var m1 = fmt[0].match(cfregex2);
  740. var m2 = fmt[1].match(cfregex2);
  741. return chkcond(v, m1) ? [l, fmt[0]] : chkcond(v, m2) ? [l, fmt[1]] : [l, fmt[m1 != null && m2 != null ? 2 : 1]];
  742. }
  743. return [l, ff];
  744. }
  745. function format(fmt,v,o) {
  746. fixopts(o != null ? o : (o=[]));
  747. var sfmt = "";
  748. switch(typeof fmt) {
  749. case "string": sfmt = fmt; break;
  750. case "number": sfmt = (o.table != null ? o.table : table_fmt)[fmt]; break;
  751. }
  752. if(isgeneral(sfmt,0)) return general_fmt(v, o);
  753. var f = choose_fmt(sfmt, v);
  754. if(isgeneral(f[1])) return general_fmt(v, o);
  755. if(v === true) v = "TRUE"; else if(v === false) v = "FALSE";
  756. else if(v === "" || v == null) return "";
  757. return eval_fmt(f[1], v, o, f[0]);
  758. }
  759. SSF._table = table_fmt;
  760. SSF.load = function load_entry(fmt, idx) { table_fmt[idx] = fmt; };
  761. SSF.format = format;
  762. SSF.get_table = function get_table() { return table_fmt; };
  763. SSF.load_table = function load_table(tbl) { for(var i=0; i!=0x0188; ++i) if(tbl[i] !== undefined) SSF.load(tbl[i], i); };
  764. };
  765. make_ssf(SSF);
  766. if(typeof module !== 'undefined' && typeof DO_NOT_EXPORT_SSF === 'undefined') module.exports = SSF;