sheet_common.js 61 KB

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