sheet_common.js 71 KB

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