简体   繁体   中英

Exceljs append row

I wrote this code so I can save some info in excel file but it runs multiple time for every user and for some reason it doesn't append a new row but it removes old one and writes a new one. Anyone can help?

        // A new Excel Work Book
        var workbook = new Excel.Workbook();

        // Create a sheet
        var sheet = workbook.addWorksheet("Sheet1");


        // Add rows in the above header
        sheet.addRow({
          user_id: info.id,
          user_tag: username,
          followed_by: inform.username,
          followers: info.edge_followed_by.count,
          followings: info.edge_follow.count,
          biography: info.biography,
          category: info.business_category_name,
          external_url: info.external_url,
          external_lynx_url: info.external_url_linkshimmed,
          is_verified: info.is_verified,
        });

        // Save Excel on Hard Disk
        workbook.xlsx.writeFile("business_accounts.xlsx").then(function () {
          // Success Message
          alert("User Saved");
        });
// A new Excel Work Book
        var workbook = new Excel.Workbook();

        // Create a sheet
        var sheet = workbook.addWorksheet("Sheet1");

        for (user of users) {
        // Add rows in the above header
        sheet.addRow({
              user_id: info.id,
              user_tag: username,
              followed_by: inform.username,
              followers: info.edge_followed_by.count,
              followings: info.edge_follow.count,
              biography: info.biography,
              category: info.business_category_name,
              external_url: info.external_url,
              external_lynx_url: info.external_url_linkshimmed,
              is_verified: info.is_verified,
            });
        }

        // Save Excel on Hard Disk
        workbook.xlsx.writeFile("business_accounts.xlsx").then(function () {
          // Success Message
          alert("User Saved");
        });

you only need one excel file for all the users so only the addrow function need to be repeated. make sure you create and write only once.

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