Extract information from Excel files using the js-xlsx library

I am currently utilizing the js-xlsx library. If necessary, you can access my excel sheet here. The code I have implemented is as follows:

/* setting up XMLHttpRequest */
var url = "Test.xlsx";
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";

oReq.onload = function(e) {
var arraybuffer = oReq.response;

/* converting data to binary string */
var data = new Uint8Array(arraybuffer);
var arr = new Array();
for(var i = 0; i != data.length; ++i) arr[i] =String.fromCharCode(data[i]);
var bstr = arr.join("");

/* Calling XLSX */
var workbook = XLSX.read(bstr, {type:"binary"});

/* PROCESS workbook DATA HERE */
var first_sheet_name = workbook.SheetNames[0];
/* Accessing worksheet */
var worksheet = workbook.Sheets[first_sheet_name];

console.log(XLSX.utils.sheet_to_json(worksheet,{raw:true}));
}
oReq.send();

In the console log, I receive the following output:

(3) […]​0: Object { FirstName: "Mayuresh", MiddleName: "Dinkar ", LastName: "Joshi", … }​1: Object { FirstName: "Arun", MiddleName: "Vikas", LastName: "Pathak", … }​2: Object { FirstName: "Narendra", MiddleName: "Damodardas", LastName: "Modi", … }​length: 3​<prototype>: Array []

I am struggling to access this data. I am unsure of how to refer to it by name. I attempted using `arr.length`, but it only returned 1 instead of the expected 3. The JavaScript script does return a length of 3, although I am uncertain where this value is derived from. I simply need assistance in accessing that array. Thank you.

Answer №1

It appears to be functioning properly. Follow these steps to test it out...

const sheet = workbook.Sheets[selected_sheet_name];
const data = XLSX.utils.sheet_to_json(sheet,{raw:true});
const fullNames = data.map(entry => `${entry.FirstName} ${entry.LastName}`);
console.log(fullNames);

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

What are the steps to adjust the width of a website from the standard size to a widescreen

Is it possible to create a "floating" screen adjustment on websites? I know you can set the standard size of pixels, but how do sites adjust for different screen sizes like wider laptop screens? Does it automatically detect the reader's screen size an ...

Utilize Python to extract values from nested JSON data retrieved via ActiveCollab's REST API

Received this data from ActiveCollab via REST API: { "time_records": [ { "id": 122, "class": "TimeRecord", "url_path": "\/projects\/89\/time-r ...

Enable browser caching for form data when using AJAX to submit

I'm currently working on a web application that relies heavily on AJAX to submit form data. However, I've encountered an issue where I want the browser to cache the user's input for auto-completion in future form fillings. While I know I cou ...

Tips for effectively utilizing "Session" in jQuery

Are there any comparable features in jQuery to Session["Param"] in C#? How can I implement it? I've looked up "sessionStorage" in jQuery, but I'm having trouble grasping it. ...

jQuery datatable loses highlight after page refresh

I am currently working with a datatable that utilizes ajax to load data. var table = $('#example').DataTable({ "ajax": "xxxxxx.aspx", stateSave: true, "aLengthMenu": [ [10, 25, 50, 100], [10, 25, 50, 100] ], ...

Can Definitions be used as nested sub-definitions in JSON-Schema (Draft 0.7)?

I've been familiar with using JSON for quite some time now, but I am new to JSON-Schema. I'm currently exploring the possibility of having nested definitions. What I mean: I would like to utilize "$ref":"#definitions/link/post" and "$ref":"#defi ...

Create a new object by extracting JSON data using JavaScript

I am attempting to extract various data elements from a JSON file and place them into an object. My ultimate goal is to then convert this object back to JSON format, containing only the desired data. I believe that structuring the object like this might w ...

Should custom objects in MongoDB arrays be embedded or referenced for storage?

My products collection contains product objects structured like this: { "name" : "price" : "category" : "quality" : } In addition, I have a user object with an empty array for products: { "products" : [] } My question is how should I store prod ...

Create a file by generating JSON data in an ASP.NET MVC post controller and then return the file

I am currently working on an ajax post request: function downloadElevationMap() { var jsonData = ko.toJSON(mod.SelectedItem); $.ajax({ url: '/Home/GetData/', type: 'POST', contentType: "application/jso ...

A script in PHP or JavaScript that dynamically generates two dual drop-down menus to assist with data selection

I have experience with php scripting, but I am facing challenges when trying to combine it with JavaScript. The issue arises when I have a form that includes dropdown menus for categories and subcategories. When a category is selected, the options in the s ...

What is the most efficient way to include multiple JavaScript functions without requiring document.ready() for each individual function?

I currently have functional code that looks like this: $(document).ready(function() { $("#num1").click(function() { $("li.elementsA").addClass("alerty"); return false }); }); $(document).ready(function() { $("#num2").click(function() { $(" ...

What is the process for generating SDF-Icons (Mapbox's specialized icons) from PNG files?

I am currently working on changing the icon color of an icon image in Mapbox. According to Mapbox documentation, the only way to do this is by using sdf-icons (https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#paint-symbol-icon-color). After hours o ...

Updating a component when a prop changes

Embarking on my journey into the world of React, I find myself in need of assistance. The issue at hand involves re-rendering data within my Table component. Whenever a new API call is made to create an entry in my database using Prisma, the newly added d ...

Sending a JSON array from an Ajax request to a Spring controller: A step-by-step guide

My HTML table data is converted to JSON format and sent via AJAX to a Spring controller. In the controller, I used @RequestParam Map<String, String> to retrieve the values, but the entire JSON string was received as the only key. I cannot use a model ...

"Easy step-by-step guide on rearranging field display order in radDataForm with a JSON data

How can I rearrange the order of fields displayed when using JSON as a source in Vue.js / NativeScript (radDataForm)? Currently, my code is functioning correctly, but the displayed order is: Album Name Band Name Owned Year Borrowed This order does not m ...

A guide to determining the dimensions (width, height, length) of a mesh using THREE.js

I've been scouring different sources in hopes of finding a way to obtain the width and height of a mesh, but I haven't had any luck. I have imported a collada model into my project, and all I need is to determine its dimensions in Webgl/Three.js ...

Determining in Express.js whether headers have been sent or not

As I develop a library that involves setting headers, I aim to provide a personalized error message in case the headers have already been sent. Rather than simply letting it fail with the default "Can't set headers after they are sent" message from No ...

Manually validate inputs in Angular

I've encountered an issue with the bootstrap datepicker. When I click on the date icon, the date and time automatically populate in the input field. However, the input remains invalid because I haven't directly interacted with it. Has anyone else ...

What is the best way to retrieve content from a different website using javascript in an asp.net environment?

Currently working on a web application in asp.net where I want to provide users with a JavaScript code that allows them to fetch content from my website and display it on their own website. To handle user requests on my website, I have implemented a gener ...

The overall outcome determined by the score in JavaScript

Currently, I am working with a dataset where each person is matched with specific shopping items they have purchased. For instance, Joe bought Apples and Grapes. To gather this information, individuals need to indicate whether they have made a purchase. I ...