Reputation: 827
I trying to develop a Node.JS script that pulls data from a spreadsheet to use in the script.
My script starts like this:
var webdriverio = require('webdriverio');
var Excel = require('exceljs');
var testData = require('xlsx-extract');
//var Excel = require ('xlsx-style')
var options = {
desiredCapabilities: {
//browserName: 'phantomjs'
browserName: 'chrome'
}
};
var count = (1);
var testDate = new Date();
var Login = ('#button.mb24.secondary.wide')
var workbook = new Excel.Workbook();
workbook.xlsx.readFile('Myspreadsheet_v1.xlsx')
.then(function() {
getCell('A2').value
console.log('url is: ' + getCell('A2').value);
});
//var workbook = new Excel.Workbook();
//stream.pipe(workbook.xlsx.createInputStream('./Myspreadsheet_v1.xlsx'));
var worksheet = workbook.getWorksheet(1);
var sfurl = worksheet.getCell('A2').value;
var loginName = worksheet.getCell('B2').value;
var loginPassword = worksheet.getCell('C2').value;
However, I get an error when I try to run it:
xxxxxxx/xxxxxxx/xxxxx.js:32
var sfurl = worksheet.getCell('A2').value;
^
TypeError: Cannot read property 'getCell' of undefined
at Object.<anonymous> (xxxxxxx/xxxxxxx/xxxxx.js:32:22)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
I am really at a loss for what I did wrong and how to fix it. I have NPM installed and I installed node-xlxs and node-exceljs as well. I have NodeJS installed on my system.
I used these links to try to get started:
https://www.npmjs.com/package/xlsx
https://www.npmjs.com/package/exceljs
Upvotes: 0
Views: 920
Reputation: 382
The function readFile
is asynchronous it means that node will tell your system to load the file and keep executing the rest of the code in the meantime. When the system has read the file it will resolve the promise which will execute the code in your .then(...)
-function.
This means that when you get to getCell
the workbook is not even available yet. Move this part in to the .then(...)
-function and it should work.
Upvotes: 2