sheet_common.js 73 KB

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