添加自定义公式和代码注释
This commit is contained in:
parent
13fc866e8e
commit
c137797f5c
@ -61,24 +61,44 @@ export default {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
// 从MinIO加载Excel文件
|
||||||
loadExcel() {
|
loadExcel() {
|
||||||
const minioUrl = 'http://47.95.198.7:30000/tmp/123.xlsx';
|
const minioUrl = 'http://47.95.198.7:30000/tmp/123.xlsx';
|
||||||
|
|
||||||
axios.get(minioUrl, { responseType: 'arraybuffer' })
|
axios.get(minioUrl, { responseType: 'arraybuffer' })
|
||||||
.then(response => {
|
.then(response => {
|
||||||
|
// 将Excel文件转换为数组缓冲区
|
||||||
const data = new Uint8Array(response.data);
|
const data = new Uint8Array(response.data);
|
||||||
|
// 将数组缓冲区转换为工作簿对象
|
||||||
const workbook = XLSX.read(data, { type: 'array' });
|
const workbook = XLSX.read(data, { type: 'array' });
|
||||||
this.spreadsheet.loadData(stox(workbook));
|
// 将工作簿对象转换为Spreadsheet数据格式
|
||||||
|
const spreadsheetData = stox(workbook);
|
||||||
|
// 添加自定义公式,123.xlsx表格中B列已有的公式为A+C+D。下面在F列添加A+D的计算公式.
|
||||||
|
const firstSheetRows = spreadsheetData[0].rows;
|
||||||
|
const keys = Object.keys(firstSheetRows);
|
||||||
|
for (let i = 1; i < keys.length - 1; i++) {
|
||||||
|
console.log(i, keys[i]);
|
||||||
|
// 在F列添加A+D的计算公式,F列的序号为5.
|
||||||
|
firstSheetRows[keys[i]].cells['5'] = {
|
||||||
|
text: `=SUM(A${i + 1}:D${i + 1})`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 将数据加载到Spreadsheet
|
||||||
|
this.spreadsheet.loadData(spreadsheetData);
|
||||||
})
|
})
|
||||||
.catch(error => console.error('加载文件失败:', error));
|
.catch(error => console.error('加载文件失败:', error));
|
||||||
},
|
},
|
||||||
saveExcel() {
|
saveExcel() {
|
||||||
|
// 获取Spreadsheet的数据
|
||||||
const data = this.spreadsheet.getData();
|
const data = this.spreadsheet.getData();
|
||||||
|
// 将数据转换为XLSX格式
|
||||||
const workbook = xtos(data);
|
const workbook = xtos(data);
|
||||||
|
|
||||||
|
// 将XLSX数据保存为文件
|
||||||
const excelBuffer = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
|
const excelBuffer = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
|
||||||
const blob = new Blob([excelBuffer], { type: 'application/octet-stream' });
|
const blob = new Blob([excelBuffer], { type: 'application/octet-stream' });
|
||||||
|
|
||||||
|
// 将blob对象上传至minio
|
||||||
const minioUploadUrl = 'http://47.95.198.7:30000/tmp/123.xlsx';
|
const minioUploadUrl = 'http://47.95.198.7:30000/tmp/123.xlsx';
|
||||||
axios.put(minioUploadUrl, blob, {
|
axios.put(minioUploadUrl, blob, {
|
||||||
headers: {
|
headers: {
|
||||||
|
Loading…
Reference in New Issue
Block a user