ExcelUtil.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 workbook = xlsx.readFile(excelPath, { cellStyles: true, bookFiles: true });
  44. var sheetname = workbook.SheetNames[0];
  45. var worksheet = workbook.Sheets[sheetname];
  46. var ret = {
  47. keys: [],
  48. excelPath: excelPath,
  49. sheetname: sheetname,
  50. worksheet: [],
  51. cols: [],
  52. minRow: 3,
  53. maxRow: 2,
  54. };
  55. var _loop_1 = function (address) {
  56. if (address[0] === '!') {
  57. return "continue";
  58. }
  59. // jump the value type of e and z (type: b Boolean, e Error, n Number, d Date, s Text, z Stub)
  60. if (worksheet[address].v == undefined ||
  61. worksheet[address].t == 'e' ||
  62. worksheet[address].t == 'z') {
  63. return "continue";
  64. }
  65. ret.worksheet[address] = worksheet[address];
  66. var row = this_1.getCellRowNum(address);
  67. var col = this_1.getCellColName(address);
  68. // console.log('col', col, address, )
  69. var name_1 = worksheet[address].v;
  70. if ((name_1 == undefined || name_1 == null) && row == 1) {
  71. 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"));
  72. }
  73. if (ret.cols.findIndex(function (v) { return v == col; }) == -1 && worksheet[col + '1']) {
  74. ret.cols.push(col);
  75. }
  76. if (ret.cols.length > 0 && ret.maxRow < row && name_1 != undefined) {
  77. ret.maxRow = row;
  78. }
  79. if (row == 1) {
  80. // 读取表头参数
  81. var ls = name_1.indexOf('[');
  82. var le = name_1.indexOf(']');
  83. var outputclient = false;
  84. var outputserver = false;
  85. var type = '';
  86. var isUniq = false; // 重复的行
  87. var isArray = false; // 是否是数组
  88. var canZeroStr = false; // 是否允许字符串数据为'0'
  89. if (ls != -1 && le != -1 && le > ls + 1) {
  90. var s = name_1.substring(ls + 1, le);
  91. name_1 = name_1.substring(0, ls);
  92. for (var i = 0; i < s.length; i++) {
  93. if (s[i] == 'c') {
  94. outputclient = true;
  95. }
  96. else if (s[i] == 's') {
  97. outputserver = true;
  98. }
  99. else if (s[i] == 'n') {
  100. type = 'number';
  101. }
  102. else if (s[i] == 't') {
  103. type = 'string';
  104. }
  105. else if (s[i] == 'u') { // uniq
  106. isUniq = true;
  107. }
  108. else if (s[i] == 'a') { // array
  109. isArray = true;
  110. }
  111. else if (s[i] == 'k') { // key
  112. Config_1.Config.objectKey = name_1;
  113. }
  114. else if (s[i] == 'z') {
  115. canZeroStr = true;
  116. }
  117. else {
  118. 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));
  119. }
  120. }
  121. }
  122. if (ret.keys.findIndex(function (v) { return v == name_1; }) != -1) {
  123. 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));
  124. }
  125. ret.keys.push({ name: name_1, outputclient: outputclient, outputserver: outputserver, type: type, isUniq: isUniq, isArray: isArray, canZeroStr: canZeroStr });
  126. delete worksheet[address]; // 删除表头信息
  127. }
  128. };
  129. var this_1 = this;
  130. // console.log(excelPath, sheetname)
  131. for (var address in worksheet) {
  132. _loop_1(address);
  133. }
  134. return ret;
  135. };
  136. ExcelUtil.checkArr = function (value, address, headInfo) {
  137. var ret = {
  138. isOneArray: false,
  139. isTwoArray: false,
  140. isThreeArray: false,
  141. };
  142. if (MathUtil_1.default.isNumber(value)) {
  143. return ret;
  144. }
  145. if (value.indexOf(Config_1.Config.THREEE_ARRAY_SYMBOL) != -1) {
  146. if (value.indexOf(Config_1.Config.TWO_ARRAY_SYMBOL) != -1) {
  147. if (value.indexOf(Config_1.Config.ONE_ARRAY_SYMBOL) != -1) {
  148. ret.isOneArray = true;
  149. ret.isTwoArray = true;
  150. ret.isThreeArray = true;
  151. }
  152. else {
  153. Config_1.Config.checkState(Config_1.RetCode.NOT_FOUND_ONE_ARRAY_SYMBOL, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \n\u6570\u7EC4\u5206\u5272\u7B26\u4F7F\u7528\u6709\u8BEF\uFF0C \u53D1\u73B0\u4E86\u4E09\u7EF4\u5206\u5272\u7B26\u4EE5\u53CA\u4E8C\u7EF4\u5206\u9694\u7B26\uFF0C\u4F46\u662F\u672A\u53D1\u73B0\u4E00\u7EF4\u5206\u5272\u7B26\uFF01"));
  154. }
  155. }
  156. else {
  157. if (value.indexOf('color') == -1) {
  158. Config_1.Config.checkState(Config_1.RetCode.NOT_FOUND_TWO_ARRAY_SYMBOL, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \n\u6570\u7EC4\u5206\u5272\u7B26\u4F7F\u7528\u6709\u8BEF\uFF0C \u53D1\u73B0\u4E86\u4E09\u7EF4\u5206\u5272\u7B26\uFF0C\u4F46\u662F\u672A\u53D1\u73B0\u4E8C\u7EF4\u5206\u5272\u7B26\uFF01"));
  159. }
  160. }
  161. }
  162. if (value.indexOf(Config_1.Config.TWO_ARRAY_SYMBOL) != -1) {
  163. if (value.indexOf(Config_1.Config.ONE_ARRAY_SYMBOL) != -1) {
  164. ret.isOneArray = true;
  165. ret.isTwoArray = true;
  166. }
  167. else {
  168. Config_1.Config.checkState(Config_1.RetCode.NOT_FOUND_TWO_ARRAY_SYMBOL, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \n\u6570\u7EC4\u5206\u5272\u7B26\u4F7F\u7528\u6709\u8BEF\uFF0C \u53D1\u73B0\u4E86\u4E8C\u7EF4\u5206\u5272\u7B26\uFF0C\u4F46\u662F\u672A\u53D1\u73B0\u4E00\u7EF4\u5206\u5272\u7B26\uFF01"));
  169. }
  170. }
  171. if (value.indexOf(Config_1.Config.ONE_ARRAY_SYMBOL) != -1) {
  172. ret.isOneArray = true;
  173. }
  174. return ret;
  175. };
  176. ExcelUtil.tryGetArray = function (value, isMult, isNumber, canZeroStr) {
  177. var ret = '';
  178. value = StringUtil_1.default.replaceAll(value, '<color=#', '<color=@');
  179. var threeArr = value.split(Config_1.Config.THREEE_ARRAY_SYMBOL);
  180. for (var threeIndex = 0; threeIndex < threeArr.length; threeIndex++) {
  181. var twoArr = threeArr[threeIndex].split(Config_1.Config.TWO_ARRAY_SYMBOL);
  182. if (isMult) {
  183. ret += '[';
  184. }
  185. for (var twoIndex = 0; twoIndex < twoArr.length; twoIndex++) {
  186. var oneArr = twoArr[twoIndex].split(Config_1.Config.ONE_ARRAY_SYMBOL);
  187. ret += '[';
  188. for (var oneIndex = 0; oneIndex < oneArr.length; oneIndex++) {
  189. var oneUnit = oneArr[oneIndex];
  190. if (MathUtil_1.default.isNumber(oneUnit) && isNumber) {
  191. ret += oneUnit;
  192. }
  193. else {
  194. if ((canZeroStr && oneUnit.toString() == '0') || oneUnit.toString() != '0') {
  195. ret += "'".concat(oneUnit, "'"); // 支持字符串数组
  196. }
  197. }
  198. if (oneIndex != oneArr.length - 1) {
  199. ret += ', ';
  200. }
  201. }
  202. ret += '], ';
  203. }
  204. if (ret.substr(ret.length - 2, 2) == ', ') {
  205. ret = ret.substr(0, ret.length - 2);
  206. }
  207. if (isMult) {
  208. ret += '], ';
  209. }
  210. }
  211. if (ret.substr(ret.length - 2, 2) == ', ') {
  212. ret = ret.substr(0, ret.length - 2);
  213. }
  214. if (threeArr.length > 1) {
  215. ret = "[".concat(ret, "]");
  216. }
  217. ret = StringUtil_1.default.replaceAll(ret, '<color=@', '<color=#');
  218. return ret;
  219. };
  220. ExcelUtil.getObj = function (values, isMultArr, isArray, headInfo, row) {
  221. var cobj = {};
  222. var sobj = {};
  223. for (var i = 0; i < values.length; i++) {
  224. var temp = values[i].toString();
  225. temp = StringUtil_1.default.replaceDoubleMid(temp, '$', function (mid) { return (parseInt(mid, 10) * Config_1.Config.yuanRate).toString(); }, true);
  226. temp = StringUtil_1.default.replaceDoubleMid(temp, '¥', function (mid) { return Config_1.Config.replaceStr ? Config_1.Config.replaceStr : mid; });
  227. if (headInfo.keys[i].outputserver) {
  228. sobj[headInfo.keys[i].name] = temp;
  229. }
  230. if (headInfo.keys[i].outputclient) {
  231. var address = headInfo.cols[i] + row;
  232. if (headInfo.keys[i].type == 'number' || headInfo.keys[i].type == 'number[]') {
  233. if (headInfo.keys[i].isArray) {
  234. headInfo.keys[i].type = 'number[]';
  235. temp = this.tryGetArray(temp, isMultArr[i], true);
  236. var regex = /^[0-9\[\],"\s\-.]+$/;
  237. if (!regex.test(temp))
  238. 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"));
  239. }
  240. else {
  241. temp = Number(temp);
  242. if (isNaN(temp)) {
  243. 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"));
  244. }
  245. }
  246. }
  247. else if (headInfo.keys[i].type == 'string' || headInfo.keys[i].type == 'string[]') {
  248. if (isArray[i]) {
  249. headInfo.keys[i].type = 'string[]';
  250. temp = this.tryGetArray(temp, isMultArr[i], false, headInfo.keys[i].canZeroStr);
  251. }
  252. else {
  253. if (!headInfo.keys[i].canZeroStr && temp == '0')
  254. temp = '';
  255. }
  256. }
  257. cobj[headInfo.keys[i].name] = temp;
  258. }
  259. }
  260. return {
  261. c: cobj,
  262. s: sobj,
  263. };
  264. };
  265. ExcelUtil.creatObj = function (headInfo) {
  266. var worksheet = headInfo.worksheet;
  267. var cobjlist = [];
  268. var sobjlist = [];
  269. var errorlist = [];
  270. for (var col = 0; col < headInfo.cols.length; col++) {
  271. var uniq = [];
  272. var colError = [];
  273. if (headInfo.keys[col].isUniq) {
  274. for (var row = headInfo.minRow; row <= headInfo.maxRow; row++) {
  275. var address = headInfo.cols[col] + row;
  276. if (!worksheet[address]) {
  277. // console.log(address);
  278. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u5355\u5143\u683C\u4E3A\u7A7A0\uFF01"));
  279. }
  280. if (worksheet[address].v == undefined) {
  281. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u6570\u636E\u9519\u8BEF1\uFF01"));
  282. }
  283. var value = worksheet[address].v;
  284. var index = MathUtil_1.default.findSame(uniq, value, 'value');
  285. if (index == -1) {
  286. uniq.push({ address: address, value: value });
  287. }
  288. else {
  289. var first = uniq[index];
  290. if (MathUtil_1.default.findSame(colError, first.address, 'address') == -1) {
  291. colError.push(first);
  292. }
  293. if (MathUtil_1.default.findSame(colError, address, 'address') == -1) {
  294. colError.push({ address: address, value: value });
  295. }
  296. }
  297. }
  298. }
  299. if (colError.length > 0) {
  300. errorlist.push({ name: headInfo.cols[col], list: colError });
  301. }
  302. }
  303. if (errorlist.length > 0) {
  304. var out_1 = '';
  305. errorlist.forEach(function (error) {
  306. out_1 += error.name + ' ';
  307. console.log("".concat(error.name, "\u884C"));
  308. error.list.forEach(function (unit) {
  309. console.log(unit);
  310. });
  311. });
  312. 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"));
  313. }
  314. // 遍历整张表,对每一列进行分析
  315. var isMultArr = [];
  316. var isArray = [];
  317. for (var col = 0; col < headInfo.cols.length; col++) {
  318. var arrayNum = 0;
  319. if (headInfo.keys[col].isArray) {
  320. var isOne = 1;
  321. var isTwo = 0;
  322. var isThree = 0;
  323. for (var row = headInfo.minRow; row <= headInfo.maxRow; row++) {
  324. var address = headInfo.cols[col] + row;
  325. if (!worksheet[address]) {
  326. // console.log(address);
  327. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u5355\u5143\u683C\u4E3A\u7A7A1\uFF01"));
  328. }
  329. if (worksheet[address].v == undefined) {
  330. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u6570\u636E\u9519\u8BEF2\uFF01"));
  331. }
  332. var value = worksheet[address].v;
  333. var result = this.checkArr(value, address, headInfo);
  334. if (result.isOneArray) {
  335. isOne++;
  336. }
  337. if (result.isTwoArray) {
  338. isTwo++;
  339. }
  340. if (result.isThreeArray) {
  341. isThree++;
  342. }
  343. }
  344. isMultArr.push(isTwo > 0 || isThree > 0);
  345. isArray.push(isOne > 0);
  346. if (isOne > 0) {
  347. arrayNum = 1;
  348. }
  349. if (isTwo > 0) {
  350. arrayNum = 2;
  351. }
  352. if (isThree > 0) {
  353. arrayNum = 3;
  354. }
  355. }
  356. else {
  357. isMultArr.push(false);
  358. isArray.push(false);
  359. }
  360. headInfo.keys[col].arrayNum = arrayNum;
  361. }
  362. // 初始化
  363. for (var row = headInfo.minRow; row <= headInfo.maxRow; row++) {
  364. var values = [];
  365. var address = '';
  366. // 获取一列的对象
  367. for (var col = 0; col < headInfo.cols.length; col++) {
  368. address = headInfo.cols[col] + row;
  369. if (worksheet[address] == undefined) {
  370. if (headInfo.cols[col] == 'A') {
  371. var lastAddress = headInfo.cols[headInfo.cols.length - 1] + row; // 获取最后一个的地址
  372. if (worksheet[lastAddress] == undefined) {
  373. if (row == 2) {
  374. console.log("\u8B66\u544A\uFF1A".concat(StringUtil_1.default.getCheckString('行' + row, headInfo), " \u53EF\u80FD\u662F\u7A7A\u8868\uFF01"));
  375. return {
  376. sheetname: headInfo.sheetname,
  377. jsobj: cobjlist,
  378. csvobj: sobjlist,
  379. headInfo: headInfo
  380. };
  381. }
  382. else {
  383. 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"));
  384. }
  385. }
  386. else {
  387. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(lastAddress, headInfo), " \u6570\u636E\u9519\u8BEF3\uFF01"));
  388. }
  389. }
  390. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u6570\u636E\u9519\u8BEF4\uFF01"));
  391. }
  392. var value = worksheet[address].v;
  393. // jump the value type of e and z (type: b Boolean, e Error, n Number, d Date, s Text, z Stub)
  394. if (worksheet[address].t == 'e' || worksheet[address].t == 'z') {
  395. continue;
  396. }
  397. if (value.toString().indexOf(',') != -1) {
  398. if (headInfo.keys[col].outputserver) {
  399. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u5355\u5143\u683C\u5185\u5B58\u5728\u82F1\u6587\u9017\u53F7\uFF0C\u5F71\u54CDcsv\u8F93\u51FA\uFF01"));
  400. }
  401. else if (headInfo.keys[col].outputclient) {
  402. var reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
  403. if (reg.test(value)) {
  404. 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"));
  405. }
  406. }
  407. }
  408. values.push(value);
  409. }
  410. if (headInfo.keys.length != values.length) {
  411. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u6570\u636E\u9519\u8BEF5\uFF01"));
  412. }
  413. var obj = this.getObj(values, isMultArr, isArray, headInfo, row);
  414. cobjlist.push(obj.c);
  415. sobjlist.push(obj.s);
  416. }
  417. return {
  418. sheetname: headInfo.sheetname,
  419. jsobj: cobjlist,
  420. csvobj: sobjlist,
  421. headInfo: headInfo
  422. };
  423. };
  424. return ExcelUtil;
  425. }());
  426. exports.default = ExcelUtil;