SheetJS used in Angular

Hi @twu,

Here is what I read and used, utilizing the example under “Browser download file (ajax)”:

Also note that you will need to have $http declared on your controller:

$scope.processFile = function(file){

      
   // need to retrieve an array buffer as per documentation 
   $http.get(file.path, {responseType: "arraybuffer"}).then(function(response){

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

          /* read into XLSX */
          var workbook = XLSX.read(bstr, {type:"binary"});
          console.info('workbook %o', workbook);
      });

};

and here are the details of how the above code came to be

Had checked what was being returned by the original code in there and found out that it was an array buffer. From there, had checked how we can grab that via Angular. Had then found that an additional parameter named {responseType: "arraybuffer"}

needs to be passed into the $http.get() as per code above. The rest of it is similar to the examples already in the SheetJS documentation / site.

Running the above code with the tm1-ui-upload should now give you:

Cheers!
Paul

1 Like