ExcelUtil.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. if (value.indexOf(Config_1.Config.TWO_ARRAY_SYMBOL) != -1) {
  157. if (value.indexOf(Config_1.Config.ONE_ARRAY_SYMBOL) != -1) {
  158. ret.isOneArray = true;
  159. ret.isTwoArray = true;
  160. ret.isThreeArray = true;
  161. }
  162. else {
  163. 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"));
  164. }
  165. }
  166. else {
  167. if (value.indexOf('color') == -1) {
  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\u4E09\u7EF4\u5206\u5272\u7B26\uFF0C\u4F46\u662F\u672A\u53D1\u73B0\u4E8C\u7EF4\u5206\u5272\u7B26\uFF01"));
  169. }
  170. }
  171. }
  172. if (value.indexOf(Config_1.Config.TWO_ARRAY_SYMBOL) != -1) {
  173. if (value.indexOf(Config_1.Config.ONE_ARRAY_SYMBOL) != -1) {
  174. ret.isOneArray = true;
  175. ret.isTwoArray = true;
  176. }
  177. else {
  178. 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"));
  179. }
  180. }
  181. if (value.indexOf(Config_1.Config.ONE_ARRAY_SYMBOL) != -1) {
  182. ret.isOneArray = true;
  183. }
  184. return ret;
  185. };
  186. ExcelUtil.tryGetArray = function (value, isMult, isNumber, canZeroStr) {
  187. var ret = '';
  188. value = StringUtil_1.default.replaceAll(value, '<color=#', '<color=@');
  189. var threeArr = value.split(Config_1.Config.THREEE_ARRAY_SYMBOL);
  190. for (var threeIndex = 0; threeIndex < threeArr.length; threeIndex++) {
  191. var twoArr = threeArr[threeIndex].split(Config_1.Config.TWO_ARRAY_SYMBOL);
  192. if (isMult) {
  193. ret += '[';
  194. }
  195. for (var twoIndex = 0; twoIndex < twoArr.length; twoIndex++) {
  196. var oneArr = twoArr[twoIndex].split(Config_1.Config.ONE_ARRAY_SYMBOL);
  197. ret += '[';
  198. for (var oneIndex = 0; oneIndex < oneArr.length; oneIndex++) {
  199. var oneUnit = oneArr[oneIndex];
  200. if (MathUtil_1.default.isNumber(oneUnit) && isNumber) {
  201. ret += oneUnit;
  202. }
  203. else {
  204. if ((canZeroStr && oneUnit.toString() == '0') || oneUnit.toString() != '0') {
  205. ret += "'".concat(oneUnit, "'"); // 支持字符串数组
  206. }
  207. }
  208. if (oneIndex != oneArr.length - 1) {
  209. ret += ', ';
  210. }
  211. }
  212. ret += '], ';
  213. }
  214. if (ret.substr(ret.length - 2, 2) == ', ') {
  215. ret = ret.substr(0, ret.length - 2);
  216. }
  217. if (isMult) {
  218. ret += '], ';
  219. }
  220. }
  221. if (ret.substr(ret.length - 2, 2) == ', ') {
  222. ret = ret.substr(0, ret.length - 2);
  223. }
  224. if (threeArr.length > 1) {
  225. ret = "[".concat(ret, "]");
  226. }
  227. ret = StringUtil_1.default.replaceAll(ret, '<color=@', '<color=#');
  228. return ret;
  229. };
  230. ExcelUtil.getObj = function (values, isMultArr, isArray, headInfo, row) {
  231. var cobj = {};
  232. var sobj = {};
  233. for (var i = 0; i < values.length; i++) {
  234. var temp = values[i].toString();
  235. temp = StringUtil_1.default.replaceDoubleMid(temp, '$', function (mid) { return (parseInt(mid, 10) * Config_1.Config.yuanRate).toString(); }, true);
  236. temp = StringUtil_1.default.replaceDoubleMid(temp, '¥', function (mid) { return Config_1.Config.replaceStr ? Config_1.Config.replaceStr : mid; });
  237. if (headInfo.keys[i].outputserver) {
  238. sobj[headInfo.keys[i].name] = temp;
  239. }
  240. if (headInfo.keys[i].outputclient) {
  241. var address = headInfo.cols[i] + row;
  242. if (headInfo.keys[i].type == 'number' || headInfo.keys[i].type == 'number[]') {
  243. if (headInfo.keys[i].isArray) {
  244. headInfo.keys[i].type = 'number[]';
  245. temp = this.tryGetArray(temp, isMultArr[i], true);
  246. var regex = /^[0-9\[\],"\s\-.]+$/;
  247. if (!regex.test(temp))
  248. 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"));
  249. }
  250. else {
  251. temp = Number(temp);
  252. if (isNaN(temp)) {
  253. 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"));
  254. }
  255. }
  256. }
  257. else if (headInfo.keys[i].type == 'string' || headInfo.keys[i].type == 'string[]') {
  258. if (isArray[i]) {
  259. headInfo.keys[i].type = 'string[]';
  260. temp = this.tryGetArray(temp, isMultArr[i], false, headInfo.keys[i].canZeroStr);
  261. }
  262. else {
  263. if (!headInfo.keys[i].canZeroStr && temp == '0')
  264. temp = '';
  265. }
  266. }
  267. cobj[headInfo.keys[i].name] = temp;
  268. }
  269. }
  270. return {
  271. c: cobj,
  272. s: sobj,
  273. };
  274. };
  275. ExcelUtil.creatObj = function (headInfo) {
  276. var worksheet = headInfo.worksheet;
  277. var cobjlist = [];
  278. var sobjlist = [];
  279. var errorlist = [];
  280. for (var col = 0; col < headInfo.cols.length; col++) {
  281. var uniq = [];
  282. var colError = [];
  283. if (headInfo.keys[col].isUniq) {
  284. for (var row = headInfo.minRow; row <= headInfo.maxRow; row++) {
  285. var address = headInfo.cols[col] + row;
  286. if (!worksheet[address]) {
  287. // console.log(address);
  288. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u5355\u5143\u683C\u4E3A\u7A7A0\uFF01"));
  289. }
  290. if (worksheet[address].v == undefined) {
  291. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u6570\u636E\u9519\u8BEF1\uFF01"));
  292. }
  293. var value = worksheet[address].v;
  294. var index = MathUtil_1.default.findSame(uniq, value, 'value');
  295. if (index == -1) {
  296. uniq.push({ address: address, value: value });
  297. }
  298. else {
  299. var first = uniq[index];
  300. if (MathUtil_1.default.findSame(colError, first.address, 'address') == -1) {
  301. colError.push(first);
  302. }
  303. if (MathUtil_1.default.findSame(colError, address, 'address') == -1) {
  304. colError.push({ address: address, value: value });
  305. }
  306. }
  307. }
  308. }
  309. if (colError.length > 0) {
  310. errorlist.push({ name: headInfo.cols[col], list: colError });
  311. }
  312. }
  313. if (errorlist.length > 0) {
  314. var out_1 = '';
  315. errorlist.forEach(function (error) {
  316. out_1 += error.name + ' ';
  317. console.log("".concat(error.name, "\u884C"));
  318. error.list.forEach(function (unit) {
  319. console.log(unit);
  320. });
  321. });
  322. 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"));
  323. }
  324. // 遍历整张表,对每一列进行分析
  325. var isMultArr = [];
  326. var isArray = [];
  327. for (var col = 0; col < headInfo.cols.length; col++) {
  328. var arrayNum = 0;
  329. if (headInfo.keys[col].isArray) {
  330. var isOne = 1;
  331. var isTwo = 0;
  332. var isThree = 0;
  333. for (var row = headInfo.minRow; row <= headInfo.maxRow; row++) {
  334. var address = headInfo.cols[col] + row;
  335. if (!worksheet[address]) {
  336. // console.log(address);
  337. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u5355\u5143\u683C\u4E3A\u7A7A1\uFF01"));
  338. }
  339. if (worksheet[address].v == undefined) {
  340. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u6570\u636E\u9519\u8BEF2\uFF01"));
  341. }
  342. var value = worksheet[address].v;
  343. var result = this.checkArr(value, address, headInfo);
  344. if (result.isOneArray) {
  345. isOne++;
  346. }
  347. if (result.isTwoArray) {
  348. isTwo++;
  349. }
  350. if (result.isThreeArray) {
  351. isThree++;
  352. }
  353. }
  354. isMultArr.push(isTwo > 0 || isThree > 0);
  355. isArray.push(isOne > 0);
  356. if (isOne > 0) {
  357. arrayNum = 1;
  358. }
  359. if (isTwo > 0) {
  360. arrayNum = 2;
  361. }
  362. if (isThree > 0) {
  363. arrayNum = 3;
  364. }
  365. }
  366. else {
  367. isMultArr.push(false);
  368. isArray.push(false);
  369. }
  370. headInfo.keys[col].arrayNum = arrayNum;
  371. }
  372. // 初始化
  373. for (var row = headInfo.minRow; row <= headInfo.maxRow; row++) {
  374. var values = [];
  375. var address = '';
  376. // 获取一列的对象
  377. for (var col = 0; col < headInfo.cols.length; col++) {
  378. address = headInfo.cols[col] + row;
  379. if (worksheet[address] == undefined) {
  380. if (headInfo.cols[col] == 'A') {
  381. var lastAddress = headInfo.cols[headInfo.cols.length - 1] + row; // 获取最后一个的地址
  382. if (worksheet[lastAddress] == undefined) {
  383. if (row == 2) {
  384. console.log("\u8B66\u544A\uFF1A".concat(StringUtil_1.default.getCheckString('行' + row, headInfo), " \u53EF\u80FD\u662F\u7A7A\u8868\uFF01"));
  385. return {
  386. sheetname: headInfo.sheetname,
  387. jsobj: cobjlist,
  388. csvobj: sobjlist,
  389. headInfo: headInfo
  390. };
  391. }
  392. else {
  393. 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"));
  394. }
  395. }
  396. else {
  397. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(lastAddress, headInfo), " \u6570\u636E\u9519\u8BEF3\uFF01"));
  398. }
  399. }
  400. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u6570\u636E\u9519\u8BEF4\uFF01"));
  401. }
  402. var value = worksheet[address].v;
  403. // jump the value type of e and z (type: b Boolean, e Error, n Number, d Date, s Text, z Stub)
  404. if (worksheet[address].t == 'e' || worksheet[address].t == 'z') {
  405. continue;
  406. }
  407. if (value.toString().indexOf(',') != -1) {
  408. if (headInfo.keys[col].outputserver) {
  409. 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"));
  410. }
  411. else if (headInfo.keys[col].outputclient) {
  412. var reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
  413. if (reg.test(value)) {
  414. 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"));
  415. }
  416. }
  417. }
  418. values.push(value);
  419. }
  420. if (headInfo.keys.length != values.length) {
  421. Config_1.Config.checkState(Config_1.RetCode.EXCEL_DATA_ERROR, "".concat(StringUtil_1.default.getCheckString(address, headInfo), " \u6570\u636E\u9519\u8BEF5\uFF01"));
  422. }
  423. var obj = this.getObj(values, isMultArr, isArray, headInfo, row);
  424. cobjlist.push(obj.c);
  425. sobjlist.push(obj.s);
  426. }
  427. return {
  428. sheetname: headInfo.sheetname,
  429. jsobj: cobjlist,
  430. csvobj: sobjlist,
  431. headInfo: headInfo
  432. };
  433. };
  434. return ExcelUtil;
  435. }());
  436. exports.default = ExcelUtil;