Export Data from Background Script to Excel

DebjitGhosh31
Tera Contributor

Hi,

 

I am currently using a script that stores all duplicate CI serial number in an array. Now my requirement is to export the data stored in the array to an excel file. 

Is there any way I can dump the data from the script to an excel file.

 

Regards,

Debjit

1 ACCEPTED SOLUTION

Hi,
Below is sample code

 

var grX = new GlideRecord("sys_user");//some where we need to save the file here I am attaching on one of the user record logged in one
var csvData="";//This should always be in the 2D array format
//FieldLabel1,FieldLabel2,FieldLabel3,FieldLabel4=====This Row is For Header of CSV
//Val11,Val12,Val13,Val14==== Row 1
//Val21,Val22,Val23,Val24 ==== Row 2
  grX.addQuery('sys_id',gs.getUserID());
  grX.query();
  if(grX.next())
  {
csvHeader="UserID,Name,Company\n";
csvRows1="ABC1,Mario1,SPM1\n";
csvRows2="ABC2,Mario2,SPM2\n";
csvRows3="ABC3,Mario3,SPM3\n";
csvData=csvHeader+csvRows1+csvRows2+csvRows3;
    var grAttachment = new GlideSysAttachment();
    grAttachment.write(grX, "User Details.csv", 'application/csv', csvData);
  }

 

 


Thanks and Regards,

Saurabh Gupta

View solution in original post

11 REPLIES 11

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi,
Excel I am not sure in csv you can do.

 

 


Thanks and Regards,

Saurabh Gupta

Hi @Saurabh Gupta  ,

 

Thanks for your response.

How do I post in CSV?

 

Hi,
Below is sample code

 

var grX = new GlideRecord("sys_user");//some where we need to save the file here I am attaching on one of the user record logged in one
var csvData="";//This should always be in the 2D array format
//FieldLabel1,FieldLabel2,FieldLabel3,FieldLabel4=====This Row is For Header of CSV
//Val11,Val12,Val13,Val14==== Row 1
//Val21,Val22,Val23,Val24 ==== Row 2
  grX.addQuery('sys_id',gs.getUserID());
  grX.query();
  if(grX.next())
  {
csvHeader="UserID,Name,Company\n";
csvRows1="ABC1,Mario1,SPM1\n";
csvRows2="ABC2,Mario2,SPM2\n";
csvRows3="ABC3,Mario3,SPM3\n";
csvData=csvHeader+csvRows1+csvRows2+csvRows3;
    var grAttachment = new GlideSysAttachment();
    grAttachment.write(grX, "User Details.csv", 'application/csv', csvData);
  }

 

 


Thanks and Regards,

Saurabh Gupta

Hi @Saurabh Gupta  ,

Thank you for the response.

Allthough the requirement is not exactly the same, but I think I will be able to work with the code you have shared and make adjustment to make it work.

 

Regards,

Debjit