0

I have uploaded ExcelJS.min.Js file in the static resource as XLSX name. I want to import and use its functions in LWC. Here's my code and I am getting 'undefined' error at this line - await loadScript(this, XLSX); Help please?

import { LightningElement, api } from 'lwc';
import { loadScript, loadStyle } from 'lightning/platformResourceLoader';
import XLSX from '@salesforce/resourceUrl/XLSX';

export default class ExcelFileTest extends LightningElement {

 async handleDownloadClick() {
   
   alert('handleclicked');

try{
    await loadScript(this, XLSX);
}
catch(error){
  alert('ERROR'+JSON.stringify(error));
}
    const workbook = new XLSX.workbook();
    alert('line 13 book craeted');
//    const worksheet = workbook.addworksheet('My worksheet');
    alert('line 13 sheet craeted');


const fileName = 'MyData.xlsx';
alert(filename);


  }

}

6
  • What is the version of exceljs you are using ?
    – Rohit
    Mar 5, 2023 at 6:55
  • consider accepting this as the answer if it helps you.
    – Rohit
    Mar 5, 2023 at 12:15
  • @Rohit Begging for votes and accepts like this is in poor taste and looked down upon.
    – Derek F
    Mar 5, 2023 at 16:06
  • @DerekF : ok , I will not ask again. I think begging is a wrong word which you are using. When we analyse and solve an issue , we also expect something out of it and when someone visits the question in the future , there's no way of knowing if the answer solves the issue ! I will not ask for it again as you said but I just have a different POV.
    – Rohit
    Mar 5, 2023 at 16:17
  • @DerekF : Given the reputation and expertise you have on SFSE , I feel that you should have abstained from using the word "begging", but again that's just my POV.
    – Rohit
    Mar 5, 2023 at 16:27

1 Answer 1

3

This error is seen since you are trying to load external JS in click handler and not in connectedCallback or renderedCallback. Also , the path specified for JS in loadScript needs to be changed.

I tried excelJS from here and it loaded fine for me. Below are the steps which I followed using execlJS 4.3.0:

1 Enable Lightning Web Security in your org. Follow step here

2 Use below code in your JS file. Here , we need to load JS in connectedCallback for using it.

import { LightningElement } from 'lwc';
import { loadScript } from 'lightning/platformResourceLoader';
import XLSX from '@salesforce/resourceUrl/XLSX';

export default class ExceljsDemo extends LightningElement {


  connectedCallback() {

    this.initExcelJs();
  }

  async initExcelJs() {
    try {
      // Make sure to add the correct path in loadScript
      await loadScript(this,XLSX+'/unpkg/exceljs.min.js');
    } catch(error){
      console.log("error ", error);
    }
  }

  handleClick() {
    try {
      console.log("clicked");
    const workbook = new XLSX.workbook();
    alert('line 13 sheet craeted');


  const fileName = 'MyData.xlsx';
  alert(filename);
    }catch(error) {
      console.log("erro in excel workbook ",error);
    }

  }
}

3 Html file :

<template>
    ExcelJS demo :
    <lightning-button label="Neutral" title="Non-primary action" onclick={handleClick}></lightning-button>
</template>

4 Static resource in org (uploaded zip): enter image description here

I did not really understand why to use async/await instead of promises but it will work in both ways.

Refer LWC documentation for more info : https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.js_third_party_library

There's some error specific to excelJS for which you can refer : https://stackoverflow.com/questions/55132760/creating-excel-file-and-writing-to-it-with-exceljs

5
  • Hello Rohit - I got an error again. I copied your code and uploaded the zip file you shared (via URL) but it's still failing. :( Mar 6, 2023 at 15:50
  • It's working in my org. There's some error in excelJS code which you are using in LWC as stated in the last part of the answer.
    – Rohit
    Mar 6, 2023 at 15:55
  • Ok, let me check Mar 7, 2023 at 6:48
  • @PraveenBehera : There seems to be a known issue with excelJS - github.com/exceljs/exceljs/issues/960 I would recommend trying it in VF page first.
    – Rohit
    Mar 17, 2023 at 12:04
  • Sure, let me try. will get back. Mar 18, 2023 at 17:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .