请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册

QQ登录

只需一步,快速开始

无法

初级会员

25

主题

62

帖子

201

积分

初级会员

积分
201
无法
初级会员   /  发表于:2019-5-27 14:00  /   查看:3577  /  回复:5


我结合这两个 demo 尝试下载,但是下下来的文件是空的

https://demo.grapecity.com.cn/SpreadJS/TutorialSample/#/demos/customCellType
https://demo.grapecity.com.cn/SpreadJS/TutorialSample/#/demos/ExcelIO


可以把下面的代码复制到 https://demo.grapecity.com.cn/SpreadJS/TutorialSample/#/demos/ExcelIO 的 JavaScript 窗口,运行后下载试一下,可以发现下载的文件是空的。
试了把 https://demo.grapecity.com.cn/SpreadJS/TutorialSample/#/demos/customHeaderCellType 这个 demo 的代码放到下载案例的窗口,下载后是有数据的,但是自定义的内容都没有了。像这个圆就没有了。这些内容是不能下载的吗?
  1. var spreadNS = GC.Spread.Sheets;

  2. window.onload = function () {
  3.     var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
  4.     initSpread(spread);
  5.     var excelIo = new GC.Spread.Excel.IO();
  6.     var sheet = spread.getActiveSheet();
  7.     document.getElementById('loadExcel').onclick = function () {
  8.         var excelFile = document.getElementById("fileDemo").files[0];
  9.         var password = document.getElementById('password').value;
  10.         // here is excel IO API
  11.         excelIo.open(excelFile, function (json) {
  12.             var workbookObj = json;
  13.             spread.fromJSON(workbookObj);
  14.         }, function (e) {
  15.             // process error
  16.             alert(e.errorMessage);
  17.             if (e.errorCode === 2/*noPassword*/ || e.errorCode === 3 /*invalidPassword*/) {
  18.                 document.getElementById('password').onselect = null;
  19.             }
  20.         }, {password: password});
  21.     };
  22.     document.getElementById('saveExcel').onclick = function () {

  23.         var fileName = document.getElementById('exportFileName').value;
  24.         var password = document.getElementById('password').value;
  25.         if (fileName.substr(-5, 5) !== '.xlsx') {
  26.             fileName += '.xlsx';
  27.         }

  28.         var json = spread.toJSON();

  29.         // here is excel IO API
  30.         excelIo.save(json, function (blob) {
  31.             saveAs(blob, fileName);
  32.         }, function (e) {
  33.             // process error
  34.             console.log(e);
  35.         }, {password: password});

  36.     };
  37. };

  38. //Custom Cell Type
  39. function FivePointedStarCellType() {
  40.     this.size = 10;
  41. }
  42. FivePointedStarCellType.prototype = new spreadNS.CellTypes.Base();
  43. FivePointedStarCellType.prototype.paint = function (ctx, value, x, y, w, h, style, context) {
  44.     if (!ctx) {
  45.         return;
  46.     }

  47.     ctx.save();

  48.     // draw inside the cell's boundary
  49.     ctx.rect(x, y, w, h);
  50.     ctx.clip();
  51.     ctx.beginPath();

  52.     if (value) {
  53.         ctx.fillStyle = "orange";
  54.     } else {
  55.         ctx.fillStyle = "gray";
  56.     }

  57.     var size = this.size;
  58.     var dx = x + w / 2;
  59.     var dy = y + h / 2;
  60.     ctx.beginPath();
  61.     var dig = Math.PI / 5 * 4;
  62.     ctx.moveTo(dx + Math.sin(0 * dig) * size, dy + Math.cos(0 * dig) * size);
  63.     for (var i = 1; i < 5; i++) {
  64.         ctx.lineTo(dx + Math.sin(i * dig) * size, dy + Math.cos(i * dig) * size);
  65.     }
  66.     ctx.closePath();
  67.     ctx.fill();

  68.     ctx.restore();
  69. };
  70. FivePointedStarCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  71.     var xm = cellRect.x + cellRect.width / 2,
  72.             ym = cellRect.y + cellRect.height / 2,
  73.             size = 10;
  74.     var info = { x: x, y: y, row: context.row, col: context.col, cellRect: cellRect, sheetArea: context.sheetArea };
  75.     if (xm - size <= x && x <= xm + size && ym - size <= y && y <= ym + size) {
  76.         info.isReservedLocation = true;
  77.     }
  78.     return info;
  79. };
  80. FivePointedStarCellType.prototype.processMouseUp = function (hitInfo) {
  81.     var sheet = hitInfo.sheet;
  82.     if (sheet && hitInfo.isReservedLocation) {
  83.         var row = hitInfo.row, col = hitInfo.col, sheetArea = hitInfo.sheetArea;
  84.         var newValue = !sheet.getValue(row, col, sheetArea);
  85.         var spread = sheet.getParent();
  86.         spread.commandManager().execute({cmd: "editCell", sheetName: sheet.name(), row: row, col: col, newValue: newValue});
  87.         return true;
  88.     }
  89.     return false;
  90. };

  91. function FullNameCellType() {
  92. }
  93. FullNameCellType.prototype = new spreadNS.CellTypes.Base();
  94. FullNameCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  95.     if (value) {
  96.         spreadNS.CellTypes.Base.prototype.paint.apply(this, [ctx, value.firstName + "." + value.lastName, x, y, w, h, style, options]);
  97.     }
  98. };
  99. FullNameCellType.prototype.updateEditor = function(editorContext, cellStyle, cellRect) {
  100.     if (editorContext) {
  101.         editorContext.style.width=cellRect.width;
  102.         editorContext.style.height=100;
  103.         return {height: 100};
  104.     }
  105. };
  106. FullNameCellType.prototype.createEditorElement = function () {
  107.     var div = document.createElement("div");
  108.     div.setAttribute("gcUIElement", "gcEditingInput");
  109.     div.style.backgroundColor= "white";
  110.     div.style.overflow= "hidden";
  111.     var span1 = document.createElement('span');
  112.     span1.style.display = "block";
  113.     var span2 = document.createElement("span");
  114.     span2.style.display = "block";
  115.     var input1 = document.createElement("input");
  116.     var input2 = document.createElement("input");
  117.     var type = document.createAttribute('type');
  118.     type.nodeValue="text";
  119.     var clonedType = type.cloneNode(true);
  120.     input1.setAttributeNode(type);
  121.     input2.setAttributeNode(clonedType);
  122.     div.appendChild(span1);
  123.     div.appendChild(input1);
  124.     div.appendChild(span2);
  125.     div.appendChild(input2);
  126.     return div;
  127. };
  128. FullNameCellType.prototype.getEditorValue = function (editorContext) {
  129.     if (editorContext && editorContext.children.length === 4) {
  130.         var input1 = editorContext.children[1];
  131.         var firstName = input1.value;
  132.         var input2 = editorContext.children[3];
  133.         var lastName = input2.value;
  134.         return { firstName: firstName, lastName: lastName };
  135.     }
  136. };
  137. FullNameCellType.prototype.setEditorValue = function (editorContext, value) {
  138.     if (editorContext && editorContext.children.length === 4) {
  139.         var span1 = editorContext.children[0];
  140.         span1.innerHTML="First Name:";
  141.         var span2 = editorContext.children[2];
  142.         span2.innerHTML="Last Name:";
  143.         if (value) {
  144.             var input1 = editorContext.children[1];
  145.             input1.value=value.firstName;
  146.             var input2 = editorContext.children[3];
  147.             input2.value=value.lastName;
  148.         }
  149.     }
  150. };
  151. FullNameCellType.prototype.isReservedKey = function (e) {
  152.     //cell type handle tab key by itself
  153.     return (e.keyCode === GC.Spread.Commands.Key.tab && !e.ctrlKey && !e.shiftKey && !e.altKey);
  154. };
  155. FullNameCellType.prototype.isEditingValueChanged = function(oldValue, newValue) {
  156.     if (newValue.firstName != oldValue.firstName || newValue.lastName != oldValue.lastName) {
  157.         return true;
  158.     }
  159.     return false;
  160. };
  161. function initSpread(spread) {
  162.     var sheet = spread.getSheet(0);
  163.     sheet.suspendPaint();
  164.     sheet.setColumnWidth(0, 100);
  165.     sheet.setColumnWidth(1, 170);

  166.     var columnInfo = [
  167.         { name: "result", displayName: "Result", cellType: new FivePointedStarCellType(), size: 50 },
  168.         { name: "person", displayName: "Person", cellType: new FullNameCellType(), size: 170 }
  169.     ];

  170.     var source = [
  171.         { result: true, person: {firstName:"LeBron",lastName:"James"}},
  172.         { result: false, person: { firstName: "Chris", lastName: "Bosh" } },
  173.         { result: true, person: { firstName: "Dwyane", lastName: "Wade" } },
  174.         { result: false, person: { firstName: "Mike", lastName: "Miller" } },
  175.         { result: true, person: { firstName: "Mike", lastName: "Miller" } },
  176.         { result: true, person: { firstName: "Udonis", lastName: "Haslem" } },
  177.         { result: true, person: { firstName: "Mario", lastName: "Chalmers" } },
  178.         { result: true, person: { firstName: "Joel", lastName: "Anthony" } },
  179.         { result: false, person: { firstName: "Shane", lastName: "Battier" } },
  180.         { result: false, person: { firstName: "Ray", lastName: "Allen" } },
  181.         { result: true, person: { firstName: "James", lastName: "Jones" } },
  182.         { result: false, person: { firstName: "Rashard", lastName: "Lewis" } },
  183.         { result: true, person: { firstName: "Norris", lastName: "Cole" } },
  184.         { result: true, person: { firstName: "Chris", lastName: "Andersen" } },
  185.         { result: false, person: { firstName: "Jarvis", lastName: "Varnado" } },
  186.         { result: true, person: { firstName: "Juwan", lastName: "Howard" } },
  187.     ];
  188.     sheet.setDataSource(source);
  189.     sheet.bindColumns(columnInfo);
  190.     sheet.resumePaint();

  191. };
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

5 个回复

倒序浏览
KevinChen讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2019-5-28 09:09:42
沙发
您好,自定义单元格类型,以及其它自定义项,比如自定义公式、格式等,

都可以导出,但我需要给您解释一下。

首先,导出自定义元素请参考此示例:

https://demo.grapecity.com.cn/Sp ... customItemSerialize

自定义元素由于包含自定义代码逻辑,

要让自定义元素生效,导入时页面上必须有自定义元素的定义代码。
回复 使用道具 举报
无法
初级会员   /  发表于:2019-5-29 17:36:43
板凳
KevinChen 发表于 2019-5-28 09:09
您好,自定义单元格类型,以及其它自定义项,比如自定义公式、格式等,

都可以导出,但我需要给您解释一 ...
要让自定义元素生效,导入时页面上必须有自定义元素的定义代码


所以本地 excel 打开的话,就是没法显示自定义元素的吧?
回复 使用道具 举报
KevinChen讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2019-5-29 18:05:42
地板
对,自定义元素之所以是自定义的,就是本身SpreadJS和Excel都不支持,Excel是一个封闭的应用环境,当然也无法直接生成自定义逻辑。
回复 使用道具 举报
无法
初级会员   /  发表于:2019-5-30 10:46:07
5#
KevinChen 发表于 2019-5-29 18:05
对,自定义元素之所以是自定义的,就是本身SpreadJS和Excel都不支持,Excel是一个封闭的应用环境,当然也无 ...

明白了,谢谢。
回复 使用道具 举报
KevinChen讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2019-5-30 14:17:19
6#
不客气,本帖结贴了,有新的问题欢迎发新贴交流
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部