ssf.js 37 KB

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