ssf.js 34 KB

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