Skip to content

Instantly share code, notes, and snippets.

@llad
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save llad/004e943c55b72ab65325 to your computer and use it in GitHub Desktop.
Save llad/004e943c55b72ab65325 to your computer and use it in GitHub Desktop.
Export Salesforce Developer Console query #paste
// credit: Ir0nm on StackExchange
// Reference: http://salesforce.stackexchange.com/questions/15328/export-results-from-developer-console-query-editor
var o = document.evaluate("//div[@id='editors-body']/div[not(contains(@style,'display:none') or contains(@style,'display: none'))]//table/tbody/tr",document,null,0,null);
var r = [];
while(row = o.iterateNext()){
var cols = row.getElementsByTagName('td');
var a = [];
for(var i=0; i<cols.length; i++){
a.push( formatData( cols[i].textContent ) );
}
r.push( a );
}
// generating csv file
var csv = "data:text/csv;charset=utf-8,filename=download.csv,";
var rows = [];
for(var i=0; i<r.length; i++){
rows.push( r[i].join(",") );
}
csv += rows.join('\r\n');
popup(csv);
function formatData(input) {
// replace " with “
var regexp = new RegExp(/["]/g);
var output = input.replace(regexp, "“");
//HTML
var regexp = new RegExp(/\<[^\<]+\>/g);
var output = output.replace(regexp, "");
if (output == "") return '';
return '"' + output + '"';
}
// showing data in window for copy/ paste
function popup(data) {
var generator = window.open('', 'csv', 'height=400,width=600');
generator.document.write('<html><head><title>CSV</title>');
generator.document.write('</head><body style="overflow: hidden;">');
generator.document.write('<a href="'+encodeURI(csv)+'" download="Sf_export.csv">Download CSV</a><br>');
generator.document.write('<textArea style="width: 100%; height: 97%;" wrap="off" >');
generator.document.write(data);
generator.document.write('</textArea>');
generator.document.write('</body></html>');
generator.document.close();
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment