common_spread.js 7.1 KB

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