sheet_common.js 61 KB

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