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

Hi,

Will this script work for NewYork and above versions.

 

Thanks,

Vamshi

 

nayeem2
Giga Expert

Hello All,



In Helsinki version



replace below line with "var sysparm_view="rpt-temp"+gel('sysparm_report_id').value+g_user.userName;"



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



Thanks


Nayeem


david_hreben
Giga Expert

Hello Everyone,



Thanks to Nayeem it is working as it should now. Below you can find the code with the fix Nayeem had proposed already tested in Helsinki & Istanbul releases. Hope is useful for anyone.



Script:


addLoadEvent(addExportXLButton);



function addExportXLButton(){



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


  //Creates element type


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


  //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 title


  sp1.setAttribute('title', 'Export this report to Excel');


  //Sets element type


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


  //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="rpt-temp"+gel('sysparm_report_id').value+g_user.userName;


  //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


I added the UIscript. Do I need to do any other changes to make this work. I was clicking on the open incidents and I do not see that new button that was created as a UIscript. AM I missing some thing.



Should I create a UI action that should be calling the UI script.


addLoadEvent(addExportXLButton);



Regards


Param


Hello Paramahanns,



The script is explained at the beginning of the initial post. It should be a Global UI Script & this button is added on the report module. Maybe you are looking for something else?