sheet_common.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. var sheetCommonObj = {
  5. // CSL.2017.06.05
  6. // createSpread、initSheet 在一个Spread多个Sheet分别调用时的情况下使用。
  7. createSpread: function (container, SheetCount) {
  8. var me = this;
  9. var spreadBook = new GC.Spread.Sheets.Workbook(container, { sheetCount: SheetCount });
  10. spreadBook.options.tabStripVisible = false;
  11. spreadBook.options.showHorizontalScrollbar = true;
  12. spreadBook.options.showVerticalScrollbar = true;
  13. spreadBook.options.allowCopyPasteExcelStyle = false;
  14. spreadBook.options.allowUserDragDrop = true;
  15. spreadBook.options.allowContextMenu = false;
  16. spreadBook.options.allowUserEditFormula = false;
  17. spreadBook.options.showDragFillSmartTag = false;
  18. return spreadBook;
  19. },
  20. initSheet: function (sheet, setting, rowCount) {
  21. var me = this;
  22. var spreadNS = GC.Spread.Sheets;
  23. sheet.suspendPaint();
  24. sheet.suspendEvent();
  25. if (setting.frozenCols) sheet.frozenColumnCount(setting.frozenCols);//冻结列
  26. sheet.setRowCount(1, spreadNS.SheetArea.colHeader);
  27. sheet.setColumnCount(setting.header.length, spreadNS.SheetArea.viewport);
  28. if (setting && setting.view && setting.view.colHeaderHeight) {
  29. sheet.setRowHeight(0, setting.view.colHeaderHeight, spreadNS.SheetArea.colHeader);
  30. };
  31. if (setting && setting.view && setting.view.rowHeaderWidth) {
  32. sheet.setColumnWidth(0, setting.view.rowHeaderWidth, spreadNS.SheetArea.rowHeader);
  33. };
  34. if (setting.emptyRowHeader) {
  35. sheet.setColumnWidth(0, 1, GC.Spread.Sheets.SheetArea.rowHeader);
  36. }
  37. sheet.options.colHeaderAutoTextIndex = 1;
  38. sheet.options.colHeaderAutoText = spreadNS.HeaderAutoText.numbers;
  39. sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  40. sheet.options.protectionOptions = {
  41. allowResizeColumns: true
  42. };
  43. sheet.showRowOutline(false);
  44. sheet.options.allowCellOverflow = false;
  45. me.buildHeader(sheet, setting);
  46. if (rowCount > 0)
  47. sheet.setRowCount(rowCount);
  48. else
  49. sheet.setRowCount(1);
  50. sheet.resumeEvent();
  51. sheet.resumePaint();
  52. },
  53. // buildSheet 在一个Spread、一个Sheet的情况下使用。
  54. buildSheet: function (container, setting, rowCount) {
  55. var me = this;
  56. var spreadBook = me.createSpread(container, { sheetCount: 1 });
  57. var sheet = spreadBook.getSheet(0);
  58. me.initSheet(sheet, setting, rowCount);
  59. return spreadBook;
  60. },
  61. buildHeader: function (sheet, setting) {
  62. var me = this, ch = GC.Spread.Sheets.SheetArea.colHeader;
  63. for (var i = 0; i < setting.header.length; i++) {
  64. sheet.setValue(0, i, setting.header[i].headerName, ch);
  65. sheet.getCell(0, i, ch).wordWrap(true);
  66. sheet.setColumnWidth(i, setting.header[i].headerWidth ? setting.header[i].headerWidth : 100);
  67. sheet.setColumnVisible(i, setting.header[i].visible === false ? false : true);
  68. }
  69. },
  70. cleanSheet: function (sheet, setting, rowCount) {
  71. sheet.suspendPaint();
  72. sheet.suspendEvent();
  73. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  74. if (rowCount > 0) sheet.setRowCount(rowCount);
  75. sheet.clearSelection();
  76. sheet.resumeEvent();
  77. sheet.resumePaint();
  78. },
  79. cleanData: function (sheet, setting, rowCount) {
  80. sheet.suspendPaint();
  81. sheet.suspendEvent();
  82. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  83. if (rowCount > 0) sheet.setRowCount(rowCount);
  84. sheet.resumeEvent();
  85. sheet.resumePaint();
  86. },
  87. setAreaAlign: function (area, hAlign, vAlign) {
  88. if (!(hAlign) || hAlign === "left") {
  89. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  90. } else if (hAlign === "right") {
  91. area.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  92. } else if (hAlign === "center") {
  93. area.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  94. } else {
  95. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  96. }
  97. if (!(vAlign) || vAlign === "center") {
  98. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  99. } else if (vAlign === "top") {
  100. area.vAlign(GC.Spread.Sheets.VerticalAlign.top);
  101. } else if (vAlign === "bottom") {
  102. area.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
  103. } else {
  104. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  105. }
  106. },
  107. showData: function (sheet, setting, data, distTypeTree, callback) {//这个callback是为了在showdata后还做了引起重画表格的操作,在callback里调用能提高效率
  108. var me = this, ch = GC.Spread.Sheets.SheetArea.viewport;
  109. sheet.suspendPaint();
  110. sheet.suspendEvent();
  111. //sheet.addRows(row, 1);
  112. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  113. if (sheet.getRowCount() < data.length) {
  114. data.length < 30 ? sheet.setRowCount(30) : sheet.setRowCount(data.length);
  115. } else if (sheet.getRowCount() == 0) {
  116. sheet.setRowCount(30);
  117. }
  118. for (var col = 0; col < setting.header.length; col++) {
  119. var hAlign = "left", vAlign = "center";
  120. if (setting.header[col].hAlign) {
  121. hAlign = setting.header[col].hAlign;
  122. } else if (setting.header[col].dataType !== "String") {
  123. hAlign = "right";
  124. }
  125. vAlign = setting.header[col].vAlign ? setting.header[col].vAlign : vAlign;
  126. me.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
  127. if (setting.header[col].formatter) {
  128. sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
  129. }
  130. /* 直接在showrowdata时当val为null时返回一个text类型的单元格
  131. if (setting.header[col].cellType === "checkBox" || setting.header[col].cellType === "button") {//clear and reset
  132. var me = this, header = GC.Spread.Sheets.SheetArea.colHeader;
  133. sheet.deleteColumns(col, 1);
  134. sheet.addColumns(col, 1);
  135. sheet.setValue(0, col, setting.header[col].headerName, header);
  136. sheet.setColumnWidth(col, setting.header[col].headerWidth ? setting.header[col].headerWidth : 100);
  137. } */
  138. if (setting.header[col].visible !== null && setting.header[col].visible !== undefined) {
  139. sheet.setColumnVisible(col, setting.header[col].visible);
  140. }
  141. sheet.getCell(0, col, GC.Spread.Sheets.SheetArea.colHeader).wordWrap(true);
  142. }
  143. for (var row = 0; row < data.length; row++) {
  144. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  145. this.showRowData(sheet, setting, row, data, distTypeTree);
  146. }
  147. if (setting.emptyRowHeader) {
  148. let rowCount = sheet.getRowCount();
  149. for (let row = 0; row < rowCount; row++) {
  150. sheet.setValue(row, 0, '', GC.Spread.Sheets.SheetArea.rowHeader);
  151. }
  152. }
  153. this.lockCells(sheet, setting);
  154. if (callback) callback();
  155. sheet.resumeEvent();
  156. sheet.resumePaint();
  157. //me.shieldAllCells(sheet);
  158. },
  159. showRowData: function (sheet, setting, row, data, distTypeTree = null) {
  160. let ch = GC.Spread.Sheets.SheetArea.viewport;
  161. for (var col = 0; col < setting.header.length; col++) {
  162. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  163. var val = _.get(data[row],setting.header[col].dataCode);
  164. if (val && setting.header[col].dataType === "Number") {
  165. if (setting.header[col].hasOwnProperty('tofix')) {
  166. val = scMathUtil.roundToString(val, setting.header[col].tofix);
  167. }
  168. else if (setting.header[col].hasOwnProperty('decimalField')) {
  169. var decimal = getDecimal(setting.header[col].decimalField);
  170. val = scMathUtil.roundToString(val, decimal);
  171. sheet.setFormatter(-1, col, getFormatter(decimal), GC.Spread.Sheets.SheetArea.viewport);
  172. } else {
  173. val = val + '';
  174. }
  175. }
  176. if (setting.header[col].cellType === "checkBox") {
  177. this.setCheckBoxCell(row, col, sheet, val)
  178. }
  179. if (setting.header[col].cellType === "comboBox") {
  180. this.setComboBox(row, col, sheet, setting.header[col].options, setting.header[col].editorValueType, setting.header[col].editable, setting.header[col].maxDropDownItems);
  181. }
  182. if (setting.header[col].cellType === "selectButton") {
  183. this.setSelectButton(row, col, sheet, setting.header[col]);
  184. }
  185. if (setting.header[col].cellType === "replaceButton") {
  186. this.setReplaceButton(row, col, sheet, setting.header[col]);
  187. }
  188. if (setting.header[col].cellType === "cusButton") {
  189. this.setCusButton(row, col, sheet, setting);
  190. }
  191. if (setting.header[col].cellType === "tipsCell") {
  192. this.setTipsCell(row, col, sheet, setting.header[col]);
  193. }
  194. if (setting.owner === 'gljTree') {
  195. if (setting.header[col].cellType === "checkBox") {
  196. val= val == 1 ? val : 0;
  197. this.setCheckBoxCell(row, col, sheet, val);
  198. }
  199. if (setting.header[col].dataCode === 'gljType' && data[row].gljType) {
  200. let distTypeVal = distTypeTree.distTypes[distTypeTree.prefix + data[row].gljType].data.fullName;
  201. val = distTypeVal;
  202. }
  203. }
  204. if (setting.header[col].getText) {
  205. val = setting.getText[setting.header[col].getText](data[row], val)
  206. }
  207. sheet.setValue(row, col, val, ch);
  208. }
  209. this.setRowStyle(row, sheet, data[row].bgColour);
  210. if (setting.autoFit == true) {//设置自动行高
  211. if (setting.fitRow && setting.fitRow.length > 0) {//如果有设置特定的某些列才需要自动行高就按设置的来,没有设置就默认所有列
  212. for (let dataCode of setting.fitRow) {
  213. let col = _.findIndex(setting.header, { dataCode: dataCode });
  214. sheet.getCell(row, col).wordWrap(true);
  215. }
  216. } else {
  217. sheet.getRange(row, -1, 1, -1, GC.Spread.Sheets.SheetArea.viewport).wordWrap(true);
  218. }
  219. sheet.autoFitRow(row);
  220. }
  221. if (setting.getStyle && setting.getStyle(data[row], row, sheet.getActiveRowIndex())) {
  222. sheet.setStyle(row, -1, setting.getStyle(data[row]));
  223. }
  224. },
  225. showTreeData:function (sheet,setting,data) {
  226. let ch = GC.Spread.Sheets.SheetArea.viewport;
  227. let parentMap=_.groupBy(data, 'ParentID');
  228. let visibleMap = {};
  229. let styleRow=[];
  230. let treeCol = setting.treeCol?setting.treeCol:0;
  231. sheet.suspendPaint();
  232. sheet.suspendEvent();
  233. for (let col = 0; col < setting.header.length; col++) {
  234. let hAlign = "left", vAlign = "center";
  235. if (setting.header[col].hAlign) {
  236. hAlign = setting.header[col].hAlign;
  237. } else if (setting.header[col].dataType !== "String"){
  238. hAlign = "right";
  239. }
  240. vAlign = setting.header[col].vAlign?setting.header[col].vAlign:vAlign;
  241. sheetCommonObj.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
  242. if (setting.header[col].formatter) {
  243. sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
  244. }
  245. if(setting.header[col].cellType === "comboBox"){
  246. this.setComboBox(-1,col,sheet,setting.header[col].options,setting.header[col].editorValueType,setting.header[col].editable,setting.header[col].maxDropDownItems);
  247. }
  248. for (let row = 0; row < data.length; row++) {
  249. if(data[row].cellType === 'comboBox'){
  250. let options = data[row].options ? data[row].options.split("@") : [];
  251. this.setComboBox(row,col,sheet,options);
  252. }else if(data[row].cellType === 'String'){//默认设置字符输入,避免出现输入10:01变成日期的情况
  253. sheet.setFormatter(row, col,"@", GC.Spread.Sheets.SheetArea.viewport);
  254. }
  255. let val = data[row][setting.header[col].dataCode];
  256. if(val&&setting.header[col].dataType === "Number"){
  257. if(setting.header[col].hasOwnProperty('decimalField')){
  258. let decimal = getDecimal(setting.header[col].decimalField);
  259. val =scMathUtil.roundToString(val,decimal);
  260. sheet.setFormatter(-1, col,getFormatter(decimal), GC.Spread.Sheets.SheetArea.viewport);
  261. }else {
  262. val = val + '';
  263. }
  264. }
  265. if(val!=null && setting.header[col].cellType == "checkBox"){
  266. this.setCheckBoxCell(row,col,sheet,val);
  267. }
  268. if(setting.header[col].getText) {
  269. val = setting.getText[setting.header[col].getText](data[row], val)
  270. }
  271. sheet.setValue(row, col, val, ch);
  272. if(col==treeCol){
  273. let treeType = sheetCommonObj.getTreeNodeCellType(data,row,parentMap,treeCol);
  274. sheet.getCell(row, col).cellType(treeType);
  275. visibleMap[data[row].ID] = treeType.collapsed;
  276. this.setRowVisible(data,row,visibleMap,sheet);
  277. }
  278. if(data[row].bgColour) styleRow.push(row)
  279. }
  280. }
  281. for(let r of styleRow){
  282. this.setRowStyle(r,sheet,data[r].bgColour);
  283. }
  284. this.lockCells(sheet,setting);
  285. sheet.resumeEvent();
  286. sheet.resumePaint();
  287. },
  288. setRowVisible:function (data,row,visibleMap,sheet) {
  289. sheet.getRange(row , -1, 1, -1).visible(getVisible(data[row].ParentID));//显示或隐藏
  290. function getVisible(ParentID) {
  291. if(visibleMap[ParentID]) return false //如果父节点是缩起的,那就隐藏本身。
  292. if(visibleMap[ParentID] == false){//如果父节点不是缩起的,要再往父节点找看
  293. let pnode = _.find(data,{'ID':ParentID});
  294. if(pnode) return getVisible(pnode.ParentID);//如果有父节点,递归调用
  295. return true;//没有,返回显示
  296. }
  297. }
  298. },
  299. checkData: function (col, setting, value) {
  300. let result = true;
  301. let validator = setting.header[col].validator !== undefined ? setting.header[col].validator : null;
  302. if (validator === null) {
  303. return result;
  304. }
  305. switch (validator) {
  306. case 'number':
  307. let regular = /^\d+(\.\d+)?$/;
  308. result = regular.test(value);
  309. break;
  310. case 'boolean':
  311. let booleanValue = [true, false];
  312. result = booleanValue.indexOf(value) >= 0;
  313. break;
  314. }
  315. return result;
  316. },
  317. analyzePasteData: function (setting, pastedInfo) {
  318. var rst = [], propId = pastedInfo.cellRange.col, preStrIdx = 0, itemObj = {};//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  319. for (var i = 0; i < pastedInfo.pasteData.text.length; i++) {
  320. if (pastedInfo.pasteData.text[i] === "\n") {
  321. propId = pastedInfo.cellRange.col;//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  322. preStrIdx = i + 1;
  323. rst.push(itemObj);
  324. if (i < pastedInfo.pasteData.text.length - 1) {
  325. itemObj = {};
  326. }
  327. } else if (pastedInfo.pasteData.text[i] === "\t" || pastedInfo.pasteData.text[i] === "\r") {
  328. if (setting.header[propId]) {
  329. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx, i);
  330. }
  331. propId++;
  332. preStrIdx = i + 1;
  333. //if the last copied-cell were empty, should check whether the end of text
  334. if (i == pastedInfo.pasteData.text.length - 1 && setting.header[propId]) {
  335. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  336. rst.push(itemObj);
  337. }
  338. } else if (i == pastedInfo.pasteData.text.length - 1 && setting.header[propId]) {
  339. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  340. rst.push(itemObj);
  341. }
  342. }
  343. return rst;
  344. },
  345. combineRowData: function (sheet, setting, row) {
  346. var rst = {};
  347. for (var col = 0; col < setting.header.length; col++) {
  348. rst[setting.header[col].dataCode] = sheet.getValue(row, col);
  349. }
  350. return rst;
  351. },
  352. shieldAllCells: function (sheet) {
  353. sheet.options.isProtected = true;
  354. },
  355. unShieldAllCells: function (sheet) {
  356. sheet.options.isProtected = false;
  357. },
  358. lockCells: function (sheet, setting) {
  359. if (setting && setting.view.lockColumns && setting.view.lockColumns.length > 0) {
  360. sheet.options.isProtected = true;
  361. sheet.getRange(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport).locked(false);
  362. for (var i = 0; i < setting.view.lockColumns.length; i++) {
  363. let col = setting.view.lockColumns[i];
  364. if (_.isString(col)) {//如果是dataCode 进行转换
  365. col = _.findIndex(setting.header, { dataCode: col })
  366. }
  367. sheet.getRange(-1, col, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  368. }
  369. }
  370. },
  371. setCheckBoxCell(row, col, sheet, val) {
  372. var c = val==null?new GC.Spread.Sheets.CellTypes.Text():new GC.Spread.Sheets.CellTypes.CheckBox();
  373. if(val != null) c.isThreeState(false);
  374. sheet.setCellType(row, col, c, GC.Spread.Sheets.SheetArea.viewport);
  375. sheet.getCell(row, col).value(val);
  376. sheet.getCell(row, col).hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  377. },
  378. setComboBox(row, col, sheet, options, editorValueType, editable, maxDropDownItems) {
  379. //let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  380. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  381. if (options) {
  382. dynamicCombo.items(options);
  383. if (editorValueType == true) {
  384. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  385. }
  386. if (editable) dynamicCombo.editable(true);
  387. if (maxDropDownItems) dynamicCombo.maxDropDownItems(maxDropDownItems);
  388. }
  389. sheet.setCellType(row, col, dynamicCombo, GC.Spread.Sheets.SheetArea.viewport);
  390. },
  391. setRowStyle(row, sheet, bgColour) {
  392. if (bgColour) {
  393. let style = new GC.Spread.Sheets.Style();
  394. style.backColor = bgColour;
  395. style.borderLeft = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  396. style.borderTop = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  397. style.borderRight = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  398. style.borderBottom = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  399. sheet.setStyle(row, -1, style);
  400. }
  401. },
  402. getCustomerCoeCellType: function (htmlGenerator, setEditorValue, updateCallback) {
  403. let me = this;
  404. function CustomerCoeCellType() {
  405. this.isEscKey = false;
  406. this.displayText = '';
  407. }
  408. CustomerCoeCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  409. CustomerCoeCellType.prototype.createEditorElement = function (context) {
  410. console.log("create editor")
  411. let element = document.createElement("div");//这里创建的,会自动销毁
  412. return element
  413. };
  414. CustomerCoeCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  415. if (editorContext) {
  416. $editor = $(editorContext);
  417. $editor.css("position", "fixed");
  418. $editor.css("background", "white");
  419. //$editor.css("width", cellRect.width); 2018-11-15 改成固定列宽
  420. $editor.css("width", 160);
  421. $editor.attr("gcUIElement", "gcEditingInput");
  422. if (htmlGenerator) htmlGenerator(context, cellRect, $editor);
  423. }
  424. }
  425. CustomerCoeCellType.prototype.deactivateEditor = function (editorContext, context) {
  426. };
  427. CustomerCoeCellType.prototype.setEditorValue = function (editor, value, context) {
  428. console.log("set editor value");
  429. this.displayText = value;
  430. };
  431. CustomerCoeCellType.prototype.getEditorValue = function (editor, context) {
  432. console.log("get value");
  433. if (this.isEscKey != true && updateCallback) {
  434. updateCallback();
  435. }
  436. this.isEscKey = false;
  437. return this.displayText;
  438. };
  439. CustomerCoeCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  440. console.log(" update editor");
  441. if (setEditorValue) {//不是esc时才更新
  442. setEditorValue(context);
  443. }
  444. };
  445. CustomerCoeCellType.prototype.isReservedKey = function (e, context) {
  446. //cell type handle tab key by itself
  447. this.isEscKey = e.keyCode === GC.Spread.Commands.Key.esc;
  448. return false;
  449. };
  450. return new CustomerCoeCellType();
  451. },
  452. scrollSheetForOption: function (sheet, cxt, cellRect, row, options) {
  453. let topRow = sheet.getViewportTopRow(1);
  454. if (row == topRow) return;//已经是最顶行了
  455. let length = options && options.length > 0 ? options.length : 1;
  456. let height = cxt.canvas.height;
  457. let startY = cellRect.y + cellRect.height;//下拉框的起始显示位置
  458. let endY = startY + length * cellRect.height;//下拉框的结束显示位置
  459. if (endY <= height) return; //如果没有超出显示位置,直接返回
  460. let overRow = Math.ceil((endY - height) / cellRect.height);//超出的行数
  461. let showRow = topRow + overRow > row ? row : topRow + overRow;
  462. sheet.showRow(showRow, GC.Spread.Sheets.VerticalPosition.top);
  463. },
  464. setSelectButton(row, col, sheet, header) {
  465. sheet.setCellType(row, col, this.getSelectButton(header.headerWidth), GC.Spread.Sheets.SheetArea.viewport);
  466. },
  467. getSelectButton(cellWidth = 100) {
  468. function moreButton() {
  469. }
  470. moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  471. moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  472. GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
  473. ctx.font = '14px Calibri';
  474. let buttonW = cellWidth / 5;
  475. let endX = x + w - 2;
  476. if (value) {
  477. let textWidth = ctx.measureText(value).width;
  478. let spaceWidth = cellWidth - buttonW;
  479. let textEndX = x + textWidth + 2;
  480. if (spaceWidth < textWidth) {
  481. for (let i = value.length - 1; i > 1; i--) {
  482. let newValue = value.substr(0, i);
  483. let newTestWidth = ctx.measureText(newValue).width;
  484. if (spaceWidth > newTestWidth) {
  485. value = newValue;
  486. textEndX = x + newTestWidth + 2;
  487. break;
  488. }
  489. }
  490. }
  491. ctx.fillText(value, textEndX, y + h - 6);
  492. }
  493. //画三个点
  494. ctx.save();
  495. ctx.beginPath();
  496. ctx.arc(endX - buttonW / 2, y + h / 2, 1, 0, 360, false);
  497. ctx.arc(endX - buttonW / 2 - 4, y + h / 2, 1, 0, 360, false);
  498. ctx.arc(endX - buttonW / 2 + 4, y + h / 2, 1, 0, 360, false);
  499. ctx.fillStyle = "black";//填充颜色,默认是黑色
  500. ctx.fill();//画实心圆
  501. ctx.closePath();
  502. ctx.restore();
  503. };
  504. moreButton.prototype.processMouseLeave = function (hitinfo) {
  505. let newCell = new selectButton();
  506. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
  507. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
  508. };
  509. function selectButton() {
  510. }
  511. selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
  512. selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  513. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  514. };
  515. selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  516. return {
  517. x: x,
  518. y: y,
  519. row: context.row,
  520. col: context.col,
  521. cellStyle: cellStyle,
  522. cellRect: cellRect,
  523. sheetArea: context.sheetArea
  524. };
  525. };
  526. selectButton.prototype.processMouseDown = function (hitinfo) {
  527. if (hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked() != true) {
  528. let b1 = new moreButton();
  529. b1.marginLeft(cellWidth * 4 / 5);
  530. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
  531. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
  532. }
  533. };
  534. return new selectButton();
  535. },
  536. setCusButton: function (row, col, sheet, setting) {
  537. let functionName = setting.header[col].callback;
  538. let readOnly = setting.disable[setting.header[col].disable];
  539. if (typeof (readOnly) == 'function') {
  540. readOnly = readOnly(row, col);
  541. }
  542. if (functionName) {
  543. sheet.setCellType(row, col, this.getCusButtonCellType(setting.callback[functionName], readOnly));
  544. }
  545. //sheet.setCellType(row, col,this.getSelectButton(header.headerWidth),GC.Spread.Sheets.SheetArea.viewport);
  546. },
  547. getCusButtonCellType: function (callback, readOnly = false) {
  548. var ns = GC.Spread.Sheets;
  549. function CusButtonCellType() {
  550. }
  551. CusButtonCellType.prototype = new ns.CellTypes.Text();
  552. CusButtonCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  553. if (!readOnly) {
  554. if (options.sheet.getActiveRowIndex() == options.row && options.sheet.getActiveColumnIndex() == options.col) {
  555. var image = document.getElementById('f_btn'), imageMagin = 3;
  556. var imageHeight = 15;
  557. var imageWidth = 25;
  558. var imageX = x + w - imageWidth - imageMagin, imageY = y + h / 2 - imageHeight / 2;
  559. ctx.save();
  560. if (style.backColor) {
  561. ctx.fillStyle = style.backColor;
  562. ctx.fillRect(x, y, w, h);
  563. }
  564. ctx.drawImage(image, imageX, imageY, imageWidth, imageHeight);
  565. ctx.beginPath();
  566. ctx.arc(imageX + imageWidth / 2, imageY + imageHeight / 2, 1, 0, 360, false);
  567. ctx.arc(imageX + imageWidth / 2 - 4, imageY + imageHeight / 2, 1, 0, 360, false);
  568. ctx.arc(imageX + imageWidth / 2 + 4, imageY + imageHeight / 2, 1, 0, 360, false);
  569. ctx.fillStyle = "black";//填充颜色,默认是黑色
  570. ctx.fill();//画实心圆
  571. ctx.closePath();
  572. ctx.restore();
  573. w = w - imageWidth - imageMagin;
  574. //这里的左对齐的,当显示的字长度超过空白地方时,要改成右对齐
  575. if (style.hAlign == 0) {
  576. if (value) {
  577. let textWidth = ctx.measureText(value).width;
  578. let spaceWidth = w;
  579. if (spaceWidth < textWidth) {
  580. style.hAlign = 2;
  581. }
  582. }
  583. }
  584. }
  585. }
  586. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  587. };
  588. CusButtonCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  589. return {
  590. x: x,
  591. y: y,
  592. row: context.row,
  593. col: context.col,
  594. cellStyle: cellStyle,
  595. cellRect: cellRect,
  596. sheetArea: context.sheetArea
  597. };
  598. };
  599. CusButtonCellType.prototype.processMouseDown = function (hitinfo) {
  600. if (hitinfo.sheet.getActiveRowIndex() == hitinfo.row && hitinfo.sheet.getActiveColumnIndex() == hitinfo.col) {
  601. var offset = hitinfo.cellRect.x + hitinfo.cellRect.width - 6;
  602. var imageWidth = 25;
  603. if (hitinfo.x < offset && hitinfo.x > offset - imageWidth) {
  604. if (!readOnly) {
  605. if (callback) callback(hitinfo)
  606. }
  607. }
  608. }
  609. };
  610. return new CusButtonCellType();
  611. },
  612. setReplaceButton(row, col, sheet) {
  613. let replaceButton = function () {
  614. };
  615. replaceButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  616. replaceButton.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  617. GC.Spread.Sheets.CellTypes.Button.prototype.paint.apply(this, arguments);
  618. if (value) {
  619. ctx.save();
  620. ctx.fillStyle = "white";
  621. let fh = options.fontInfo && options.fontInfo.fontSize ? options.fontInfo.fontSize : h - 6;
  622. ctx.fillText(value, x + (w + ctx.measureText(value).width) / 2, y + (h + fh) / 2 - 2);
  623. ctx.restore();
  624. }
  625. };
  626. let cellType = new replaceButton();
  627. cellType.buttonBackColor("#07A0FF");
  628. sheet.setCellType(row, col, cellType, GC.Spread.Sheets.SheetArea.viewport);
  629. },
  630. setTipsCell(row, col, sheet, header) {
  631. let TipCellType = function () { };
  632. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  633. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  634. return {
  635. x: x,
  636. y: y,
  637. row: context.row,
  638. col: context.col,
  639. cellStyle: cellStyle,
  640. cellRect: cellRect,
  641. sheet: context.sheet,
  642. sheetArea: context.sheetArea
  643. };
  644. };
  645. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  646. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  647. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  648. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  649. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  650. zoom = hitinfo.sheet.zoom();
  651. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, { sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport });
  652. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  653. let setting = {};
  654. if (textLength <= cellWidth) {
  655. return;
  656. }
  657. if (sheet && sheet.getParent().qo) {
  658. setting.pos = SheetDataHelper.getObjPos(sheet.getParent().qo);
  659. }
  660. TREE_SHEET_HELPER.showTipsDiv(text, setting, hitinfo);
  661. };
  662. TipCellType.prototype.processMouseLeave = function (hitinfo) {
  663. TREE_SHEET_HELPER.tipDiv = 'hide';
  664. if (TREE_SHEET_HELPER._toolTipElement) {
  665. $(TREE_SHEET_HELPER._toolTipElement).hide();
  666. TREE_SHEET_HELPER._toolTipElement = null;
  667. };
  668. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  669. };
  670. sheet.setCellType(row, col, new TipCellType(), GC.Spread.Sheets.SheetArea.viewport);
  671. },
  672. chkIfEmpty: function (rObj, setting) {
  673. var rst = true;
  674. if (rObj) {
  675. for (var i = 0; i < setting.header.length; i++) {
  676. if (rObj[setting.header[i].dataCode]) {
  677. rst = false;
  678. break;
  679. }
  680. }
  681. }
  682. return rst;
  683. },
  684. //add by zhong 2017-10-10
  685. //动态下拉框,配合EnterCell, args.sheet.repaint();
  686. getDynamicCombo: function (forLocked) {
  687. let ComboCellForActiveCell = function () { };
  688. ComboCellForActiveCell.prototype = new GC.Spread.Sheets.CellTypes.ComboBox();
  689. ComboCellForActiveCell.prototype.paintValue = function (ctx, value, x, y, w, h, style, options) {
  690. let sheet = options.sheet;
  691. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  692. GC.Spread.Sheets.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  693. } else {
  694. GC.Spread.Sheets.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  695. }
  696. };
  697. ComboCellForActiveCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  698. let sheet = options.sheet;
  699. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  700. return GC.Spread.Sheets.CellTypes.ComboBox.prototype.getHitInfo.apply(this, arguments);
  701. } else {
  702. return {
  703. x: x,
  704. y: y,
  705. row: options.row,
  706. col: options.col,
  707. cellStyle: cellStyle,
  708. cellRect: cellRect,
  709. sheetArea: options.sheetArea
  710. };//GC.Spread.Sheets.CellTypes.Text.prototype.getHitInfo.apply(this, arguments);
  711. }
  712. };
  713. return new ComboCellForActiveCell();
  714. },
  715. getTipsCombo: function (forLocked, tips, setting, node) {
  716. let getTipsCombo = function () {
  717. this.clickCom = false;
  718. };
  719. getTipsCombo.prototype = sheetCommonObj.getDynamicCombo(forLocked);
  720. if (tips && tips != "") {
  721. getTipsCombo.prototype.processMouseEnter = function (hitinfo) {
  722. if (this.clickCom == true) { //点击了下拉框的三角形,则不用再显示悬浮框了
  723. this.clickCom = false;
  724. return;
  725. }
  726. let text = typeof tips == 'function' ? tips(node) : tips;
  727. TREE_SHEET_HELPER.delayShowTips(hitinfo, setting, text);
  728. };
  729. getTipsCombo.prototype.processMouseLeave = function (hitinfo) {
  730. TREE_SHEET_HELPER.hideTipsDiv();
  731. };
  732. getTipsCombo.prototype.processMouseDown = function (hitinfo) {
  733. if (hitinfo.isReservedLocation == true) {//这里是点击了下拉框的三角形才会有这个属性
  734. TREE_SHEET_HELPER.hideTipsDiv();
  735. this.clickCom = true;
  736. }
  737. GC.Spread.Sheets.CellTypes.ComboBox.prototype.processMouseDown.apply(this, arguments);
  738. };
  739. getTipsCombo.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  740. TREE_SHEET_HELPER.hideTipsDiv();
  741. GC.Spread.Sheets.CellTypes.ComboBox.prototype.updateEditor.apply(this, arguments);
  742. };
  743. }
  744. return new getTipsCombo();
  745. },
  746. getTreeNodeCellType: function (datas, row, parentMap,treeCol) {// 2018-09-26 不用spreadjs默认的树结构,自定义控件
  747. var ns = GC.Spread.Sheets;
  748. let rectW = 10;
  749. let rectH = 10;
  750. let margin = 3;
  751. function TreeNodeCellType() {
  752. this.collapsed = gljUtil.isDef(datas[row].collapsed) ? datas[row].collapsed : true; //默认是折叠的
  753. this.treeNodeType = true;
  754. this.rectInfo = {};
  755. }
  756. TreeNodeCellType.prototype = new ns.CellTypes.Text();
  757. TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  758. let offset = 0;
  759. let step = 7;
  760. let level = getTreeLevel(datas[row], datas);//从0开始,取当前节点是第几级的
  761. let tem = offset + margin + rectW / 2 + step;//两条线之间的间隔
  762. let t_offset = offset;
  763. let temParentID = datas[row].ParentID;
  764. if (this.treeNodeType == true) {
  765. for (let i = level; i > 0; i--) {//这里是画子节点前面的竖线,从第二级开始
  766. let temParent = getParent(temParentID, datas);
  767. if (temParent) {//父节点有下一个兄弟节点才需要画
  768. if (hasNextBrother(parentMap, temParent)) sheetCommonObj.drawLine(ctx, x + t_offset + tem * i, y, x + t_offset + tem * i, y + h);
  769. temParentID = temParent.ParentID;
  770. }
  771. offset += tem;
  772. }
  773. }
  774. offset += step; //这个没法移动,所以要两个判断
  775. if (this.treeNodeType == true) {
  776. if (hasChildern(datas[row].ID, datas)) {//如果是有子节点
  777. //第一条 或者没有父节点(如费率子表综合里程项)不用画方框头上那条竖线其它都要
  778. if (row != 0 && gljUtil.isDef(datas[row].ParentID)) sheetCommonObj.drawLine(ctx, x + offset + rectW / 2 + margin, y, x + offset + rectW / 2 + margin, y + Math.round(h / 2) - rectH / 2);
  779. //画方框下面的那条竖线,如果没有下一个兄弟节点,则不用画
  780. if (hasNextBrother(parentMap, datas[row])) sheetCommonObj.drawLine(ctx, x + offset + rectW / 2 + margin, y + Math.round(h / 2) + rectH / 2, x + offset + rectW / 2 + margin, y + h);
  781. sheetCommonObj.drowRect(ctx, x + offset, y, w, h, rectW, rectH, margin);
  782. sheetCommonObj.drowSymbol(ctx, x + offset, y, w, h, rectW, rectH, margin, this.collapsed);
  783. this.rectInfo = { x: x + offset + margin, rectW: rectW }//计录一下可点击位置
  784. } else {
  785. let hasNext = datas[row + 1] ? datas[row + 1].ParentID == datas[row].ParentID : false;
  786. sheetCommonObj.drowSubItem(ctx, x, y, w, h, offset, hasNext, margin + rectW / 2);
  787. }
  788. }
  789. offset += step;
  790. offset += rectW;
  791. x = x + offset;//设置偏移
  792. w = w - offset;
  793. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  794. };
  795. // override getHitInfo to allow cell type get mouse messages
  796. TreeNodeCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  797. return {
  798. x: x,
  799. y: y,
  800. row: context.row,
  801. col: context.col,
  802. cellStyle: cellStyle,
  803. cellRect: cellRect,
  804. sheetArea: context.sheetArea
  805. };
  806. }
  807. TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
  808. ////方框外1像素内都有效
  809. if (!_.isEmpty(this.rectInfo) && Math.floor(hitinfo.x) <= this.rectInfo.x + this.rectInfo.rectW + 2 && Math.floor(hitinfo.x) >= this.rectInfo.x - 2) {
  810. this.collapsed = !this.collapsed;
  811. datas[row].collapsed = this.collapsed;
  812. this.refreshChildrenVisible(hitinfo.sheet);
  813. hitinfo.sheet.invalidateLayout();
  814. hitinfo.sheet.repaint();
  815. }
  816. };
  817. TreeNodeCellType.prototype.refreshChildrenVisible = function (sheet) {
  818. sheet.suspendPaint();
  819. sheet.suspendEvent();
  820. refreshVisible(datas[row]);
  821. sheet.resumeEvent();
  822. sheet.resumePaint();
  823. function refreshVisible(item) {
  824. if (parentMap[item.ID]) {
  825. for (let sub of parentMap[item.ID]) {
  826. refreshVisible(sub)
  827. }
  828. }
  829. let visible = getVisible(item);
  830. let trow = datas.indexOf(item);
  831. sheet.getRange(trow, -1, 1, -1).visible(visible);
  832. }
  833. function getVisible(item) {
  834. if (item.ParentID) {
  835. let parent = getParent(item.ParentID, datas);
  836. if (!parent) return true;
  837. let p_row = datas.indexOf(parent);
  838. treeCol = treeCol?treeCol:0;
  839. let visible = !sheet.getCellType(p_row, treeCol).collapsed;
  840. if (visible == true) { //如果是显示的,则要再往父节点的父节点检查,只要有一个节点是隐藏的,则都是隐藏
  841. return getVisible(parent);
  842. } else {
  843. return visible
  844. }
  845. } else {//如果parentID 为空则是最根节点
  846. return true;
  847. }
  848. }
  849. };
  850. return new TreeNodeCellType()
  851. function getTreeLevel(item, data) {
  852. if (item.ParentID && item.ParentID!=-1) {
  853. let pitem = _.find(data, { 'ID': item.ParentID });
  854. return getTreeLevel(pitem, data) + 1;
  855. } else {
  856. return 0
  857. }
  858. }
  859. function hasChildern(ID, data) {//返回是否有子项
  860. let p = _.find(data, { 'ParentID': ID });
  861. if (p) return true;
  862. return false
  863. }
  864. function getParent(ParentID, data) {
  865. let p = _.find(data, { 'ID': ParentID });
  866. return p;
  867. }
  868. function hasNextBrother(parentMap, item) {
  869. let children = parentMap[item.ParentID];
  870. if (!gljUtil.isDef(children) || children.indexOf(item) == children.length - 1) return false;
  871. return true
  872. }
  873. },
  874. setDynamicCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  875. let me = this;
  876. sheet.suspendPaint();
  877. let combo = me.getDynamicCombo();
  878. for (let i = 0, len = rowCount; i < len; i++) {
  879. if (itemsHeight) {
  880. combo.itemHeight(itemsHeight);
  881. combo._maxDropDownItems = itemsHeight + 5;
  882. }
  883. if (itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  884. else if (itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  885. else combo.items(items);
  886. sheet.getCell(beginRow + i, col).cellType(combo);
  887. }
  888. sheet.resumePaint();
  889. },
  890. setStaticCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  891. sheet.suspendPaint();
  892. let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  893. for (let i = 0, len = rowCount; i < len; i++) {
  894. if (itemsHeight) combo.itemHeight(itemsHeight);
  895. if (itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  896. else if (itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  897. else combo.items(items);
  898. sheet.getCell(beginRow + i, col).cellType(combo);
  899. }
  900. sheet.resumePaint();
  901. },
  902. //设置系统粘贴板数据,需要用户触发事件,直接调用会失败
  903. copyTextToClipboard: function (text) {
  904. let textArea = document.createElement("textarea");
  905. textArea.style.position = 'fixed';
  906. textArea.style.top = 0;
  907. textArea.style.left = 0;
  908. textArea.style.width = '2em';
  909. textArea.style.height = '2em';
  910. textArea.style.padding = 0;
  911. textArea.style.border = 'none';
  912. textArea.style.outline = 'none';
  913. textArea.style.boxShadow = 'none';
  914. textArea.style.background = 'transparent';
  915. textArea.value = text;
  916. document.body.appendChild(textArea);
  917. textArea.select();
  918. try {
  919. let successful = document.execCommand('copy');
  920. let msg = successful ? 'successful' : 'unsuccessful';
  921. console.log('Copying text command was ' + msg);
  922. } catch (err) {
  923. console.log('Oops, unable to copy');
  924. }
  925. document.body.removeChild(textArea);
  926. },
  927. //获取选中区域的表格类型数据(可粘贴到excel)
  928. getTableData: function (sheet, colSettings = null) {
  929. let rst = '';
  930. let sel = sheet.getSelections()[0];
  931. let pasteText = [];
  932. for (let row = sel.row; row < sel.row + sel.rowCount; row++) {
  933. if (!sheet.getCell(row, -1).visible())
  934. continue;
  935. let rowText = [];
  936. for (let j = 0; j < sel.colCount; j++) {
  937. let col = sel.col + j;
  938. if (!sheet.getCell(-1, col).visible())
  939. continue;
  940. if (colSettings && (colSettings[col]['data']['field'] === 'itemCharacterText' || colSettings[col]['data']['field'] === 'jobContentText'))
  941. rowText.push(sheet.getText(row, col) ? `"${sheet.getText(row, col)}"` : '');
  942. else
  943. rowText.push(sheet.getText(row, col) ? sheet.getText(row, col) : '');
  944. }
  945. pasteText.push(rowText.join('\t'));
  946. }
  947. return pasteText.join('\n');
  948. },
  949. transferToTreeSetting: function (setting, treeSetting, treeCol) {
  950. for (let h of setting.header) {
  951. treeSetting.cols.push(getSettingCol(h))
  952. }
  953. for (let l of setting.view.lockColumns) {
  954. treeSetting.cols[l].readOnly = true;
  955. }
  956. return treeSetting;
  957. function getSettingCol(header) {
  958. let aMap = { left: 0, center: 1, right: 2 };
  959. let hAlign = header.hAlign ? aMap[header.hAlign] : 0;
  960. let col = {
  961. "width": header.headerWidth ? header.headerWidth : 100,
  962. "head": {
  963. "titleNames": Array.isArray(header.headerName) ? header.headerName : [header.headerName],
  964. "spanCols": header.spanCols ? header.spanCols : [1],
  965. "spanRows": header.spanRows ? header.spanRows : [1],
  966. "vAlign": [1],
  967. "hAlign": [1],
  968. "font": ["Arial"]
  969. },
  970. "data": {
  971. "field": header.dataCode,
  972. "vAlign": 1,
  973. "hAlign": hAlign,
  974. "font": "Arial"
  975. }
  976. };
  977. if (header.showHint == true) {
  978. col.showHint = true;
  979. }
  980. if (header.cellType) {
  981. col.data.cellType = getCellType(header);
  982. }
  983. if (header.decimalField) {//设置formatter
  984. let decimal = getDecimal(header.decimalField);
  985. col.formatter = getFormatter(decimal);
  986. }
  987. if (header.getText && treeCol) {
  988. col.data.getText = treeCol.getEvent(header.getText);
  989. }
  990. /*col.readOnly = function (node) {
  991. if(node.data.ParentID == -1 || node.data.id == 'GJ'){//三材类别项不能编辑)
  992. return true;
  993. }
  994. return false;
  995. };*/
  996. return col;
  997. }
  998. function getCellType(header) {
  999. return function () {
  1000. if (header.cellType === "checkBox") {
  1001. return new GC.Spread.Sheets.CellTypes.CheckBox();
  1002. }
  1003. if (header.cellType === "comboBox") {
  1004. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  1005. if (header.options) {
  1006. dynamicCombo.itemHeight(header.options.length).items(header.options);
  1007. if (header.editorValueType == true) {
  1008. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  1009. }
  1010. }
  1011. return dynamicCombo
  1012. }
  1013. }
  1014. }
  1015. },
  1016. //注册自定义回车键事件
  1017. bindEnterKey: function (workBook, operation) {
  1018. workBook.commandManager().register('myEnter', operation);
  1019. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.enter, false, false, false, false);
  1020. workBook.commandManager().setShortcutKey('myEnter', GC.Spread.Commands.Key.enter, false, false, false, false);
  1021. },
  1022. //解决esc后触发了编辑结束的保存事件,显示与实际数据不同问题
  1023. bindEscKey: function (workBook, sheets) {
  1024. function isDef(v) {
  1025. return typeof v !== 'undefined' && v !== null;
  1026. }
  1027. workBook.commandManager().register('myEsc', function () {
  1028. let activeSheet = workBook.getActiveSheet();
  1029. let hasTheSheet = false;
  1030. for (let sheetObj of sheets) {
  1031. let sheet = sheetObj.sheet;
  1032. if (sheet === activeSheet) {
  1033. hasTheSheet = true;
  1034. let editStarting = sheetObj.editStarting;
  1035. let editEnded = sheetObj.editEnded;
  1036. if (editStarting) {
  1037. sheet.unbind(GC.Spread.Sheets.Events.EditStarting);
  1038. }
  1039. if (editEnded) {
  1040. sheet.unbind(GC.Spread.Sheets.Events.EditEnded);
  1041. }
  1042. let row = sheet.getActiveRowIndex();
  1043. let col = sheet.getActiveColumnIndex();
  1044. let orgV = sheet.getValue(row, col);
  1045. let orgText = sheet.getText(row, col);
  1046. if (!isDef(orgV)) {
  1047. orgV = '';
  1048. }
  1049. if (sheet.isEditing()) {
  1050. sheet.endEdit();
  1051. sheet.setValue(row, col, orgV);
  1052. }
  1053. if (editStarting) {
  1054. sheet.bind(GC.Spread.Sheets.Events.EditStarting, editStarting);
  1055. }
  1056. if (editEnded) {
  1057. sheet.bind(GC.Spread.Sheets.Events.EditEnded, editEnded);
  1058. }
  1059. }
  1060. }
  1061. //容错处理,以防没把所有工作簿的表格信息传入参数
  1062. if (!hasTheSheet) {
  1063. if (activeSheet.isEditing()) {
  1064. activeSheet.endEdit();
  1065. }
  1066. }
  1067. });
  1068. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.esc, false, false, false, false);
  1069. workBook.commandManager().setShortcutKey('myEsc', GC.Spread.Commands.Key.esc, false, false, false, false);
  1070. },
  1071. //设置默认样式
  1072. spreadDefaultStyle: function (workBook) {
  1073. let defaultStyle = new GC.Spread.Sheets.Style();
  1074. defaultStyle.font = '14px Calibri';
  1075. let sheetCount = workBook.getSheetCount();
  1076. for (let i = 0; i < sheetCount; i++) {
  1077. let sheet = workBook.getSheet(i);
  1078. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
  1079. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.colHeader);
  1080. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.rowHeader);
  1081. }
  1082. },
  1083. //动态根据工作簿宽度和各列宽度比例设置宽度
  1084. setColumnWidthByRate: function (workBookWidth, workBook, headers) {
  1085. if (workBook) {
  1086. const sheet = workBook.getActiveSheet();
  1087. sheet.suspendEvent();
  1088. sheet.suspendPaint();
  1089. for (let col = 0; col < headers.length; col++) {
  1090. if (headers[col]['rateWidth'] !== undefined && headers[col]['rateWidth'] !== null && headers[col]['rateWidth'] !== '') {
  1091. sheet.setColumnWidth(col, workBookWidth * headers[col]['rateWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  1092. }
  1093. else {
  1094. if (headers[col]['headerWidth'] !== undefined && headers[col]['headerWidth'] !== null && headers[col]['headerWidth'] !== '') {
  1095. sheet.setColumnWidth(col, headers[col]['headerWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  1096. }
  1097. }
  1098. }
  1099. sheet.resumeEvent();
  1100. sheet.resumePaint();
  1101. }
  1102. },
  1103. drowRect: function (ctx, x, y, w, h, rectW, rectH, margin) {
  1104. ctx.save();
  1105. ctx.strokeStyle = "gray";
  1106. ctx.translate(0.5, 0.5);
  1107. ctx.beginPath();
  1108. let rectX = x + margin;
  1109. let rectY = y + Math.round(h / 2) - rectH / 2;
  1110. ctx.moveTo(rectX, rectY);
  1111. ctx.lineTo(rectX, rectY + rectH);
  1112. ctx.lineTo(rectX + rectW, rectY + rectH);
  1113. ctx.lineTo(rectX + rectW, rectY);
  1114. ctx.lineTo(rectX, rectY);
  1115. ctx.moveTo(rectX + rectW, y + Math.round(h / 2));
  1116. ctx.lineTo(rectX + rectW + 5, y + Math.round(h / 2));
  1117. ctx.stroke();
  1118. ctx.restore();
  1119. },
  1120. drawLine: function (ctx, x1, y1, x2, y2, color) {
  1121. let l_color = color ? color : "gray";
  1122. ctx.save();
  1123. ctx.translate(0.5, 0.5);
  1124. ctx.beginPath();
  1125. ctx.moveTo(x1, y1);
  1126. ctx.lineTo(x2, y2);
  1127. ctx.strokeStyle = l_color;
  1128. ctx.stroke();
  1129. ctx.restore();
  1130. },
  1131. drowSymbol: function (ctx, x, y, w, h, rectW, rectH, margin, collapsed) {
  1132. ctx.save();
  1133. ctx.strokeStyle = "#000000";
  1134. ctx.translate(0.5, 0.5);
  1135. ctx.beginPath();
  1136. ctx.moveTo(x + margin + 2, y + Math.round(h / 2));
  1137. ctx.lineTo(x + margin + 8, y + Math.round(h / 2));
  1138. let rectY = y + Math.round(h / 2) - rectH / 2;
  1139. if (collapsed) {
  1140. ctx.moveTo(x + margin + rectW / 2, rectY + 2);
  1141. ctx.lineTo(x + margin + rectW / 2, rectY + 2 + 6);
  1142. }
  1143. ctx.stroke();
  1144. ctx.restore();
  1145. },
  1146. drowTriangle: function (ctx, x, y) {//画向下三角形
  1147. ctx.save();
  1148. ctx.fillStyle = "black";
  1149. ctx.beginPath();
  1150. ctx.moveTo(x, y);
  1151. ctx.lineTo(x - 3, y - 6);
  1152. ctx.lineTo(x + 3, y - 6);
  1153. ctx.fill();
  1154. ctx.restore();
  1155. },
  1156. drowSubItem: function (ctx, x, y, w, h, offset, hasNext, step) {
  1157. let t_step = step ? step : 6;
  1158. offset += t_step;
  1159. ctx.save();
  1160. ctx.strokeStyle = "gray";
  1161. ctx.translate(0.5, 0.5);
  1162. ctx.beginPath();
  1163. ctx.moveTo(x + offset, y);
  1164. ctx.lineTo(x + offset, y + Math.round(h / 2));
  1165. offset += 9;
  1166. ctx.lineTo(x + offset, y + Math.round(h / 2));
  1167. if (hasNext) {
  1168. ctx.moveTo(x + offset - 9, y + Math.round(h / 2));
  1169. ctx.lineTo(x + offset - 9, y + h);
  1170. }
  1171. ctx.stroke();
  1172. ctx.restore();
  1173. return offset;
  1174. },
  1175. checkData: function (col, setting, value) {
  1176. let result = true;
  1177. let validator = setting.header[col].validator !== undefined ? setting.header[col].validator : null;
  1178. if (validator === null) {
  1179. return result;
  1180. }
  1181. switch (validator) {
  1182. case 'number':
  1183. let regular = /^\d+(\.\d+)?$/;
  1184. result = regular.test(value);
  1185. break;
  1186. case 'boolean':
  1187. let booleanValue = [true, false];
  1188. result = booleanValue.indexOf(value) >= 0;
  1189. break;
  1190. }
  1191. return result;
  1192. },
  1193. getSelectedRecode: function (sheet, datas) {
  1194. let sel = sheet.getSelections()[0];
  1195. let srow = sel.row == -1 || sel.row == "" ? 0 : sel.row;
  1196. if (gljUtil.isDef(srow) && datas.length > srow) {
  1197. return datas[srow];
  1198. }
  1199. return null;
  1200. },
  1201. // 延迟一段时间刷新表格,因为有的弹窗里面有表格,马上刷新可能会造成,弹窗界面还未完全显示完就完成了表格刷新,导致表格显示不完整
  1202. refreshWorkbookDelDefer(workbook, time) {
  1203. if (workbook) {
  1204. setTimeout(() => workbook.refresh(), time);
  1205. }
  1206. },
  1207. setRowsAutoFit(sheet, rows, col, wordWrap) {
  1208. rows.forEach(row => {
  1209. sheet.getCell(row, col).wordWrap(wordWrap);
  1210. sheet.autoFitRow(row);
  1211. });
  1212. }
  1213. }