ExcelUtil.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var xlsx = require("xlsx");
  4. var MathUtil_1 = require("./MathUtil");
  5. var Config_1 = require("./Config");
  6. var StringUtil_1 = require("./StringUtil");
  7. var ExcelUtil = /** @class */ (function () {
  8. function ExcelUtil() {
  9. }
  10. /**
  11. * 倒序查找一个字符串种字母第一次出现的位置
  12. * @param str
  13. */
  14. ExcelUtil.getLastCharIndex = function (str) {
  15. var len = str.length;
  16. if (!len || len < 1) {
  17. return -1;
  18. }
  19. for (var i = len - 1; i >= 0; --i) {
  20. var ch = str[i];
  21. if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
  22. return i;
  23. }
  24. }
  25. return -1;
  26. };
  27. /**
  28. * 根据一个地址获取行号
  29. * @param address
  30. */
  31. ExcelUtil.getCellRowNum = function (address) {
  32. return parseInt(address.substring(this.getLastCharIndex(address) + 1), 10);
  33. };
  34. ExcelUtil.getCellColName = function (address) {
  35. return address.substring(0, this.getLastCharIndex(address) + 1);
  36. };
  37. /**
  38. * 读取表格自定义表头信息
  39. * @param workbookArg
  40. * @param sheetname
  41. */
  42. ExcelUtil.getExcelInfo = function (excelPath) {
  43. var _a;
  44. var workbook = xlsx.readFile(excelPath, { cellStyles: true, bookFiles: true });
  45. var sheetname = workbook.SheetNames[0];
  46. var worksheet = workbook.Sheets[sheetname];
  47. var ret = {
  48. keys: [],
  49. excelPath: excelPath,
  50. sheetname: sheetname,
  51. worksheet: [],
  52. cols: [],
  53. minRow: 3,
  54. maxRow: 2,
  55. };
  56. var _loop_1 = function (address) {
  57. if (address[0] === '!') {
  58. return "continue";
  59. }
  60. // jump the value type of e and z (type: b Boolean, e Error, n Number, d Date, s Text, z Stub)
  61. if (worksheet[address].v == undefined ||
  62. worksheet[address].t == 'e' ||
  63. worksheet[address].t == 'z') {
  64. return "continue";
  65. }
  66. ret.worksheet[address] = worksheet[address];
  67. var row = this_1.getCellRowNum(address);
  68. var col = this_1.getCellColName(address);
  69. // console.log('col', col, address, )
  70. var name_1 = worksheet[address].v;
  71. if ((name_1 == undefined || name_1 == null) && row == 1) {
  72. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, ret), " \n\u7F3A\u5931\u8868\u5934\u540D\u5B57!,\u662F\u4E0D\u662F\u63D2\u5165\u4E86\u7A7A\u5217\uFF1F"));
  73. }
  74. if (ret.cols.findIndex(function (v) { return v == col; }) == -1 && worksheet[col + '1'] && worksheet[col + '1'].v) {
  75. ret.cols.push(col);
  76. }
  77. if (ret.cols.length > 0 && ret.maxRow < row && name_1 != undefined) {
  78. ret.maxRow = row;
  79. }
  80. if (row == 1) {
  81. // 读取表头参数
  82. var ls = name_1.indexOf('[');
  83. var le = name_1.indexOf(']');
  84. var outputclient = false;
  85. var outputserver = false;
  86. var type = '';
  87. var isUniq = false; // 重复的行
  88. var isArray = false; // 是否是数组
  89. var canZeroStr = false; // 是否允许字符串数据为'0'
  90. if (ls != -1 && le != -1 && le > ls + 1) {
  91. var s = name_1.substring(ls + 1, le);
  92. name_1 = name_1.substring(0, ls);
  93. for (var i = 0; i < s.length; i++) {
  94. if (s[i] == 'c') {
  95. outputclient = true;
  96. }
  97. else if (s[i] == 's') {
  98. outputserver = true;
  99. }
  100. else if (s[i] == 'n') {
  101. type = 'number';
  102. }
  103. else if (s[i] == 't') {
  104. type = 'string';
  105. }
  106. else if (s[i] == 'u') { // uniq
  107. isUniq = true;
  108. }
  109. else if (s[i] == 'a') { // array
  110. isArray = true;
  111. }
  112. else if (s[i] == 'k') { // key
  113. Config_1.Config.objectKey = name_1;
  114. }
  115. else if (s[i] == 'z') {
  116. canZeroStr = true;
  117. }
  118. else {
  119. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, ret), " \n\u610F\u5916\u7684\u8868\u5934\u5B9A\u4E49\uFF01").concat(s));
  120. }
  121. }
  122. }
  123. if (ret.keys.findIndex(function (v) { return v == name_1; }) != -1) {
  124. Config_1.Config.checkState(Config_1.RetCode.COL_NAME_REPEAT, "".concat(StringUtil_1.default.getCheckString(address, ret), " \n\u540C\u4E00\u4E2A\u8868\u4E2D\u53D1\u73B0\u5217\u540D\u91CD\u590D\uFF01").concat(name_1));
  125. }
  126. ret.keys.push({
  127. name: name_1,
  128. outputclient: outputclient,
  129. outputserver: outputserver,
  130. type: type,
  131. isUniq: isUniq,
  132. isArray: isArray,
  133. canZeroStr: canZeroStr,
  134. des: (_a = worksheet[col + 2]) === null || _a === void 0 ? void 0 : _a.v
  135. });
  136. delete worksheet[address]; // 删除表头信息
  137. }
  138. };
  139. var this_1 = this;
  140. // console.log(excelPath, sheetname)
  141. for (var address in worksheet) {
  142. _loop_1(address);
  143. }
  144. return ret;
  145. };
  146. ExcelUtil.checkArr = function (value, address, headInfo) {
  147. var ret = {
  148. isOneArray: false,
  149. isTwoArray: false,
  150. isThreeArray: false,
  151. };
  152. if (MathUtil_1.default.isNumber(value)) {
  153. return ret;
  154. }
  155. if (value.indexOf(Config_1.Config.THREEE_ARRAY_SYMBOL) != -1) {
  156. ret.isThreeArray = true;
  157. // if (value.indexOf(Config.TWO_ARRAY_SYMBOL) != -1) {
  158. // if (value.indexOf(Config.ONE_ARRAY_SYMBOL) != -1) {
  159. // ret.isOneArray = true;
  160. // ret.isTwoArray = true;
  161. // ret.isThreeArray = true;
  162. // } else {
  163. // Config.checkState(RetCode.NOT_FOUND_ONE_ARRAY_SYMBOL, `${StringUtil.getCheckString(address, headInfo)} \n数组分割符使用有误, 发现了三维分割符以及二维分隔符,但是未发现一维分割符!`)
  164. // }
  165. // } else {
  166. // if (value.indexOf('color') == -1) {
  167. // Config.checkState(RetCode.NOT_FOUND_TWO_ARRAY_SYMBOL, `${StringUtil.getCheckString(address, headInfo)} \n数组分割符使用有误, 发现了三维分割符,但是未发现二维分割符!`)
  168. // }
  169. // }
  170. }
  171. if (value.indexOf(Config_1.Config.TWO_ARRAY_SYMBOL) != -1) {
  172. ret.isTwoArray = true;
  173. // if (value.indexOf(Config.ONE_ARRAY_SYMBOL) != -1) {
  174. // ret.isOneArray = true;
  175. // ret.isTwoArray = true;
  176. // } else {
  177. // Config.checkState(RetCode.NOT_FOUND_TWO_ARRAY_SYMBOL, `${StringUtil.getCheckString(address, headInfo)} \n数组分割符使用有误, 发现了二维分割符,但是未发现一维分割符!`)
  178. // }
  179. }
  180. if (value.indexOf(Config_1.Config.ONE_ARRAY_SYMBOL) != -1) {
  181. ret.isOneArray = true;
  182. }
  183. return ret;
  184. };
  185. ExcelUtil.tryGetArray = function (value, isMult, isNumber, canZeroStr) {
  186. var ret = '';
  187. value = StringUtil_1.default.replaceAll(value, '<color=#', '<color=@');
  188. var threeArr = value.split(Config_1.Config.THREEE_ARRAY_SYMBOL);
  189. for (var threeIndex = 0; threeIndex < threeArr.length; threeIndex++) {
  190. var twoArr = threeArr[threeIndex].split(Config_1.Config.TWO_ARRAY_SYMBOL);
  191. if (isMult) {
  192. ret += '[';
  193. }
  194. for (var twoIndex = 0; twoIndex < twoArr.length; twoIndex++) {
  195. var oneArr = twoArr[twoIndex].split(Config_1.Config.ONE_ARRAY_SYMBOL);
  196. ret += '[';
  197. for (var oneIndex = 0; oneIndex < oneArr.length; oneIndex++) {
  198. var oneUnit = oneArr[oneIndex];
  199. if (MathUtil_1.default.isNumber(oneUnit) && isNumber) {
  200. ret += oneUnit;
  201. }
  202. else {
  203. if ((canZeroStr && oneUnit.toString() == '0') || oneUnit.toString() != '0') {
  204. ret += "'".concat(oneUnit, "'"); // 支持字符串数组
  205. }
  206. }
  207. if (oneIndex != oneArr.length - 1) {
  208. ret += ', ';
  209. }
  210. }
  211. ret += '], ';
  212. }
  213. if (ret.substr(ret.length - 2, 2) == ', ') {
  214. ret = ret.substr(0, ret.length - 2);
  215. }
  216. if (isMult) {
  217. ret += '], ';
  218. }
  219. }
  220. if (ret.substr(ret.length - 2, 2) == ', ') {
  221. ret = ret.substr(0, ret.length - 2);
  222. }
  223. if (threeArr.length > 1) {
  224. ret = "[".concat(ret, "]");
  225. }
  226. ret = StringUtil_1.default.replaceAll(ret, '<color=@', '<color=#');
  227. return ret;
  228. };
  229. ExcelUtil.getObj = function (values, isMultArr, isArray, headInfo, row) {
  230. var cobj = {};
  231. var sobj = {};
  232. for (var i = 0; i < values.length; i++) {
  233. var temp = values[i].toString();
  234. temp = StringUtil_1.default.replaceDoubleMid(temp, '$', function (mid) { return (parseInt(mid, 10) * Config_1.Config.yuanRate).toString(); }, true);
  235. temp = StringUtil_1.default.replaceDoubleMid(temp, '¥', function (mid) { return Config_1.Config.replaceStr ? Config_1.Config.replaceStr : mid; });
  236. if (headInfo.keys[i].outputserver) {
  237. sobj[headInfo.keys[i].name] = temp;
  238. }
  239. if (headInfo.keys[i].outputclient) {
  240. var address = headInfo.cols[i] + row;
  241. if (headInfo.keys[i].type == 'number' || headInfo.keys[i].type == 'number[]') {
  242. if (headInfo.keys[i].isArray) {
  243. headInfo.keys[i].type = 'number[]';
  244. temp = this.tryGetArray(temp, isMultArr[i], true);
  245. var regex = /^[0-9\[\],"\s\-.]+$/;
  246. if (!regex.test(temp))
  247. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \n\u6570\u7EC4\u5185\u5BB9\u5B58\u5728\u5B57\u7B26\u4E32\uFF01"));
  248. }
  249. else {
  250. temp = Number(temp);
  251. if (isNaN(temp)) {
  252. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \n\u6570\u5B57\u5185\u5BB9\u5B58\u5728\u5B57\u7B26\u4E32\uFF01"));
  253. }
  254. }
  255. }
  256. else if (headInfo.keys[i].type == 'string' || headInfo.keys[i].type == 'string[]') {
  257. if (isArray[i]) {
  258. headInfo.keys[i].type = 'string[]';
  259. temp = this.tryGetArray(temp, isMultArr[i], false, headInfo.keys[i].canZeroStr);
  260. }
  261. else {
  262. if (!headInfo.keys[i].canZeroStr && temp == '0')
  263. temp = '';
  264. }
  265. }
  266. cobj[headInfo.keys[i].name] = temp;
  267. }
  268. }
  269. return {
  270. c: cobj,
  271. s: sobj,
  272. };
  273. };
  274. ExcelUtil.creatObj = function (headInfo) {
  275. var worksheet = headInfo.worksheet;
  276. var cobjlist = [];
  277. var sobjlist = [];
  278. var errorlist = [];
  279. for (var col = 0; col < headInfo.cols.length; col++) {
  280. var uniq = [];
  281. var colError = [];
  282. if (headInfo.keys[col].isUniq) {
  283. for (var row = headInfo.minRow; row <= headInfo.maxRow; row++) {
  284. var address = headInfo.cols[col] + row;
  285. if (!worksheet[address]) {
  286. // console.log(address);
  287. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u5355\u5143\u683C\u4E3A\u7A7A0\uFF01"));
  288. }
  289. if (worksheet[address].v == undefined) {
  290. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u6570\u636E\u9519\u8BEF1\uFF01"));
  291. }
  292. var value = worksheet[address].v;
  293. var index = MathUtil_1.default.findSame(uniq, value, 'value');
  294. if (index == -1) {
  295. uniq.push({ address: address, value: value });
  296. }
  297. else {
  298. var first = uniq[index];
  299. if (MathUtil_1.default.findSame(colError, first.address, 'address') == -1) {
  300. colError.push(first);
  301. }
  302. if (MathUtil_1.default.findSame(colError, address, 'address') == -1) {
  303. colError.push({ address: address, value: value });
  304. }
  305. }
  306. }
  307. }
  308. if (colError.length > 0) {
  309. errorlist.push({ name: headInfo.cols[col], list: colError });
  310. }
  311. }
  312. if (errorlist.length > 0) {
  313. var out_1 = '';
  314. errorlist.forEach(function (error) {
  315. out_1 += error.name + ' ';
  316. console.log("".concat(error.name, "\u884C"));
  317. error.list.forEach(function (unit) {
  318. console.log(unit);
  319. });
  320. });
  321. Config_1.Config.checkState(Config_1.RetCode.SAME_COL_ROW_UNIQ_REPEAT, "".concat(StringUtil_1.default.getCheckString(out_1, headInfo), "\u540C\u4E00\u5217\u884C\u5185\u5BB9\u91CD\u590D\uFF01"));
  322. }
  323. // 遍历整张表,对每一列进行分析
  324. var isMultArr = [];
  325. var isArray = [];
  326. for (var col = 0; col < headInfo.cols.length; col++) {
  327. var arrayNum = 0;
  328. if (headInfo.keys[col].isArray) {
  329. var isOne = 1;
  330. var isTwo = 0;
  331. var isThree = 0;
  332. for (var row = headInfo.minRow; row <= headInfo.maxRow; row++) {
  333. var address = headInfo.cols[col] + row;
  334. if (!worksheet[address]) {
  335. // console.log(address);
  336. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u5355\u5143\u683C\u4E3A\u7A7A1\uFF01"));
  337. }
  338. if (worksheet[address].v == undefined) {
  339. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u6570\u636E\u9519\u8BEF2\uFF01"));
  340. }
  341. var value = worksheet[address].v;
  342. var result = this.checkArr(value, address, headInfo);
  343. if (result.isOneArray) {
  344. isOne++;
  345. }
  346. if (result.isTwoArray) {
  347. isTwo++;
  348. }
  349. if (result.isThreeArray) {
  350. isThree++;
  351. }
  352. }
  353. isMultArr.push(isTwo > 0 || isThree > 0);
  354. isArray.push(isOne > 0);
  355. if (isOne > 0) {
  356. arrayNum = 1;
  357. }
  358. if (isTwo > 0) {
  359. arrayNum = 2;
  360. }
  361. if (isThree > 0) {
  362. arrayNum = 3;
  363. }
  364. }
  365. else {
  366. isMultArr.push(false);
  367. isArray.push(false);
  368. }
  369. headInfo.keys[col].arrayNum = arrayNum;
  370. }
  371. // 初始化
  372. for (var row = headInfo.minRow; row <= headInfo.maxRow; row++) {
  373. var values = [];
  374. var address = '';
  375. // 获取一列的对象
  376. for (var col = 0; col < headInfo.cols.length; col++) {
  377. address = headInfo.cols[col] + row;
  378. if (worksheet[address] == undefined) {
  379. if (headInfo.cols[col] == 'A') {
  380. var lastAddress = headInfo.cols[headInfo.cols.length - 1] + row; // 获取最后一个的地址
  381. if (worksheet[lastAddress] == undefined) {
  382. if (row == 2) {
  383. console.log("\u8B66\u544A\uFF1A".concat(StringUtil_1.default.getCheckString('行' + row, headInfo), " \u53EF\u80FD\u662F\u7A7A\u8868\uFF01"));
  384. return {
  385. sheetname: headInfo.sheetname,
  386. jsobj: cobjlist,
  387. csvobj: sobjlist,
  388. headInfo: headInfo
  389. };
  390. }
  391. else {
  392. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString('行' + row, headInfo), " \u53EF\u80FD\u5B58\u5728\u7A7A\u884C\uFF01\uFF0C\u8BF7\u68C0\u67E5"));
  393. }
  394. }
  395. else {
  396. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(lastAddress, headInfo), " \u6570\u636E\u9519\u8BEF3\uFF01"));
  397. }
  398. }
  399. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u6570\u636E\u9519\u8BEF4\uFF01"));
  400. }
  401. var value = worksheet[address].v;
  402. // jump the value type of e and z (type: b Boolean, e Error, n Number, d Date, s Text, z Stub)
  403. if (worksheet[address].t == 'e' || worksheet[address].t == 'z') {
  404. continue;
  405. }
  406. if (value.toString().indexOf(',') != -1) {
  407. // if (headInfo.keys[col].outputserver) {
  408. // Config.checkState(RetCode.EXCEL_DATA_ERROR, `${StringUtil.getCheckString(address, headInfo)} 单元格内存在英文逗号,影响csv输出!`)
  409. // } else
  410. if (headInfo.keys[col].outputclient) {
  411. var reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
  412. if (reg.test(value)) {
  413. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u5355\u5143\u683C\u5185\u5305\u542B\u4E2D\u6587\u5374\u4F7F\u7528\u4E86\u82F1\u6587\u9017\u53F7\uFF01"));
  414. }
  415. }
  416. }
  417. //最多保留两位小数
  418. if (typeof value === 'number' && !isNaN(value)) {
  419. value = parseFloat(value.toFixed(2));
  420. }
  421. values.push(value);
  422. }
  423. if (headInfo.keys.length != values.length) {
  424. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u6570\u636E\u9519\u8BEF5\uFF01"));
  425. }
  426. var obj = this.getObj(values, isMultArr, isArray, headInfo, row);
  427. cobjlist.push(obj.c);
  428. sobjlist.push(obj.s);
  429. }
  430. return {
  431. sheetname: headInfo.sheetname,
  432. jsobj: cobjlist,
  433. csvobj: sobjlist,
  434. headInfo: headInfo
  435. };
  436. };
  437. return ExcelUtil;
  438. }());
  439. exports.default = ExcelUtil;