common_spread.js 7.0 KB

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