如何编辑 Multer 使用 excel.js 库发布的文件?

How can I edit a file posted by Multer with the excel.js library?

我正在开展一个评估电子表格然后对其进行编辑的项目,但我的编辑没有保存。我使用 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)

具体的错误是cannot read 属性 then of undefined。但我不是已经定义了一切吗?

在 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:then 在您的示例中有两个地方使用。请明确是在哪个函数上发生了错误。由于我没有足够的声誉来评论,而不是要求评论的清晰度,我发布这个答案时假设错误是在 writefile 函数中。