How to exclude null or empty values using Script Includes?

Lisa Goldman
Kilo Sage

Hello,

I'm using the following script to parse an Excel file:

var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();

// use attachment sys id of an excel file
var attachmentStream = attachment.getContentStream('61d8600297262110610dbfb6f053af1c');

parser.parse(attachmentStream);

//retrieve the column headers
var headers = parser.getColumnHeaders();
var header1 = headers[0];
var header2 = headers[1];

//print headers
gs.info(header1 + " " + header2);

while(parser.next()) {
var row = parser.getRow();
//print row value for both columns
gs.info(row[header1] + ' ' + row[header2])
}

The output including "Null" values:

 

LisaGoldman_0-1683083182666.png

I just want to print the row with values.  Can someone help please?

 

Thank you

 

 

 

 

 

2 ACCEPTED SOLUTIONS

Sandeep Rajput
Tera Patron
Tera Patron

@Lisa Goldman Please try the following script.

var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();

// use attachment sys id of an excel file
var attachmentStream = attachment.getContentStream('61d8600297262110610dbfb6f053af1c');

parser.parse(attachmentStream);

//retrieve the column headers
var headers = parser.getColumnHeaders();
var header1 = headers[0];
var header2 = headers[1];

//print headers
if (header1 && header2) {
    gs.info(header1 + " " + header2);
}


while (parser.next()) {
    var row = parser.getRow();
    //print row value for both columns
    if (row[header1] && row[header2]) {
        gs.info(row[header1] + ' ' + row[header2]);
    }

}

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Lisa Goldman 

try to check if value is not null

var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();

// use attachment sys id of an excel file
var attachmentStream = attachment.getContentStream('61d8600297262110610dbfb6f053af1c');

parser.parse(attachmentStream);

//retrieve the column headers
var headers = parser.getColumnHeaders();
var header1 = headers[0];
var header2 = headers[1];

//print headers
gs.info(header1 + " " + header2);

while(parser.next()) {
var row = parser.getRow();
//print row value for both columns
if(row[header1] != null && row[header2] != null)
gs.info(row[header1] + ' ' + row[header2]);
}

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Sandeep Rajput
Tera Patron
Tera Patron

@Lisa Goldman Please try the following script.

var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();

// use attachment sys id of an excel file
var attachmentStream = attachment.getContentStream('61d8600297262110610dbfb6f053af1c');

parser.parse(attachmentStream);

//retrieve the column headers
var headers = parser.getColumnHeaders();
var header1 = headers[0];
var header2 = headers[1];

//print headers
if (header1 && header2) {
    gs.info(header1 + " " + header2);
}


while (parser.next()) {
    var row = parser.getRow();
    //print row value for both columns
    if (row[header1] && row[header2]) {
        gs.info(row[header1] + ' ' + row[header2]);
    }

}

Ankur Bawiskar
Tera Patron
Tera Patron

@Lisa Goldman 

try to check if value is not null

var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();

// use attachment sys id of an excel file
var attachmentStream = attachment.getContentStream('61d8600297262110610dbfb6f053af1c');

parser.parse(attachmentStream);

//retrieve the column headers
var headers = parser.getColumnHeaders();
var header1 = headers[0];
var header2 = headers[1];

//print headers
gs.info(header1 + " " + header2);

while(parser.next()) {
var row = parser.getRow();
//print row value for both columns
if(row[header1] != null && row[header2] != null)
gs.info(row[header1] + ' ' + row[header2]);
}

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

If your using array then use this :- 

if (row[header1] != null) {
                    arrRes.push({
                        "case_number_mvs": row[header1// muti variables >> variable name                     
                    });

Amit Gujarathi
Giga Sage
Giga Sage

HI @Lisa Goldman ,
I trust you are doing great.
Looking at your current script, you are already pa

 

var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();

// use attachment sys id of an excel file
var attachmentStream = attachment.getContentStream('61d8600297262110610dbfb6f053af1c');

parser.parse(attachmentStream);

while(parser.next()) {
  var row = parser.getRow();
  //print row values for both columns
  gs.info(row[0] + ' ' + row[1]);
}

 

rsing the Excel file using the GlideExcelParser() function and retrieving the column headers using the getColumnHeaders() function. To print the row values, you simply need to modify the while loop to print the row variable instead of the headers. Here's an updated script:


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi