我无法从xlsx文件读取多行

问题描述 投票:0回答:1

我正在尝试从xlsx文件读取多行。我的xlsx工作表包含各种详细信息,其中一个是FTP目录,密码详细信息。每行包含一个不同的FTP目录。如果工作表中仅提及一行,则我能够从FTP读取,获取和检索数据但是,一旦我添加了要读取的额外行,它就会开始显示promise错误,下面是我的代码dataJson1是excel工作表(JSON)

for (let i = 0; i < dataJson1.length; i++) {

        dir = dataJson1[i]['FTP DIR'];
        subfolder = dataJson1[i]['Sub Folder'];
        ftpPath = dir + subfolder;
        host = dataJson1[i]['FTP IP'];
        user = dataJson1[i]['FTP Username'];
        password = dataJson1[i]['FTP Password'];
        ticketStatus = dataJson1[i]['Status']
        console.log("FTP LOCATION:" + ftpPath + "\n" + "HOSTNAME:" + host + "\n" + "USERNAME:" + user + "\n" + "PASSWORD:" + password + "\n")
        //Reading the list of directories present in the FTP location
        console.log("value of i" + i);
        ///////////
        if (ticketStatus == true) {
            if (!ftp) {
                ftp = new PromiseFtp();
            }
            const ftpPromise = ftp.connect({
                host: host, user: user,
                password: password
            }).then(function (serverMessage) {
                console.log('Server message: ' + serverMessage)
                //console.log("value of i" + i)

                return ftp.list(ftpPath);

            }).then(function (list) {
                console.log('Directory listing:');

                var dirList = (list)
                console.log(dirList)

                console.log("Number of directories:" + dirList.length)


                var jsondirString = JSON.stringify(dirList)
                var datadirJson1 = JSON.parse(jsondirString)
                for (var j = 0; j < dirList.length; j++) {

                    //console.log(datadirJson1[j]['name'])
                    ticketName.push(datadirJson1[j]['name'])
                    //console.log(ftpTime)
                    ftpTime.push(datadirJson1[j]['date'])
                }
                return ftp.end();
            });
            promises.push(ftpPromise)
        }//status check ends

    }//Loop ends here


  Promise.all(promises).then(arrayOfResults => {

        console.log(ticketName);
        var ticketNameArr = [];


        for (let i = 0; i < ticketName.length; i++) {
            let ticketNameIs = ticketName[i];
            let ftpTimeIs = ftpTime[i]
            let ticketDetail = ticketApp(ticketNameIs, ftpTimeIs);
            Promise.all(ticketDetail).then(function (values) {
                //console.log(values);
                ticketNameArr.push({
                    // FtpTime: values[0].ftpTime,
                    Date: values[0].ftpTime,
                    TicketName: ticketNameIs,
                    //Add Dynamic folder column----
                    In_Input_Location: values[0].ticketStatusInput,
                    Input_Time: values[0].mtime,
                    In_Result_Location: values[1].ticketStatusResult,
                    Result_Time: values[1].mtime,
                    //Will help in preparing comments
                    CallAPi: values[3].apiStatus,
                    ReportStatus: values[3].reportStatus,
                    Comment: values[4].Comment,
                    Status: values[5].ticketStatus
                    // LogStatus: values[2].logStatus,
                    // LogCreateTime: values[2].birthtime,
                    // LogModifiedTime: values[2].mtime,


                });


                if (ticketNameArr.length == ticketName.length) {


                    //uncomment below command if sheet is blank then comment back
                    // ws = XLSX.utils.sheet_add_json(ws, ticketNameArr,{origin:0, skipHeader:false});
                    //comment below command if sheet is blank then uncomment

                    ws = XLSX.utils.sheet_add_json(ws, ticketNameArr, { origin: -1, skipHeader: true });

                    //                  
                    let wsRemDup = removeDuplicate(ws)

                    console.log("Unique Data", wsRemDup)
                    //OverWriting Unique data

                    wb.Sheets[first_sheet_name] = XLSX.utils.json_to_sheet(wsRemDup);
                    XLSX.writeFile(wb, 'DailyTicketSatus.xlsx')





                    // respond.render('result', { "ticketNameArr": ticketNameArr });
                    respond.render('index', { "ticketNameArr": ticketNameArr });




                }


            });

        }


    })



})```

node.js promise ftp xlsx
1个回答
0
投票

我建议对此任务使用async / await语法,它更易于阅读,并且可以确保在继续下一个ftp主机之前ftp.end()调用已完成。这可能是原始代码无法处理多行的原因。

async function runFTPJob(dataJson1) {

    let promises = [];
    for (let i = 0; i < dataJson1.length; i++) {

        dir = dataJson1[i]['FTP DIR'];
        subfolder = dataJson1[i]['Sub Folder'];
        ftpPath = dir + subfolder;
        host = dataJson1[i]['FTP IP'];
        user = dataJson1[i]['FTP Username'];
        password = dataJson1[i]['FTP Password'];
        ticketStatus = dataJson1[i]['Status']
        console.log("Getting row:", i);
        console.log("FTP LOCATION:" + ftpPath + "\n" + "HOSTNAME:" + host + "\n" + "USERNAME:" + user + "\n" + "PASSWORD:" + password + "\n")
        //Reading the list of directories present in the FTP location

        if (ticketStatus == true) {
            if (!ftp) {
                ftp = new PromiseFtp();
            }

            try { 
                let ftpPromise = ftp.connect({ host, user, password });
                promises.push(ftpPromise);
                let serverMessage = await ftpPromise;
                console.log('Server message: ' + serverMessage)
                let dirList = await ftp.list(ftpPath);
                console.log('Directory listing:', dirList);
                console.log("Number of directories:" + dirList.length)
                for (let dirEntry of dirList) {
                    ticketName.push(dirEntry.name);
                    ftpTime.push(dirEntry.date);
                }
                await ftp.end();
            } catch (e) {
                console.error("An error occurred accessing ftp site:", e.message);
            }
        }//status check ends
    }//Loop ends here``

    return promises;
}

( async() => {
    let promiseList = await runFTPJob(dataJson1);

})();
© www.soinside.com 2019 - 2024. All rights reserved.