• Jian Ming Yu
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
I am trying to use the XLSX-JS (which is the thrid party library ) to load execl data into salesforce, I have uplaod the xlsx.js as static resource into salesforce platform and use loadScript to load the js code into webpage, but when I call XLSX.read method I got a XLSX is undefined message. Anyone can advise me how to referece XLSX 
import { LightningElement, track } from "lwc";
import { loadScript } from "lightning/platformResourceLoader";
import xlsx from "@salesforce/resourceUrl/xlsx";

xlsxInitialized = false;
  renderedCallback() {
    if (this.xlsxInitialized) {
      return;
    }
    this.xlsxInitialized = true;
    loadScript(this, xlsx + '/xlsx/xlsx.core.min.js')
            .then(() => {
                console.log('loaded');
            })
            .catch(error => {
                this.error = error;
            });
  }
  handleFileChange(e) {
    var files = e.target.files;
    var file = files[0];
    this.readWorkbookFromLocalFile(file, workbook => {
      console.log("4");
      var sheetNames = workbook.SheetNames; // 工作表名称集合
      var worksheet = workbook.Sheets[sheetNames[0]]; // 这里我们只读取第一张sheet
      console.log(worksheet);
    });
  }

  readWorkbookFromLocalFile(file, callback) {
    var reader = new FileReader();
    reader.onload = e => {
      var data = e.target.result;
      var workbook = XLSX.read(data, { type: "binary" });
      console.log("workbook :" + workbook);
      if (callback) callback(workbook);
    };
    console.log("2");
    reader.readAsBinaryString(file);
    console.log("3");
  }
Hi I am trying to use the XLSX JS libraries to parse a CSV convert to JSON and upload into salesforce object. I had done this in Visual force and wanted to do the same in LWC.
But I am stuck in calling the 3rdd party JS lib method.  My question is how do you call a method in the 3rd lib.
 
import 	shimlatest from '@salesforce/resourceUrl/shimlatest';
import 	dropsh from '@salesforce/resourceUrl/DropSheet';
import 	xlsxfull from '@salesforce/resourceUrl/xlsxfull';

Promise.all([
            loadScript(this, shimlatest),
            loadScript(this, xlsxfull),
        ])
            .then(()  => {
                X = XLSX; / THIS ONE ALWAYS COMES AS undefind 
                console.log('XLSX Value of ' + X);
                if(X !== undefined)
                   X = '';
                   this.initializePage(); 
                })
            .catch(error => {
                this.dispatchEvent(
                    new ShowToastEvent({
                        title: 'Error loading XLSX modules',
                        message: error.message,
                        variant: 'error',
                    }),
                );
            });