sheet_common.js 67 KB

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