Read Attached excel file per cell via script

ariesmanlangit
Kilo Expert

Hi guys,

        I just want to know if there is a way where in I can able to access the rows,columns and/or cells in an attached excel file via script..

find_real_file.png

        In my scenario, I need to add the values from the attached file and add it into a custom table via gliderecord. Is there any way I can access the values per row,column and put it in a loop?

        //sample layout

        //code block here

        var addGroups = new GlideRecord('u_group_names');

     

        //add a while where to access the cells of the attached excel

        while(<rows not on limit>)

                  while(<columns not on limit>)

        addGroups.initialize();

        addGroups.u_group_name = attachedFile.rows(x).columns(y);

        addGroups.insert();

                  }

        }

Something like that.

Hoping this is possible.

Thanks,

a.c.manlangit

ServiceNow Developer - Philippines

9 REPLIES 9

I haven't heard of a way to do this. That doesn't mean it doesn't exist, only that I haven't heard of a way.


Onkar Pandav
Tera Guru

Hi,

I'm having similar requirement. Have you solved it?

Could you please help me on this?

nikhil_001
Tera Contributor

HI everyone,

 

I have a requirement in which we need to check first check the mandatory attachment in serviceportal, which could be done OOB now. Along with that we need to check whether the attachment is excel or not , and if it is excel we need to count the filled rows in it. Any help will be appreciated. I have the code for checking whether the attachment is an excel or not, but its not working in portal as we are using Dom manipulation there . Below is the working code in the Service-now console and also i have the code which counts the excel rows but not able to achieve in portal.. Below are both the codes.

 

Excel mandatory working in console but not in portal:

Client script

function onSubmit() {
var cat_id = sysparm_item_guid.value;
var attObj = new GlideRecord('sys_attachment');
attObj.addQuery('table_name', 'sc_cart_item');
attObj.addQuery('table_sys_id', cat_id);
attObj.query();
if (attObj.hasNext()) {
while (attObj.next()) {
var typeMatch = 'ms-excel,spreadsheetml';
var typeStr = attObj.content_type.toString();
var typeChk = (typeStr.indexOf(typeMatch[0]) > -1 || typeStr.indexOf(typeMatch[1]) > -1);
var fName = attObj.file_name.toString();
var regex = /\.xlsx$/g;
var extChk = regex.test(fName);
if(!typeChk || (typeChk && !extChk)) {
alert("You must use an Excel spreadsheet for uploading, please remove the existing file and re-attach");
return false;
}

}
}
}

 

Counting nunmber of rows

Background script

var count=0;
var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment();
// use attachment sys id of an excel file attachment
var attachmentStream = attachment.getContentStream('6e9c7aad1b772850c1534159cc4bcb5d'); //sys_id of record from sys_attachment table
parser.parse(attachmentStream);

//retrieve the column headers
var headers = parser.getColumnHeaders();
gs.print("Apoorva"+headers );
var key = headers[0];
var value = headers[1];
while(parser.next())
{
count++;
var row = parser.getRow();
//print row value for both columns
gs.print(row[value]) ; //Uncomment this to get actual data.
}

gs.print('Number of Rows in excel attached are '+count);

Hi,

Please create your own question so the conversation can be tracked appropriately.

You're posting this across numerous other threads.

https://community.servicenow.com/community?id=community_question&sys_id=13f687e5db1cdbc01dcaf3231f96...

https://community.servicenow.com/community?id=community_question&sys_id=4216f05adb3590106c1c02d5ca96...

Over 6+ threads at this point.

Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Created my own question

https://community.servicenow.com/community?id=community_question&sys_id=6d205beddb37285092bb0b55ca961918