functionformatCsv(rows,config){config=config||{};// CSV delimitervardelim=config.delimiter||",",// New Line characternewl=config.newline||"\n",// By default take all original columns, dimensions and measuresheader=config.header||Object.keys(rows[0]).filter(function(key){returnObject.hasOwnProperty.call(rows[0],key)&&typeofrows[0][key]!=="function";});// format the CSV with the headerreturn[header.join(delim)].concat(rows.map(function(row){returnheader.map(function(col){returnrow[col];}).join(delim);})).join(newl);}functiontriggerDownload(content,config){config=config||{};varfilename=config.filename||"test.csv",mimetype=config.mime||"text/csv",charset=config.charset||"utf-8";//CSV will be downloaded immediately but this can be invoked in any eventvarencodedUri="data:"+mimetype+";charset="+charset+","+encodeURI(content);varlink=document.createElement("a");link.setAttribute("href",encodedUri);link.setAttribute("download",filename);document.body.appendChild(link);link.click();}gsp.ready("v1.0",function(gsp){// TODO: remove in Studiogsp.bi.stage.findSelectedRecords({//layer: "labor_geom"},function(ret){// ["id1", "id2", ..., "idn"]// do something with retvarcsv=formatCsv(ret,{// delimiter: ","// newline: "\n",// header: ["id_woj", "YEAR", "POPULATION_TOTAL", "WAGES_TOTAL_REAL"]});triggerDownload(csv,{// filename: "test.csv"// mime: "text/csv"// charset: "utf-8"});},function(err){// display errconsole.error(err)});})// TODO: remove in Studio
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.