简体   繁体   中英

How can I append array of object in existing xlsx sheet in node.js

I have "ticketNameArr" that stores the array of objects. Everytime the promise is resolved I want to append that array of object to the existing sheet but it is getting overridden.

 Promise.all(ticketDetail).then(function (values) {
                //console.log(values);
                ticketNameArr.push({

                    TicketName: ticketNameIs,
                    InputLocationMessage: values[0].inputLocationMessage,
                    ResultLocationMessage: values[1].resultLocationMessage,
                    TicketStatusInput: values[0].ticketStatusInput,
                    TicketStatusResult: values[1].ticketStatusResult,
                    LogStatus: values[2].logStatus,
                    ReportStatus: values[3].reportStatus,
                    InputTime: values[0].birthtime,
                    InputModifiedTime: values[0].mtime,
                    OutputTime: values[1].birthtime,
                    OutputModifiedTime: values[1].mtime,
                    LogCreateTime: values[2].birthtime,
                    LogModifiedTime: values[2].mtime,
                    FtpTime:values[0].ftpTime,
                    FTPSTATUS:values[0].FTPStatus
                });

                var wb = XLSX.utils.book_new() // make Workbook of Excel


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


                var ws  = XLSX.utils.json_to_sheet(ticketNameArr)
                XLSX.utils.book_append_sheet(wb, ws) // sheetAName is name of Worksheet
                XLSX.writeFile(wb, 'DailyTicketSatus.xlsx')

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


            });```

Inside for loop you are initiating new workbook every time

so remove the following line

var wb = XLSX.utils.book_new() // make Workbook of Excel

and initialise above the for loop

var wb = XLSX.utils.book_new() // make Workbook of Excel
Promise.all(ticketDetail).then(function (values) {
    if (ticketNameArr.length == ticketName.length) {  
       var ws  = XLSX.utils.json_to_sheet(ticketNameArr)
       XLSX.utils.book_append_sheet(wb, ws) // sheetAName is name of Worksheet
       XLSX.writeFile(wb, 'DailyTicketSatus.xlsx')
       respond.render('result', { "ticketNameArr": ticketNameArr });
    }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM