common_util.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * Created by CSL on 2017-06-06.
  3. * public functions for web.
  4. */
  5. // 忽略大小写判断字符串是否和参数指定的字符串相同
  6. String.prototype.sameText = function (str) {
  7. return this.toLowerCase() == str.toLowerCase();
  8. };
  9. // 忽略大小写判断字符串是否有参数指定的子串
  10. String.prototype.hasSubStr = function (str) {
  11. return this.toLowerCase().indexOf(str.toLowerCase()) > -1;
  12. };
  13. // 判断字符串是否是数字形式的字符串
  14. String.prototype.isNumberStr = function () {
  15. return this == +this;
  16. };
  17. // 树结点计算时,取费会出现值为NaN的情况,导致往父节点汇总(递归相加)会出现错误。
  18. function parseFloatPlus(value){
  19. let rst = parseFloat(value);
  20. return isNaN(rst) ? 0 : rst;
  21. };
  22. // 数组合并,并去重复。
  23. Array.prototype.merge = function (arr) {
  24. if (arr.length > 0){
  25. for (let e of arr){
  26. if (!this.includes(e)) this.push(e);
  27. };
  28. }
  29. };
  30. // 数组是否包含另一个数组。
  31. Array.prototype.hasSubArr = function (subArr){
  32. for(var i = 0, len = subArr.length; i < len; i++){
  33. if(this.indexOf(subArr[i]) == -1) return false;
  34. }
  35. return true;
  36. };
  37. Array.prototype.delete = function (elem){
  38. let idx = this.findIndex(function (e){return e == elem});
  39. if (idx != -1) this.splice(idx, 1);
  40. };
  41. function seqString(num,length){
  42. var numstr = num.toString();
  43. var l=numstr.length;
  44. if (numstr.length>=length) {return numstr;}
  45. for(var i = 0 ;i<length - l;i++){
  46. numstr = "0" + numstr;
  47. }
  48. return numstr;
  49. };
  50. function customRowHeader(sheet, dataLength) {
  51. sheet.suspendPaint(); //提升焦点变换性能 2019年4月12日
  52. for (let i = 0; i < dataLength; i++) {
  53. sheet.setValue(i, 0, `F${i + 1}`, GC.Spread.Sheets.SheetArea.rowHeader);
  54. }
  55. sheet.resumePaint(); //提升焦点变换性能 2019年4月12日
  56. };
  57. function changePropNames(object, oldNames, newNames) {
  58. if (!object) return;
  59. for (let i = 0; i < oldNames.length; i++) {
  60. if (object[oldNames[i]]){
  61. object[newNames[i]] = object[oldNames[i]];
  62. delete object[oldNames[i]];
  63. };
  64. }
  65. };
  66. function changePropNames(object, oldNames, newNames) {
  67. if (!object) return;
  68. for (let i = 0; i < oldNames.length; i++) {
  69. if (object[oldNames[i]]){
  70. object[newNames[i]] = object[oldNames[i]];
  71. delete object[oldNames[i]];
  72. };
  73. }
  74. };
  75. function deletePropNames(object, namesArr) {
  76. if (!object) return;
  77. for (let name of namesArr){
  78. if (object[name]) delete object[name];
  79. };
  80. };
  81. function sortTreeChildren(lists) {//树结构排序
  82. let IDMap ={},nextMap = {}, firstNode = null,newList=[];
  83. for(let l of lists){
  84. if(l.children&&l.children.length > 0) l.children = sortChildren(l.children);//递规排序
  85. IDMap[l.ID] = l;
  86. if(l.NextSiblingID!=-1) nextMap[l.NextSiblingID] = l;
  87. }
  88. for(let t of lists){
  89. if(!nextMap[t.ID]){ //如果在下一节点映射没找到,则是第一个节点
  90. firstNode = t;
  91. break;
  92. }
  93. }
  94. if(firstNode){
  95. newList.push(firstNode);
  96. delete IDMap[firstNode.ID];
  97. setNext(firstNode,newList);
  98. }
  99. //容错处理,如果链断了的情况,直接添加到后面
  100. for(let key in IDMap){
  101. if(IDMap[key]) newList.push(IDMap[key])
  102. }
  103. return newList;
  104. function setNext(node,array) {
  105. if(node.NextSiblingID != -1){
  106. let next = IDMap[node.NextSiblingID];
  107. if(next){
  108. array.push(next);
  109. delete IDMap[next.ID];
  110. setNext(next,array);
  111. }
  112. }
  113. }
  114. }
  115. function setTreeChildern(children,list,parentMap){//按顺序设置树节点
  116. for(let c of children){
  117. list.push(c);
  118. if(parentMap[c.ID]){
  119. getChildern(parentMap[c.ID],list,parentMap)
  120. }
  121. }
  122. }