我这边试了下,2940次
这段代码是我初始化的操作
- async workbookInitialized(value) {
- if (this.isDesigner) {
- initializeFunctions(GC, value, 1);
- //designer设计器的方法
- this.designer = value;
- this.spread = this.designer.getWorkbook();
- this.getTemplateParamList();
- // this.changeOpenMenu();
- } else {
- initializeFunctions(GC, value, 2);
- // 将传入的 value(电子表格实例)分配给 this.spread
- this.spread = value;
- // this.changeOpenMenu();
- }
- // 设置 tabStripPosition 为 1
- this.spread.options.tabStripPosition = 1;
- // 刷新电子表格视图
- this.spread.invalidateLayout();
- this.spread.repaint();
- // 暂停电子表格的绘制
- this.spread.suspendPaint();
- //禁用delete
- // let commandManager = this.spread.commandManager();
- // commandManager.register("clearcontent", {
- // canUndo: true,
- // execute: function () {
- // // Do nothing to prevent delete
- // return true;
- // },
- // });
- // commandManager.setShortcutKey(
- // "clearcontent",
- // GC.Spread.Commands.Key.del,
- // false,
- // false,
- // false,
- // false
- // );
- //监听sheet页的切换
- let now = this;
- this.addSheetChangedListener(this.spread, function (newSheetName) {
- console.log("切换到新的工作表:" + newSheetName);
- now.activeSheetName = newSheetName;
- });
- try {
- // 获取与 sampleTaskCode 相关的数据
- // const result = await getSampleTaskOriginalRecordsByTaskId(
- // this.$route.query.sampleTaskCode || this.sampleTaskCode
- // );
- // const result = await this.getSampleTaskOriginalRecords();
- // 如果成功获取数据
- // console.log("🚀 ~ workbookInitialized ~ this.form:", this.form);
- if (JSON.stringify(this.form) != "{}") {
- // if (this.templateMode == 3) {
- // this.markFile(result.data.result);
- // }
- // 如果 fileUrl 存在
- if (this.form.fileUrl) {
- // 更新组件数据
- // this.form = result.data.result;
- // this.flowStatus = this.form.flowStatus;
- this.showPrint = this.form.pdfFileUrl ? true : false;
- this.pdfFileUrl = this.form.pdfFileUrl;
- this.pdfFileTitle = this.form.pdfFileTitle;
- this.experienceItemId = this.form.experienceItemId;
- this.printId = this.form.id;
- this.experienceItemParamNames = this.form.experienceItemParamNames;
- if (this.form.immutableDataJson) {
- this.dirtyDataObject = JSON.parse(this.form.immutableDataJson);
- }
- this.reportConcentrate = JSON.parse(this.form.variableDataJson);
- // 下载文件并处理数据
- const source = await downLoadApi(this.form.fileUrl);
- // 读取文件内容
- const reader = new FileReader();
- reader.readAsText(source.data, "utf-8");
- reader.onload = (e) => {
- // 解析文件内容为 JSON
- let json = JSON.parse(e.currentTarget.result);
- // 将 JSON 数据导入到电子表格中
- this.spread.fromJSON(json);
- // 获取电子表格中的工作表数量
- let index = this.spread.getSheetCount();
- // 遍历所有工作表
- for (let i = 0; i <= index - 1; i++) {
- // 解析 extendDataJson
- let obj = JSON.parse(this.form.extendDataJson);
- let data = {};
- // 获取当前工作表的数据
- if (obj) {
- data = obj[this.spread.getSheet(i).name()]
- ? obj[this.spread.getSheet(i).name()]
- : {};
- }
- // 深拷贝数据
- data = JSON.parse(JSON.stringify(data));
- //对JSON中的数据尽量转换类型,防止excel无法计算
- // 从Object.prototype获取hasOwnProperty方法的引用,以安全地检查对象自有属性
- const hasOwn = Object.prototype.hasOwnProperty;
- // 遍历data对象的所有属性
- for (let key in data) {
- if (hasOwn.call(data, key)) {
- const value = data[key];
- // 如果值是数组
- if (Array.isArray(value)) {
- // 遍历数组中的每一项
- for (let i = 0; i < value.length; i++) {
- const item = value[i];
- // 如果数组中的项是对象
- if (typeof item === "object" && item !== null) {
- // 遍历对象的所有属性
- for (let prop in item) {
- if (hasOwn.call(item, prop)) {
- item[prop] = this.convertToNumberOrDate(
- item[prop]
- );
- }
- }
- }
- }
- } else {
- // 如果值不是数组,则直接转换
- data[key] = this.convertToNumberOrDate(value);
- }
- }
- }
- // 创建数据源
- let dataSource =
- new GC.Spread.Sheets.Bindings.CellBindingSource(data);
- // 如果数据是数组类型
- if (Array.isArray(data)) {
- // 设置工作表的数据源
- this.deviceData = data;
- this.spread.getSheet(i).setDataSource(data);
- // 设置列宽
- for (let J = 0; J <= 16; J++) {
- this.spread.getSheet(i).setColumnWidth(J, 200);
- }
- } else {
- // 设置工作表的数据源
- this.initDataSource(
- this.spread.getSheet(i),
- dataSource,
- data
- );
- }
- // this.autoFitRowOnSetDataSource(this.spread.getSheet(i));
- // this.spread.getSheet(i).recalcAll();
- }
- // 重置设置 tabStripPosition 为 1
- this.spread.options.tabStripPosition = 1;
- //禁止复制本地excel时粘贴的时候连带格式粘贴,保证只粘贴值
- this.spread.options.allowCopyPasteExcelStyle = false;
- // 刷新电子表格视图
- this.spread.invalidateLayout();
- this.processReports(this.spread);
- // 检查testStatus是否为'已分配'
- if (
- this.$route.query.testStatus &&
- this.$route.query.testStatus === "已分配"
- ) {
- this.processingMaterialInfo(this.spread);
- }
- // this.autoFitRowOnValueChanged(value);
- this.SelectionChanged(this.spread);
- this.CellChanged(this.spread);
- //获取设备缓存列表
- this.getDeviceCache();
- this.DragFill(this.spread);
- this.spread.options.highlightInvalidData = false;
- // 恢复电子表格的绘制
- this.spread.resumePaint();
- };
- } else {
- this.$message.error("获取模板失败");
- }
- }
- // else {
- // this.$message.error("获取详情失败");
- // }
- } catch (error) {
- // 输出错误信息并显示错误提示
- console.error(error);
- this.$message.error("发生错误");
- }
- },
复制代码
|