Javascript 如何使用excel.js库编辑Multer发布的文件?
我正在处理一个项目,该项目评估电子表格,然后对其进行编辑,但我的编辑没有保存。我使用multer将CSV保存到“uploads/HVACresultfile.CSV”中,但我似乎无法用excel.js NPM库对其进行编辑和正确写入。我缺少什么Javascript 如何使用excel.js库编辑Multer发布的文件?,javascript,node.js,excel,express,multer,Javascript,Node.js,Excel,Express,Multer,我正在处理一个项目,该项目评估电子表格,然后对其进行编辑,但我的编辑没有保存。我使用multer将CSV保存到“uploads/HVACresultfile.CSV”中,但我似乎无法用excel.js NPM库对其进行编辑和正确写入。我缺少什么 var express = require('express'); var router = express.Router(); const multer = require('multer'); var Excel = require('exceljs
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函数中。我认为我应该将上传的文件作为我的read file函数的一个参数,并在函数外部定义它