Adding "Export to Excel" button on reports

Bhupinder1
Giga Expert

How to modify the OOB report page to add button "Export to Excel" similar to "Export to PDF"?

1 ACCEPTED SOLUTION

david_hreben
Giga Expert

Hello Rajput,



I was able to create an export to Excel button that would appear on every report next to the Export to PDF button. So you need to create a global UI SCRIPT and paste the code given below. Works great for those that do not want to do 2 clicks (and hover-over) to get the excel report.



Details:


-         Button is located next to the Export to PDF Button.


-         Takes a bit longer to load since is not included in source.


-         Does not export graphs,am currently working on it. The button disappears if the graph type is selected.


-         I still could not get the total rows of the report if grouped. If the report has a "Group by" filter then the scripts gets


the total rows of the grouped report (could say 30 but in reality has > 50000) so this will automatically download the report


without asking to mail it. Works fine with no "Group by" filters.


-         Anyone who wants to point to errors or contribute, welcome!




Script Type: Global UI Script


Code:


/*


*-----------------------------------------------------------------------------------------------------------------


* BEGIN UI-SCRIPT


*-----------------------------------------------------------------------------------------------------------------


*/


addLoadEvent(addExportXLButton);



function addExportXLButton(){


  //Create the button if the PDF export button exists and if the report is of type "list"


  if($('export_pdf_button') && (gel('sysparm_type').value == 'list') ){


  //Creates element type


  var   sp1 = document.createElement('button');


  //Assigns id


  sp1.id = 'export_excel_button';


  //Names the button


  sp1.innerHTML = 'Export to Excel';


  //Button attribute value


  sp1.setAttribute('value', 'Export to Excel');


  //Executes the function "runContextAction()" when the button is clicked


  sp1.setAttribute('onclick','runContextAction()');


  //Sets element type


  sp1.setAttribute('type', 'button');


  //obtains element value


  var   sp2 = document.getElementById('export_pdf_button');


  //Parents the export to PDF button


  var parentDiv = sp2.parentNode;


  //Inserts our button next to its parent "Export to PDF"


  parentDiv.insertBefore(sp1, sp2.nextSibling);


  }//End of If statement


  }//End of addExportXLButton() function




function runContextAction() {


  //Obtains the rows number of the current report


  var sysparm_rows1 =$$('.list_row_number_input span')[1].innerHTML;


  //Parses string value into an integer value


  var num_rows = parseInt(sysparm_rows1);


  //Gets report query


  var sysparm_query =   gel('sysparm_query').value;


  //Get view name needed to export


  var sysparm_view =gel('sysparm_view').value;


  //Creates export file if rows is less than limit value


  if (num_rows < g_export_warn_threshold) {


  var dialog = new GwtPollDialog(gel('sysparm_table').value, sysparm_query, sysparm_rows1, sysparm_view, 'unload_excel');


  dialog.execute();


  return;


  }//End of IF statement


  //Else Asks user to mail report or wait for it


  var dialog = new GwtExportScheduleDialog(gel('sysparm_table').value, sysparm_query, sysparm_rows1, sysparm_view, 'unload_excel');


  dialog.execute();


}//End of runContextAction() function



/*


*-----------------------------------------------------------------------------------------------------------------


* END UI-SCRIPT


*-----------------------------------------------------------------------------------------------------------------


*/



Let me know if it worked on your end, I just have done this on ServiceNow Eureka and I have not tested this out in other releases.


View solution in original post

23 REPLIES 23

Thanks for the update David. If you do figure out a way would you be able to email me at Rhiannon@soton.ac.uk please. I would be very interested to hear how you did it.


This no longer seems to quite work in Helsinki.   Rather than exporting with the columns from the report itself, it's exporting with a seemingly default set of columns.


Hi Nick,



do you find any solution for this??



if yes can you please share it is very helpful to me



Thanks in Advance



Thanks


Nayeem


Hello Nick,



I have not updated the script for over a year to begin with. There seems to be a few changes ever since regarding the user's view so the script no longer can get the user personalized (or current) view and so intead it obtains to the default one. I have found a work around but is not pretty, you must update the list view of the table you are exporting and then those will appear on the export. But as of now, the script will always show you the default list view of the table. I will let you know if I come up with something.


Hi Devid, how to generate the catalog forms as pdf.