utils.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. var utils = {
  2. isUTF8: function (bytes) {
  3. var i = 0;
  4. while (i < bytes.length) {
  5. if (( // ASCII
  6. bytes[i] == 0x09 ||
  7. bytes[i] == 0x0A ||
  8. bytes[i] == 0x0D ||
  9. (0x20 <= bytes[i] && bytes[i] <= 0x7E)
  10. )) {
  11. i += 1;
  12. continue;
  13. }
  14. if ((// non-overlong 2-byte
  15. (0xC2 <= bytes[i] && bytes[i] <= 0xDF) &&
  16. (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xBF)
  17. )) {
  18. i += 2;
  19. continue;
  20. }
  21. if (( // excluding overlongs
  22. bytes[i] == 0xE0 &&
  23. (0xA0 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
  24. (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF)
  25. ) || ( // straight 3-byte
  26. ((0xE1 <= bytes[i] && bytes[i] <= 0xEC) ||
  27. bytes[i] == 0xEE ||
  28. bytes[i] == 0xEF) &&
  29. (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
  30. (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF)
  31. ) || ( // excluding surrogates
  32. bytes[i] == 0xED &&
  33. (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0x9F) &&
  34. (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF)
  35. )) {
  36. i += 3;
  37. continue;
  38. }
  39. if (( // planes 1-3
  40. bytes[i] == 0xF0 &&
  41. (0x90 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
  42. (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
  43. (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
  44. ) || ( // planes 4-15
  45. (0xF1 <= bytes[i] && bytes[i] <= 0xF3) &&
  46. (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
  47. (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
  48. (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
  49. ) || ( // plane 16
  50. bytes[i] == 0xF4 &&
  51. (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0x8F) &&
  52. (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
  53. (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
  54. )) {
  55. i += 4;
  56. continue;
  57. }
  58. return false;
  59. }
  60. return true;
  61. },
  62. // 读取cell的数字或字母
  63. getCellNum: function(str){
  64. var n = '';
  65. var isNum = !arguments[1];
  66. for(var i in str) {
  67. var val = parseInt(str[i]);
  68. var _isNaN = isNum ? !isNaN(val) : isNaN(val);
  69. if(_isNaN) n += str[i];
  70. }
  71. return isNum ? parseInt(n) : n;
  72. },
  73. // 表头字母转数字
  74. stringToNum: function(str){
  75. str=str.toLowerCase().split("");
  76. var al = str.length;
  77. var getCharNumber = function(charx){
  78. return charx.charCodeAt() -96;
  79. };
  80. var numout = 0;
  81. var charnum = 0;
  82. for(var i = 0; i < al; i++){
  83. charnum = getCharNumber(str[i]);
  84. numout += charnum * Math.pow(26, al-i-1);
  85. };
  86. return numout;
  87. },
  88. // 数字转字母
  89. numToString: function(numm){
  90. var stringArray = [];
  91. stringArray.length = 0;
  92. var numToStringAction = function(nnum){
  93. var num = nnum - 1;
  94. var a = parseInt(num / 26);
  95. var b = num % 26;
  96. stringArray.push(String.fromCharCode(64 + parseInt(b+1)));
  97. if(a>0){
  98. numToStringAction(a);
  99. }
  100. }
  101. numToStringAction(numm);
  102. return stringArray.reverse().join("");
  103. },
  104. // sheetjs.data转luckysheet.data
  105. xlsToLuckySheet: function(sheet, _sheet){
  106. var arr = (_.get(sheet, '!ref') || ':').split(':');
  107. var cols = this.getCellNum(arr[1], true);
  108. cols = this.stringToNum(cols);
  109. cols = cols > 26 ? cols : 26; // 列,字母,不足的填充
  110. var rows = this.getCellNum(arr[1]);
  111. rows = rows > 84 ? rows : 84; // 行,数字
  112. // 表格样式
  113. var _cols = _.get(sheet, '!cols') || {};
  114. var _rows = _.get(sheet, '!rows') || {};
  115. var _merges = _.get(sheet, '!merges') || {};
  116. var obj = [];
  117. var self = this;
  118. for(var i=1; i<=rows; i++) {
  119. var row = [];
  120. for(var j=1; j<=cols; j++) {
  121. var key = self.numToString(j) + i;
  122. var cell = null;
  123. if(sheet[key]) {
  124. // https://mengshukeji.github.io/LuckysheetDocs/zh/guide/cell.html#基本单元格
  125. var value = sheet[key].v || '';
  126. var style = sheet[key].s || {};
  127. var bgColor = _.get(style, 'fgColor.rgb'); // 前景色
  128. // var ftColor = _.get(style, 'ftColor.rgb');
  129. cell = {
  130. m: value, // 显示值
  131. v: value, // 原始值
  132. ct: {fa: sheet[key].z || 'General', t: sheet[key].t || 'g'},
  133. // bg: bgColor ? '#'+bgColor : '',
  134. // bl: _.get(style, 'patternType') == 'bold' ? 1 : 0,
  135. tb: 2, // 0:截断;1:溢出;2:换行
  136. }
  137. if (bgColor) cell.bg = '#'+bgColor;
  138. }
  139. row.push(cell);
  140. _sheet.config.columnlen[j-1] = _cols[j-1] ? _cols[j-1].wpx : 73; // 默认列宽73px
  141. }
  142. obj.push(row)
  143. _sheet.config.rowlen[i-1] = _rows[i-1] ? _rows[i-1].hpt * 4 / 3 : 19; // 本来有参数hpx,但其值和hpt一样;默认值行高19px
  144. }
  145. // 合并单元格
  146. // https://mengshukeji.github.io/LuckysheetDocs/zh/guide/sheet.html#初始化配置
  147. _.each(_merges, function(opt){
  148. var r = opt.s.r; // sheet[!merges] = [{e:{r:,c:},s:{r:,c:}}]
  149. var c = opt.s.c; // s:start,e:end
  150. _sheet.config.merge[r+'_'+c] = {
  151. r: r,
  152. c: c,
  153. rs: opt.e.r - r + 1,
  154. cs: opt.e.c - c + 1,
  155. };
  156. });
  157. return obj;
  158. },
  159. // 单个sheet初始配置
  160. getLuckySheet: function(){
  161. return {
  162. "name": "Sheet1",
  163. "color": "",
  164. "status": 1,
  165. "order": 0,
  166. "data": [ // data直接替换,这里就不写null填充了
  167. [null],
  168. [null],
  169. ],
  170. "config": {
  171. rowlen: {}, // 表格行高
  172. columnlen: {}, // 表格行宽
  173. merge: {}, // 合并单元格
  174. },
  175. "index": 0,
  176. // "jfgird_select_save": [],
  177. "luckysheet_select_save": [],
  178. "visibledatarow": [],
  179. "visibledatacolumn": [],
  180. // "ch_width": 4560,
  181. // "rh_height": 1760,
  182. "luckysheet_selection_range": [],
  183. "zoomRatio": 1,
  184. "celldata": [],
  185. // "load": "1",
  186. "scrollLeft": 0,
  187. "scrollTop": 0
  188. };
  189. }
  190. }