common_spread.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. * 工料机SpreadJS类
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/18
  6. * @version
  7. */
  8. /**
  9. * 构造函数
  10. *
  11. * @param {Object} header
  12. * @return {void}
  13. */
  14. function CommonSpreadJs (header) {
  15. this.header = header;
  16. this.spread = null;
  17. this.sheet = null;
  18. this.columnInfo = [];
  19. this.isChanging = false;
  20. }
  21. /**
  22. * 初始化
  23. *
  24. * @param {String} target
  25. * @return {Object}
  26. */
  27. CommonSpreadJs.prototype.init = function(target) {
  28. let setting = {
  29. header: []
  30. };
  31. this.columnInfo = [];
  32. for(let tmp of this.header) {
  33. setting.header.push({headerName: tmp.name, headerWidth: 120});
  34. this.columnInfo.push({name: tmp.field, displayName: tmp.name, visible: tmp.visible, cellType: tmp.cellType, size: 120});
  35. }
  36. this.spread = sheetCommonObj.buildSheet(document.getElementById(target), setting, 10);
  37. this.spread.options.scrollbarShowMax = true;
  38. this.spread.options.scrollbarMaxAlign = true;
  39. this.spread.options.showHorizontalScrollbar = true;
  40. this.sheet = this.spread.getActiveSheet();
  41. this.sheet.autoGenerateColumns = false;
  42. // 设置表单不可编辑
  43. this.sheet.options.isProtected = true;
  44. return this.spread;
  45. };
  46. /**
  47. * 获取对应字段的列号
  48. *
  49. * @param {String} field
  50. * @return {Number}
  51. */
  52. CommonSpreadJs.prototype.getFieldColumn = function(field) {
  53. let result = -1;
  54. if (this.header.length <= 0) {
  55. return result;
  56. }
  57. for (let tmp in this.header) {
  58. if (this.header[tmp].field === field) {
  59. result = tmp;
  60. break;
  61. }
  62. }
  63. return result;
  64. };
  65. /**
  66. * 根据列号获取对应的字段
  67. *
  68. * @param {Number} column
  69. * @return {String}
  70. */
  71. CommonSpreadJs.prototype.getColumnField = function(column) {
  72. let field = this.header[column] !== undefined && this.header[column].field !== undefined ?
  73. this.header[column].field : '';
  74. return field;
  75. };
  76. /**
  77. * 设置指定列可编辑
  78. *
  79. * @param {Number} column
  80. * @return {void}
  81. */
  82. CommonSpreadJs.prototype.setColumnEditable = function(column) {
  83. this.sheet.getRange(-1, column, -1, 1).locked(false);
  84. };
  85. /**
  86. * 设置指定列可编辑
  87. *
  88. *
  89. * @param {Number} column
  90. * @return {void}
  91. */
  92. CommonSpreadJs.prototype.setColumnLock = function(row, column) {
  93. this.sheet.getRange(-1, column, -1, 1).locked(true);
  94. };
  95. /**
  96. * 设置样式
  97. *
  98. * @param {Number} row
  99. * @param {Number} column
  100. * @param {Object} styleSetting
  101. */
  102. CommonSpreadJs.prototype.setStyle = function(row, column, styleSetting) {
  103. let style = new GC.Spread.Sheets.Style();
  104. style.locked = styleSetting.readOnly === undefined ? true : styleSetting.readOnly;
  105. style.hAlign = styleSetting.hAlign === undefined ? GC.Spread.Sheets.HorizontalAlign.center : styleSetting.hAlign;
  106. this.sheet.setStyle(row, column, style, GC.Spread.Sheets.SheetArea.viewport);
  107. };
  108. /**
  109. * 获取sheet对象
  110. *
  111. * @return {Object}
  112. */
  113. CommonSpreadJs.prototype.getSheet = function() {
  114. return this.sheet;
  115. };
  116. /**
  117. * 设置数据
  118. *
  119. * @param {Object} data
  120. * @return {void}
  121. */
  122. CommonSpreadJs.prototype.setData = function(data) {
  123. this.sheet.setDataSource(data);
  124. this.sheet.bindColumns(this.columnInfo);
  125. this.spread.refresh();
  126. };
  127. /**
  128. * 绑定事件
  129. *
  130. * @param {Number} event
  131. * @param {function} callback
  132. * @return {void}
  133. */
  134. CommonSpreadJs.prototype.bind = function (event, callback) {
  135. this.sheet.bind(event, callback);
  136. };
  137. /**
  138. * 查找指定数据并返回行号
  139. *
  140. * @param {String} keyword
  141. * @param {Number} rowStart
  142. * @param {Number} columnStart
  143. * @return {Number}
  144. */
  145. CommonSpreadJs.prototype.searchKeyword = function(keyword, rowStart = 0, columnStart = 0) {
  146. let condition = new GC.Spread.Sheets.Search.SearchCondition();
  147. condition.searchString = keyword;
  148. condition.startSheetIndex = 0;
  149. condition.endSheetIndex = 0;
  150. condition.searchFlags = GC.Spread.Sheets.Search.SearchFlags.ignoreCase | GC.Spread.Sheets.Search.SearchFlags.blockRange;
  151. condition.searchOrder = GC.Spread.Sheets.Search.SearchOrder.nOrder;
  152. condition.searchTarget = GC.Spread.Sheets.Search.SearchFoundFlags.cellText;
  153. condition.sheetArea = GC.Spread.Sheets.SheetArea.viewport;
  154. condition.rowStart = rowStart;
  155. condition.columnStart = columnStart;
  156. let result = this.spread.search(condition);
  157. return result.foundRowIndex;
  158. };
  159. /**
  160. * 过滤数据
  161. *
  162. * @param {String} field
  163. * @param {Array} filterList
  164. * @return {void}
  165. */
  166. CommonSpreadJs.prototype.filterData = function(field, filterList) {
  167. let fieldColumn = this.getFieldColumn(field);
  168. fieldColumn = parseInt(fieldColumn);
  169. let filter = new GC.Spread.Sheets.Filter.HideRowFilter(new GC.Spread.Sheets.Range(-1, 5, -1, 1));
  170. this.sheet.rowFilter(filter);
  171. let rowFilter = this.sheet.rowFilter();
  172. rowFilter.removeFilterItems(fieldColumn);
  173. for (let tmp of filterList) {
  174. let condition = new GC.Spread.Sheets.ConditionalFormatting.Condition(GC.Spread.Sheets.ConditionalFormatting.ConditionType.numberCondition, {
  175. compareType: GC.Spread.Sheets.ConditionalFormatting.GeneralComparisonOperators.equalsTo,
  176. expected: tmp
  177. });
  178. rowFilter.addFilterItem(fieldColumn, condition);
  179. }
  180. rowFilter.filter(fieldColumn);
  181. this.sheet.invalidateLayout();
  182. this.sheet.repaint();
  183. };
  184. /**
  185. * 选中指定行
  186. *
  187. * @param {Number} row
  188. * @return {void}
  189. */
  190. CommonSpreadJs.prototype.selectRow = function(row) {
  191. this.sheet.setSelection(row, 0, 1, 1);
  192. };
  193. /**
  194. * 验证数据
  195. *
  196. * @param {Number} column
  197. * @param {String} value
  198. * @return {boolean}
  199. */
  200. CommonSpreadJs.prototype.checkData = function(column, value) {
  201. let result = true;
  202. let validator = this.header[column].validator !== undefined ? this.header[column].validator : null;
  203. if (validator === null) {
  204. return result;
  205. }
  206. switch (validator) {
  207. case 'number':
  208. let regular = /^\d+(\.\d+)?$/;
  209. result = regular.test(value);
  210. break;
  211. case 'boolean':
  212. let booleanValue = [true, false];
  213. result = booleanValue.indexOf(value) >= 0;
  214. break;
  215. }
  216. return result;
  217. };
  218. /**
  219. * 设置对应字段数值
  220. *
  221. * @param {String} field
  222. * @param {Number} value
  223. * @param {boolean} appendMode
  224. * @return {void}
  225. */
  226. CommonSpreadJs.prototype.setCellByField = function(field, value, appendMode) {
  227. let row = this.sheet.getActiveRowIndex();
  228. let columnIndex = this.getFieldColumn(field);
  229. if (appendMode) {
  230. let oldValue = this.sheet.getValue(row, columnIndex);
  231. value = (oldValue + value).toDecimal(2);
  232. }
  233. this.sheet.setValue(row, columnIndex, value);
  234. };
  235. /**
  236. * 获取当前选中行指定字段的值
  237. *
  238. * @param {String} field
  239. * @return {Mixed}
  240. */
  241. CommonSpreadJs.prototype.getActiveDataByField = function(field) {
  242. let result = null;
  243. let activeGLJRow = this.sheet.getActiveRowIndex();
  244. let column = this.getFieldColumn(field);
  245. if (column < 0) {
  246. return result;
  247. }
  248. result = this.sheet.getValue(activeGLJRow, column);
  249. return result;
  250. };