logo

NJP

Read Excel File Using Script

Import · Jul 27, 2023 · article

We can read Excel from a server-side script using the below code.

Example: Suppose I have one Excel file which has 3 field Employee ID, LAST NAME, and FIRST NAME. I wanted to retrieve all the data from Excel and wanted to store it in any table or wanted to show it in MRVS.

Please use or leverage the below code based on your needs.

function prepareData(row) {
    myData = {};
   //Update the below variables based on your Excel column name and need
    myData.employee_id = row['Employee ID'];
    myData.last_name = row['LAST NAME'];
    myData.first_name = row['FIRST NAME'];
    return myData;
}
var myobjArray = [];
var attachmentSID = '{SYS_ID}'; // pass your excel attachment sys_id
var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();

// use attachment sys id of an excel file
var attachmentStream = attachment.getContentStream(attachmentSID);
parser.parse(attachmentStream);
while (parser.next()) {
    var row = parser.getRow();
    myobjArray.push(prepareData(row));
}
gs.info(JSON.stringify(myobjArray)); 

Please mark this article help full if it helps you in any manner.

Thanks,

Pradyumna Das

View original source

https://www.servicenow.com/community/developer-articles/read-excel-file-using-script/ta-p/2626451