我正在开发一个评估电子表格的项目,然后对其进行编辑,但我的编辑没有保存。我使用multer将CSV保存为'uploads / HVACresultfile.csv',但我似乎无法编辑它并使用excel.js NPM库正确编写。我错过了什么?
var express = require('express');
var router = express.Router();
const multer = require('multer');
var Excel = require('exceljs');
const index = require("../routes/index")
console.log('hvac doctor loads')
function readFile () {
var workbook = new Excel.Workbook();
var resultFile = workbook.csv.readFile('uploads/HVACresultfile.csv')
var worksheet = workbook.getWorksheet('uploads/HVACresultfile.csv')
.then(function(resultFile) {
resultFile.modified = new Date();
worksheet.getCell('A9').border = {
top: {style:'double', color: {argb:'FF00FF00'}},
left: {style:'double', color: {argb:'FF00FF00'}},
bottom: {style:'double', color: {argb:'FF00FF00'}},
right: {style:'double', color: {argb:'FF00FF00'}}
};
console.log('reads')
writeFile()
})};
function writeFile (workbook) {
var workbook = createAndFillWorkbook('uploads/HVACresultfile.csv');
workbook.xlsx.writeFile('wutwut')
.then(function() {
console.log('written as xlsx')
});
}
readFile()
编辑:这是具体的错误。
TypeError: Cannot read property 'then' of undefined
at readFile (***/***/hvacdoctor/controllers/hvacdoctor.js:12:5)
具体错误是无法读取未定义的属性。但我没有定义一切吗?
在writeFile函数中,您已使用函数初始化工作簿,但未在示例中提供。请将您在readFile函数中创建的工作簿作为参数传递给writeFile。
function readFile () {
/*
workbook editing done here
*/
writeFile(workbook)
})};
function writeFile (workbook) {
workbook.xlsx.writeFile('wutwut')
.then(function() {
console.log('written as xlsx')
});
}
readFile()
* PS:然后在你的例子中的两个地方使用。请说明哪个功能发生错误。由于我没有足够的声誉来评论,而不是要求澄清评论,我发布这个答案假设错误是在writefile函数中。