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

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.


Hi David Hreben,


I just tried in demo.It's working fine.Great work.


Great David Hreben   Worked Fine.



Thanks for sharing


Thanks David for sharing this!!