sheet_common.js 63 KB

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