Help with GlideExcelParser to Capture Specific Worksheet and Rows from Protected Excel File

AvinashA
Tera Contributor

 

 

Dear ServiceNow Community,

 

I need help working with GlideExcelParser to capture data from an Excel template with a password protection (xlsx). The template contains both visible and hidden sheets, and I need to capture data only from the third worksheet ("Incidents") starting from row 8 (where row 7 contains the headers).

Currently, when parsing the Excel file, I’m capturing records from all sheets (both visible and hidden) rather than just the "Incidents" sheet starting from the 8th row. Here's the current code I’m using:

 

var gratt = new GlideRecord('sys_attachment');
gratt.addQuery('sys_id', 'attachment_sys_id');
gratt.query();

if (gratt.next() && gratt.file_name.indexOf(".xlsx") >= 0) {
var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();
var attachmentStream = attachment.getContentStream(gratt.sys_id);
parser.parse(attachmentStream);

var list_sheet_name = parser.getSheetNames();
parser.setSheetName(list_sheet_name[3]);
gs.info("Sheet Names: " + list_sheet_name[3]);

parser.setNullToEmpty(true);
var par = parser.setHeaderRowNumber(7);
gs.info('Header rows: ' + par);

var headers = parser.getColumnHeaders();
gs.info("Headers: " + headers);

while (parser.next()) {
var row = parser.getRow();
var customer = row[headers[3]].toString();
var state = row[headers[9]].toString();
var zip = row[headers[10]].toString();

if (customer && state && zip && zip.trim() !== "") {
var insertRecords = new GlideRecord("Custom Table");
// Insert the records after populating the necessary fields
insertRecords.insert();
}
}
}

 
1 REPLY 1

DrewW
Mega Sage
Mega Sage

Is there a reason you are not just using a file import and putting in the sheet and row numbers as needed to import the data?