本帖最后由 Wilson.Zhang 于 2025-4-2 09:18 编辑
问题描述:我原本的表样中存在数据源单元格的绑定,而导入进行的excel没有对应的数据单元格绑定,我目前只能通过如下代码循环行列的方式进行单元格赋值,有没有更好的赋值方式 mockGet() {
return new Promise((resolve) => {
// 模拟延迟用于模拟接口
setTimeout(() => {
resolve({ data: 'mocked data' }) // 返回模拟数据
}, 50)
})
}, //导入的方法
cancel(excelFile ) {
let excelIo = new ExcelIO.IO()
var _this = this
excelIo.open(excelFile, function(json) {_this.ImportInit(json)
}, function(e) {alert(e.errorMessage)
})
}
},//导入数据需要使用的方法
async ImportInit(json) {
// 开始加载动画
this.rowLoading = true
// 获取spread的sheet页面,做数据的处理
var worksheet = this.spread.getActiveSheet()
//原表样的行列数
var rowCount = worksheet.getRowCount()
var columnCount = worksheet.getColumnCount()
// 使用模拟的接口调用 防止下面的单元格赋值出现卡死情况
await this.mockGet()
//导入表样进行赋值后获取行列
const spread = new GC.Spread.Sheets.Workbook();
spread.fromJSON(json);
var sheet = spread.getActiveSheet()
//原表样的行列数与导入的表样行列数一样进入
if(rowCount===sheet.getRowCount()&&columnCount=== sheet.getColumnCount()){
for (let row = 0; row < rowCount; row++) {
for (let col = 0; col < columnCount; col++) {
const value = sheet.getValue(row,col)
//判断是否是数值
if (this.isValidNumber(value)) {
worksheet.setValue(row, col, value)
}
}
}
}else{
return this.$message({ message: '模板不对应', type: 'warning',
duration: 1500
})
}
this.rowLoading = false // 关闭加载动画
//重新计算页面中的所有公式
worksheet.recalcAll(true)
}
|