Utilize Async Function in TypeScript to Import an Excel File

Trying to incorporate the xlsx angular library to bring in an excel file onto my page:

Following some instructions from the xlsx library documentation, I created this function:

public async HandleExcelFile(excelFile)
{
//Initialize empty Excel Array
let excelData : Array<any> = [];
let error : any = null;
return new Promise<Object>((resolve, reject) =>
  {
    var file = document.getElementById(excelFile)
    const inputFile: DataTransfer = <DataTransfer>(excelFile.target); 
    const fileReader: FileReader = new FileReader();
    fileReader.onload = (event: any) => 
    {
      const binaryString: string = event.target.result;
      const workBook: XLSX.WorkBook = XLSX.read(binaryString, { type: 'binary', sheetStubs: true}); 

      const workSheetName: string = workBook.SheetNames[0];
      const workSheet: XLSX.WorkSheet = workBook.Sheets[workSheetName];

      excelData = <aoa>(XLSX.utils.sheet_to_json(workSheet, 
      {header: 1, blankrows: true })); 
    };
    fileReader.readAsBinaryString(inputFile.files[0]); 

But when I try to upload the file using an html input, I encounter this error message:

Error: Uncaught (in promise): TypeError: undefined is not an object (evaluating 'excelFile.target')

Hunting down where the issue lies has been a bit tricky, any assistance or advice would be greatly welcomed.

Answer №1

Seems like a small oversight.

The excelFile argument you provided isn't an object. Were you intending to use the variable file that was declared here?

var file = document.getElementById(excelFile);

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

Unable to fetch an identification number using swfupload

I seem to be encountering an issue with retrieving an echoed AJAX response through SWFupload. After uploading a file, the account.php?action=uploadphotographs echoes the number 37, which should update the text field hidFileId with this data. I am uncertain ...

Having trouble retrieving values from Promise.allSettled on Node.js 12 using TypeScript version 3.8.3

Currently, I am delving into NodeJs 12 and exploring the Promise.allSettled() function along with its application. The code snippet that I have crafted allows me to display the status in the console, but there seems to be a hitch when attempting to print t ...

function for ajax response is received

How can I replace HTML code with the 'for from django' function using a jQuery AJAX call? $.ajax({ url: url, data: ...

function() is not a valid function call;

I'm trying to make my function update the data shown on a Vue chart js whenever I click a button, but I keep running into the error message saying that "_vm.chartData is not a function". I followed the guide on using computed properties, but I must be ...

Adding a new item to an array in KnockoutJS causes existing data to vanish

Check out the issue on jsfiddle: http://jsfiddle.net/6bFsY/3/ After clicking "Add Users" and then clicking it again, all data in the extensions drop-down field disappears. This occurred following the addition of an email column. The email field gets pre-f ...

Postman is displaying [object object] as the return value, but the actual value is not

Currently, I am working on automating tasks using Postman One interesting aspect is the presence of a vehicles array in the body { "Vehicles":[ { "car":"{{car}}", "bike":"{{bike}}&quo ...

Feeling overwhelmed by JavaScript jQuery errors when making an Ajax request?

I am facing an issue with two buttons on my webpage. One button triggers a table refresh using an ajax php request, while the other button is set to automatically refresh the table every 10 seconds. However, when attempting to access the document, I am enc ...

How can I incorporate an iframe into Angular while utilizing a value fetched from MongoDB as the src attribute?

When using an iframe similar to this: <iframe class="e2e-iframe-trusted-src" height="480" width="500" src={{trailer.address}}> </iframe> I encountered the following error: "ERROR Error: unsafe value used in ...

What is the best way to create a button that functions as a toggle and changes its styling when clicked?

I have implemented the following code to update prices in my pricing table: function updatePrices() { var x = document.getElementById("prijs-small"); if (x.innerHTML === "17,50") { x.innerHTML = "13,50"; } else { x.innerHTML = "17,50"; } ...

ng-repeat to display items based on dropdown choice or user's search input

Utilizing $http to retrieve JSON data for display in a table. I have successfully implemented a search functionality where users can search the JSON data from an input field. Additionally, I now want to include a feature that allows users to filter the JSO ...

Updating the function type definition in TypeScript after importing it into a separate file

I have created a unique hook named useDropdownSelection. It's a straightforward one. Take a look at the code below: import { useState } from 'react' export const useDropdownSelection = (initialValue: string) => { const [selectedOption, ...

Ways to conceal a text box generated through ng-repeat in angularjs

Within my code, I am utilizing an ng-repeat to generate three labels and textboxes dynamically. <ul class="modusScriptingHiddenList"> <li ng-repeat="fld in allFields"> <label class="control-label"> ...

Is there a way to cease the counting?

Currently immersing myself in the world of JavaScript and taking on a challenging exercise. The task at hand involves creating a simulation game, with an object containing three arrays (representing each individual game). Successfully managed to display t ...

I'm looking for help on creating a three-column table using javascript/jquery. The table should display Product, Price, and Discount (which is calculated at 20%). Can anyone

Check out this code snippet. var items = ["Laptop", "Tablet", "Smartphone", "Headphones", "Camera"]; var costs = [599.99, 299.99, 799.99, 149.99, 499.99]; displayItems = ""; totalCost = 0; for (var j = 0; j < items.length; j++) { displayItems += " ...

What is the best way to remove text messages from a channel that only allows images?

I have developed a Discord bot named YES with a specific text channel for images only. My goal is to program the bot to automatically delete any text messages in this channel and respond with "You cannot send text messages." However, I also want to create ...

Protecting String in PHP to Utilize in JavaScript

When I receive code through AJAX, I handle it as shown below: $verifiedSubject = addslashes(htmlentities($_REQUEST['subject'])); $verifiedBody = addslashes(htmlentities($_REQUEST['body'])); $verifiedAttachment1 = addslashes(htmlentitie ...

Enhance the impact of "dialogs" to the fullest extent or minimize it to achieve the

How can I position the minimized dialog on the bottom of the div when appending dialogs next to each other in a FB messaging system? To achieve a Facebook messaging effect, the titlebar must go down when minimizing/maximizing the dialog. Perform the follo ...

[Error]: Unable to access the 'getCroppedCanvas' property as it is undefined in React Cropper

I am currently utilizing the "React Cropper" library (https://www.npmjs.com/package/react-cropper). I have included this code snippet (similar to many examples): import React from 'react'; import Cropper from 'react-cropper'; export ...

Learn the process of adding a key and value into an array using Angular

I need to filter data based on the number of rows and columns provided by the user. After reading an excel file, I extract the data in the controller: https://i.sstatic.net/vPpxL.png These are the column headers retrieved after the user entered 5 as the ...

The issue of JQuery $(this) malfunctioning when used within a function parameter

Encountering an issue with the code snippet below: $(".countdown").circularCountdown({ startDate:$(this).attr('data-start'), endDate:$(this).attr('data-end'), timeZone:$(this).attr("timezone") }); On the contrary, the co ...