We need to use external library for access contents from excel file.
Needed CDN Link : https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js
Upon opening the link will show javascript code, copy the code and create a JS file and save it in file cabinet. (ex: EXCEL.js)
Create a config.json file with content path reference to the created JS file.
{
"baseUrl": "./",
"paths": {
"jszip": "/SuiteScripts/Libraries/External/jszip",
"xlsx": "/SuiteScripts/Jobin and Jismi IT Services LLP/Read Excel FIle/EXCEL"
}
}
In suitescript call the module path using non AMD module. Complete code is below for reference
/**
* @NApiVersion 2.1
* @NModuleScope Public
* @NScriptType ScheduledScript
* @NAmdConfig /SuiteScripts/Jobin and Jismi IT Services LLP/Read Excel FIle/nse_sc_excel_sample_conf.json
*
* @module N/search
* @module N/file
* @module xlsx
*
* @description Convert Excel file to json
*/
define(['N/search', 'N/file', 'xlsx'],
function (search, file, XLSX) {
/**
* @function excelFileToJson
* @description Reads excel file and returns JSON data.
*
* @module N/file
* @module xlsx
*
* @param {integer} fileId - Internal ID of the Excel file
* @param {object} headers - Object that contains array of header line columns per sheet
* @return {array} - Array of objects that contains sheet data
*/
let excelFileToJson = (fileId) => {
let returnData = [];
let excelFile = file.load({
id: fileId
});
let workbook = XLSX.read(excelFile.getContents(), {type: 'base64'});
log.debug('workbook',workbook)
var firstSheetName = workbook.SheetNames[0];
//reading only first sheet data
var jsonData = XLSX.utils.sheet_to_json(workbook.Sheets[firstSheetName]);
let stringifyData=JSON.stringify(jsonData)
log.debug('jsonDatalength',jsonData.length)
log.debug('jsonData',jsonData)
return returnData;
}
return {
execute: (context) => {
let excelJsonData = excelFileToJson(40178);
}
};
});