What is the best way to access an excel file using JavaScript and Protractor?

Is it possible to read an Excel file dynamically in JavaScript and iterate through cell values using row and column counts, without converting it into a JSON object? I have searched through several posts on Stack Overflow but have not found a solution yet. Any suggestions would be greatly appreciated.

Thank you.

Answer №1

If you're looking for a perfect npm package that fits your needs, I recommend checking out the xlsx package. I have personally used it and can provide code examples if needed.

For instance, here's a simple example of how to parse an xlsx file: This code reads the value of cell 'B1' from the first sheet of the file test.xlsx

'use strict';
var XLSX = require('xlsx');
var workbook = XLSX.readFile('test.xlsx');
var sheetName = workbook.SheetNames[0];
var worksheet = workbook.Sheets[sheetName];
var redValue = worksheet['B1'];
console.log(redValue); 

Answer №2

After some research, I discovered a solution for parsing Excel files using the 'kexcel' npm package. This package has been helpful in my tasks so far. However, I have encountered an issue - I need to find out how to retrieve the last row number from an Excel file using this package.

Answer №3

If you want to access data from an excel file, there are two methods to do so.

The first method involves using the xlsx package. To begin, navigate to your project folder and run the command 'npm i xlsx' in the terminal to install the necessary packages.

var XLSX = require('xlsx');
var UserCred = XLSX.readFile('ExcelFile/UserLogin.xlsx');
var UserLoginWorksheet= UserCred.Sheets['Sheet1'];
var BaseUrl=UserLoginWorksheet['B1'].v;
console.log(BaseUrl);

In this code snippet, 'BaseUrl' stores the value located at column B1 in the excel file.

The second method utilizes the exceljs package. Similarly, go to your project folder and run 'npm i excel' in the terminal to install the required packages.

var Excel = require("exceljs");
 var filePath = 'ExcelFiles/StaffDetails.xlsx';
 var workbook = new Excel.Workbook();
 await workbook.xlsx.readFile(filePath).then(function () {
 worksheet = workbook.getWorksheet("Sheet1"); //Use sheetName in getWorksheet function
 let cellValue1 = worksheet.getRow(1).getCell(1).value;
 console.log(cellValue1+"4");
 })
let  cellValue2 = worksheet.getRow(2).getCell(2).value;

Here, cellvalue1 represents the value of cell A1, while cellvalue2 represents the value of cell B2 in the excel file.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The canvas's document.getElementById function is unable to find any matching element,

Hello everyone, I am currently diving into the world of javascript and trying to expand my knowledge. However, I have encountered a problem that has me stumped, and I could really use some assistance. While exploring, I came across this page: http://www.w ...

Tips for filtering data using an array within an object containing arrays

Below is the provided information: userRoom = ['rm1']; data = [{ name: 'building 1', building: [{ room: 'rm1', name: 'Room 1' },{ room: 'rm2', name: ' ...

What steps can I take to ensure that this input is neat and tidy

I need to implement conditional styling for my input field. The current layout is chaotic and I want to improve it. Specifically, when the active item is "Height", I only want to display the height value and be able to change it using setHeight. const [a ...

The functionality of window.location.href seems to be malfunctioning on mobile devices within an ionic application

Attempting to open a view with a function call from the UI side <ion-option-button class="button-light icon ion-chatbubble" ng-click="openView('username')"></ion-option-button> The controller code for this action is as follows: $sc ...

Place the retrieved data from the API directly into the editor

I've integrated the LineControl Editor into my app and everything is functioning perfectly, except for when I attempt to insert text into the editor. Here's the link to the LineControl GitHub page: https://github.com/suyati/line-control/wiki Fo ...

Detecting mistakes using ES6 assurances and BookshelfJS

I'm working on implementing a simple login method for a Bookshelf User model in an ExpressJS application. However, I am facing issues with handling errors from the rejected promises returned by the login function in the User model. While referring to ...

"Click events on jQuery's cloned elements are not retained when the elements are removed and appended to a container for the second time

Take a look at this fiddle: https://jsfiddle.net/L6poures/ item.click(function() { alert("It's functioning") }) $("#container").append(item) var stored = item.clone(true, true) function add_remove() { $("#container").html("") $("#conta ...

In JavaScript, there is a missing piece of logic when iterating through an array to find

I am working on a solution to populate empty values when data is not available for specific months. You can view my progress on Plunker here: http://plnkr.co/edit/f0IklkUfX8tkRZrn2enx?p=preview $scope.year = [ {"month":"mar", "val":"23"}, {"month":"feb", ...

An Overview of Implementing Ajax Calls for Partial Views on Form Submission

Check out my jQuery code below: <script type="text/javascript"> $("#submitfileform").submit(function () { $.ajax({ type: 'POST', contentType: 'application/html;charset=utf-8', dataT ...

Is it possible to create a bot that's capable of "hosting events" using Discord.js?

I am searching for a solution to host "events" using Discord.js. After some research, I stumbled upon this. Although it seems to be exactly what I am looking for, the website does not provide any code examples to help me try and replicate its functionali ...

Exploring the significance of a super in Object-Oriented Programming within JavaScript

During my studies of OOP in JS, I encountered the super() method which serves to call the constructor of the parent class. It made me ponder - why is it necessary to invoke the parent class constructor? What significance does it hold for us? ...

What steps can I take to ensure that my React child components will render successfully, even if a prop is absent

TLDR; Seeking solution to render child components in React even if a property of this.props is missing. My React app utilizes Yahoo's Fluxible and fetches data from a Wordpress site using WP REST API. Sometimes, the API may return incomplete data cau ...

Issue: The value of an object is not defined (evaluating '$scope.inputcode.password')

HTML Form: <form ng-submit="mylogin()"> <div class="list"> <label class="item item-input"> <span class="input-label">Username</span> <input type="text" ng-model="inputcode.username"> ...

Utilize the JavaScript Email Error Box on different components

On my website, I have implemented a login system using LocalStorage and would like to incorporate an error message feature for incorrect entries. Since I already have assistance for handling email errors on another page, I am interested in applying that sa ...

Execute a Node.JS query using an HTML select dropdown

My goal is to customize queries to a mySQL database based on the user's selection from select options on my website. Here is the HTML code: <select id = "year"> <option value = "yr" selected>Choose a Year</option> <option id = " ...

Showing data retrieved from nested JSON arrays in React Native

Recently, I delved into the world of React Native and encountered a challenge in displaying nested elements from fetched JSON data. Despite utilizing react hooks in the fetch API, I couldn't quite crack the code. export default function MedProfilScre ...

What could be the reason for v-model not functioning properly?

Embarking on my Vue.js coding journey, I am currently following a straightforward online tutorial. Utilizing the vue-cli, I kickstarted my application and crafted a basic component named Test.vue. Within this component lies a simplistic input control conne ...

Encountered an issue while trying to access the 'value' property from an undefined field in the available options

When attempting to showcase the value of the select field, I encountered this error message: Cannot read properties of undefined (reading 'value') https://i.stack.imgur.com/Q0d2k.png You can find the codesandbox link here: https://codesandbox.i ...

An effective method for adding a character to a number within an input field with the use of AngularJS

There is an input field in my code of type text that is connected to a variable within a certain scope. <input type=text= ng-model="vm.someProperty"> I am looking to have the input display the value with a percentage sign after it, without changing ...

Modifying a single route among several nested routes with specific names

My template includes various named, nested views: Template 1: <body> <div ui-view></div> </body> Template 2: <header></header> <div ui-view="left"></div> <div ui-view="canva ...