question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

workbook.addWorkSheet is not a function

See original GitHub issue

Below goes code I have written

'use strict';

const Hapi = require('hapi');
const Path = require('path');
var fs = require('fs');
var Excel = require('exceljs');

// create new server instance and connection information
const server = new Hapi.Server({  
    host: 'localhost',
    port: 5009
  })

//server.path(__dirname)

// Add the route
server.route({
    method: 'GET',
    path:'/path', 
    handler: function (request, h) {
        let file = 'test.xlsx';
        let filePath = Path.join(__dirname,file);
        return 'file path - '+filePath;
    }
});

server.route({
        method: 'GET',
        path: '/test.xlsx',
        handler: function (request, h) {
            let file = 'test.xlsx';
            let filePath = Path.join(__dirname,file)
            let contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            let data;
            console.log(filePath)
            fs.readFile(filePath, function(error, content) {
                data = content;
            });
            console.log(data);
            const res = h.response(data).type(contentType)
                .header('Content-Disposition', 'attachment; filename=' + file);
            
            console.log(res);
            return res;
        }
});
function createWorkBook(filePath) {
        console.log("Inside the function createWorkBook");
        var workbook = new Excel.Workbook();
        console.log(workbook);
        workbook.views = [
            {
              x: 0, y: 0, width: 10000, height: 20000,
              firstSheet: 0, activeTab: 1, visibility: 'visible'
            }
        ]
        var worksheet = workbook.addWorkSheet("Data",{properties:{tabColor:{argb:'FFC0000'}}});
       
        workbook.xlsx.writeFile(filePath)
                .then(function() {
                    console.log("File created successfully")
                });
}
        

// Start the server
async function start() {

    try {
        await server.start();
    }
    catch (err) {
        console.log(err);
        process.exit(1);
    }

    console.log('Server running at:', server.info.uri);
};

start();
createWorkBook(Path.join(__dirname,'data.xlsx'));

Below goes the error:

    var worksheet = workbook.addWorkSheet("Fund Data",{properties:{tabColor:{argb:'FFC0000'}}});
                             ^

TypeError: workbook.addWorkSheet is not a function at createWorkBook (C:\Work\FMS\src\core\server\hapiServer.js:82:34) at Object.<anonymous> (C:\Work\FMS\src\core\server\hapiServer.js:131:1) at Module._compile (module.js:635:30) at Object.Module._extensions…js (module.js:646:10) at Module.load (module.js:554:32) at tryModuleLoad (module.js:497:12) at Function.Module._load (module.js:489:3) at Function.Module.runMain (module.js:676:10) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3

Any help on this?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10

github_iconTop GitHub Comments

3reactions
helixquarcommented, Jun 23, 2020

Can you please note how you solved it?

1reaction
digable1commented, Aug 24, 2022

Update: While I haven’t tested it yet and am about to - I’m pretty confident I know what happened. In a nutshell: Operator error.

In a further summary: Lesson for today - when you are so used to Typescript catching the idiotic stuff instead of linting Javascript, quit being really get lazy and forgetting to check basic things. Such as:

  1. Calling spliceRow instead of spliceRows.
  2. Forgetting to put an await in calling an async function in one of the few lines of code that are different between constructing the first spreadsheet and the second one. So of course this fat finger was in the constructing the second one.

Bottom line: Never mind.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Undefined is not a function when creating a Workbook
When attempting to create an Excel spreadsheet in node.js, why does the following code not work?
Read more >
@mmcraven/exceljs - npm
Sometimes (really very often) workbook has worksheets with id not starting from 1. // For instance It happens when any worksheet has been ......
Read more >
How to download created excel file in node js using exceljs
I am using exceljs module for creating excel file. The problem is it is neither getting created nor getting saved in the path....
Read more >
Code does not work in function node but works fine when run ...
Hi friends, I am using ExcelJS to write array to excel. and it's working fine when i run it using node exceltest.js const...
Read more >
addWorksheet: Add a worksheet to a workbook in openxlsx
addWorksheet ( wb, sheetName, gridLines = openxlsx_getOp("gridLines", TRUE), ... sheet = 8, 1:400) ## Save workbook ## Not run: saveWorkbook(wb, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found