When using file.load for an xlsx file, what is the...
# suitescript
j
When using file.load for an xlsx file, what is the encoding type that comes out? I looks like base64 but decoding throws even more cryptic text. I'm trying to get it into some format I can parse.
b
expect to put a lot more effort into this
xlsx is a binary format
which suitescript uses base64 to represent
j
So when I do file.load().getContents() it's a base64 representation of binary? How can I convert it to true binary?
b
there is no bit or byte in javascript
j
Hmmm, maybe I'll just need to go back to drawing board. Options to parse were pdf, html, or excel. I suppose I'll need to work through the html nodes instead.
b
2.1 will get you type arrays, but suitescript does not have any functions with built in support for types array
your best chance is to use SheetJS or exceljs
j
I was using xlsx.js and jszip.js modules which seemed to work very well in the brower. But new FileReader() method was throwing error, so was trying with N/file module to load, but getting undefined errors.
b
there is no FileReader in node, thats a browser thing
j
Yeah, was trying to get the same result as FileReader with N/file module. Doesn't seem to be able to.
s
xlsx is just a collection of xml files in zip format, so you may be able to unzip the file in SuiteScript and access whatever content you need. As a local file, just change the file extension from xlsx to zip and you can see the contents.
j
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType suitelet
 * @NAmdConfig /SuiteScripts/configuration.json
 * @Author Jack Arens | National Food Group
 */
define(['N/record', 'N/search', 'N/file', 'N/encode', 'jszip', 'xlsx'],
    function (record, search, file, encode, jszip, xlsx) {

        function onRequest(options) {
            var response = options.response;

            var f = file.load({
                id: 26779396
            });
            //Start code from <https://stackoverflow.com/questions/8238407/how-to-parse-excel-xls-file-in-javascript-html5>
            var ExcelToJSON = function () {

                this.parseExcel = function (file) {
                    //var reader = new FileReader();

                    var data = file;
                    var workbook = XLSX.read(data, {
                        type: 'file'
                    });
                    workbook.SheetNames.forEach(function (sheetName) {
                        // Here is your object
                        var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
                        var json_object = JSON.stringify(XL_row_object);
                        log.debug('JSON OBJ', JSON.parse(json_object));
                    });

                    reader.onerror = function (ex) {
                        log.debug('Error', ex);
                    };

                    reader.readAsBinaryString(file);
                };
            };

            var xl2json = new ExcelToJSON();
            xl2json.parseExcel(f);
            
            //End code from <https://stackoverflow.com/questions/8238407/how-to-parse-excel-xls-file-in-javascript-html5>

            response.write({
                output: JSON.stringify('Success')
            });
        }

        return {
            onRequest: onRequest
        };
    });
My non-working code. I don't think it's gonna happen.