找回密码
 立即注册

QQ登录

只需一步,快速开始

硕㏒

金牌服务用户

29

主题

100

帖子

289

积分

金牌服务用户

积分
289
硕㏒
金牌服务用户   /  发表于:2024-6-12 17:11  /   查看:128  /  回复:4
1金币
  1.   async workbookInitialized(value) {
  2.       initializeFunctions(GC);
  3.       if (this.isDesigner) {
  4.         //designer设计器的方法
  5.         this.designer = value;
  6.         this.spread = this.designer.getWorkbook();
  7.         this.getTemplateParamList();
  8.         // this.changeOpenMenu();
  9.       } else {
  10.         // 将传入的 value(电子表格实例)分配给 this.spread
  11.         this.spread = value;
  12.         // this.changeOpenMenu();
  13.       }
  14.       // 设置 tabStripPosition 为 1
  15.       this.spread.options.tabStripPosition = 1;
  16.       // 刷新电子表格视图
  17.       this.spread.invalidateLayout();
  18.       this.spread.repaint();
  19.       // 暂停电子表格的绘制
  20.       this.spread.suspendPaint();
  21.       //禁用delete
  22.       let commandManager = this.spread.commandManager();
  23.       commandManager.register("clearcontent", {
  24.         canUndo: true,
  25.         execute: function () {
  26.           // Do nothing to prevent delete
  27.           return true;
  28.         },
  29.       });
  30.       commandManager.setShortcutKey(
  31.         "clearcontent",
  32.         GC.Spread.Commands.Key.del,
  33.         false,
  34.         false,
  35.         false,
  36.         false
  37.       );
  38.       //监听sheet页的切换
  39.       let now = this;
  40.       this.addSheetChangedListener(this.spread, function (newSheetName) {
  41.         console.log("切换到新的工作表:" + newSheetName);
  42.         now.activeSheetName = newSheetName;
  43.       });
  44.       try {
  45.         // 获取与 sampleTaskCode 相关的数据
  46.         const result = await getSampleTaskOriginalRecordsByTaskId(
  47.           this.$route.query.sampleTaskCode || this.sampleTaskCode
  48.         );

  49.         // 如果成功获取数据
  50.         if (result.data.success) {
  51.           // 如果 fileUrl 存在
  52.           if (result.data.result.fileUrl) {
  53.             // 更新组件数据
  54.             this.form = result.data.result;
  55.             this.flowStatus = result.data.result.flowStatus;
  56.             this.showPrint = result.data.result.pdfFileUrl ? true : false;
  57.             this.pdfFileUrl = result.data.result.pdfFileUrl;
  58.             this.pdfFileTitle = result.data.result.pdfFileTitle;
  59.             this.experienceItemId = result.data.result.experienceItemId;
  60.             this.printId = result.data.result.id;
  61.             this.experienceItemParamNames =
  62.               result.data.result.experienceItemParamNames;
  63.             if (result.data.result.immutableDataJson) {
  64.               this.dirtyDataObject = JSON.parse(
  65.                 result.data.result.immutableDataJson
  66.               );
  67.             }
  68.             this.reportConcentrate = JSON.parse(
  69.               result.data.result.variableDataJson
  70.             );

  71.             // 下载文件并处理数据
  72.             const source = await downLoadApi(result.data.result.fileUrl);

  73.             // 读取文件内容
  74.             const reader = new FileReader();
  75.             reader.readAsText(source.data, "utf-8");
  76.             reader.onload = (e) => {
  77.               // 解析文件内容为 JSON
  78.               let json = JSON.parse(e.currentTarget.result);
  79.               // 将 JSON 数据导入到电子表格中
  80.               this.spread.fromJSON(json);
  81.               // 获取电子表格中的工作表数量
  82.               let index = this.spread.getSheetCount();
  83.               // 遍历所有工作表
  84.               for (let i = 0; i <= index - 1; i++) {
  85.                 // 解析 extendDataJson
  86.                 let obj = JSON.parse(result.data.result.extendDataJson);
  87.                 let data = {};

  88.                 // 获取当前工作表的数据
  89.                 if (obj) {
  90.                   data = obj[this.spread.getSheet(i).name()]
  91.                     ? obj[this.spread.getSheet(i).name()]
  92.                     : {};
  93.                 }
  94.                 // 深拷贝数据
  95.                 data = JSON.parse(JSON.stringify(data));
  96.                 //对JSON中的数据尽量转换类型,防止excel无法计算
  97.                 // 从Object.prototype获取hasOwnProperty方法的引用,以安全地检查对象自有属性
  98.                 const hasOwn = Object.prototype.hasOwnProperty;

  99.                 // 遍历data对象的所有属性
  100.                 for (let key in data) {
  101.                   // 使用hasOwnProperty.call方法检查data对象是否真正拥有当前迭代的key属性,
  102.                   // 而不是从原型链上继承来的。这样做可以避免因对象被恶意修改原型链或属性被意外覆盖而导致的错误。
  103.                   if (hasOwn.call(data, key)) {
  104.                     // // 特殊处理"制样日期",将其转换为Date对象
  105.                     // if (key === "制样日期") {
  106.                     //   data[key] = new Date(data[key]);
  107.                     // }
  108.                     // // 特殊处理"委托日期",应用自定义的日期格式化函数
  109.                     // else if (key === "委托日期") {
  110.                     //   data[key] = this.formatDateToStr(data[key]);
  111.                     // }
  112.                     // // 对于其他所有属性,尝试将其转换为Number类型
  113.                     // else {
  114.                     data[key] = this.convertToNumberOrDate(data[key]);
  115.                     // }
  116.                   }
  117.                 }
  118.                 // if (data["公称直径"]) {
  119.                 //   data["公称直径"] = Number(data["公称直径"]);
  120.                 // }

  121.                 // if (data["制样日期"]) {
  122.                 //   data["制样日期"] = new Date(data["制样日期"]);
  123.                 //   // data["制样日期"] = "2022年7月8日";
  124.                 // }
  125.                 // if (data["委托日期"]) {
  126.                 //   data["委托日期"] = this.formatDateToStr(data["委托日期"]);
  127.                 // }
  128.                 // 创建数据源
  129.                 let dataSource =
  130.                   new GC.Spread.Sheets.Bindings.CellBindingSource(data);
  131.                 // 如果数据是数组类型
  132.                 if (Array.isArray(data)) {
  133.                   // 设置工作表的数据源
  134.                   this.deviceData = data;
  135.                   this.spread.getSheet(i).setDataSource(data);
  136.                   // 设置列宽
  137.                   for (let J = 0; J <= 16; J++) {
  138.                     this.spread.getSheet(i).setColumnWidth(J, 200);
  139.                   }
  140.                 } else {
  141.                   // 设置工作表的数据源
  142.                   console.log(dataSource, "======");
  143.                   this.spread.getSheet(i).setDataSource(dataSource);
  144.                 }
  145.                 // this.autoFitRowOnSetDataSource(this.spread.getSheet(i));
  146.                 // this.spread.getSheet(i).recalcAll();
  147.               }
  148.               // 重置设置 tabStripPosition 为 1
  149.               this.spread.options.tabStripPosition = 1;
  150.               // 刷新电子表格视图
  151.               this.spread.invalidateLayout();
  152.               this.processReports(this.spread);
  153.               // this.autoFitRowOnValueChanged(value);
  154.               this.SelectionChanged(this.spread);
  155.               this.CellChanged(this.spread);
  156.               //获取设备缓存列表
  157.               this.getDeviceCache();
  158.             };
  159.           } else {
  160.             this.$message.error("获取模板失败");
  161.           }
  162.         } else {
  163.           this.$message.error("获取详情失败");
  164.         }
  165.       } catch (error) {
  166.         // 输出错误信息并显示错误提示
  167.         console.error(error);
  168.         this.$message.error("发生错误");
  169.       }

  170.       // 恢复电子表格的绘制
  171.       this.spread.resumePaint();
  172.     },
复制代码
image.png795401611.png

现在遇到一个问题,前端spreadjs在页面初始化的时候,给页面setDataSource,页面直接崩溃了,这个是什么原因导致的


image.png373062438.png

4 个回复

倒序浏览
硕㏒
金牌服务用户   /  发表于:2024-6-12 17:23:11
沙发
sheet页数量少,datasource内容也少的情况下,不会崩溃
回复 使用道具 举报
Joestar.XuSpreadJS 开发认证
超级版主   /  发表于:2024-6-12 18:13:42
板凳
您好,从代码上不太能看出问题,需要您提供一下相关的模板文件和数据源JSON,这边复现后调研一下看看。
回复 使用道具 举报
硕㏒
金牌服务用户   /  发表于:2024-6-12 18:20:02
地板
附件中是文件和datasource

20240612143616892.ssjson

898.93 KB, 下载次数: 2

20240612143616892.txt

16.58 KB, 下载次数: 2

回复 使用道具 举报
Joestar.XuSpreadJS 开发认证
超级版主   /  发表于:2024-6-13 10:09:03
5#
您好,这边在17.0.10中测试了一下,没有复现出您这个问题,测试代码如下:

image.png896039123.png

建议您先升级到17.0.10测试看看,如果仍然存在此问题,请您提供一个可以复现此问题的Demo,这边帮您调研一下看看。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部