Last active
June 26, 2017 17:39
-
-
Save ccavazos/8b22bb176c34d302bc62 to your computer and use it in GitHub Desktop.
Titanium Snippet to Export data to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Common JS module | |
exports.exportCsvData = function(input) | |
{ | |
var rowTxt = ""; | |
for(var i=0;i < input.length; i++){ // row iteration | |
for(var j = 0; j < input[i].length; j++){ // column iteration | |
rowTxt += '"' + input[i][j] + '"'; | |
if(j < (input[i].length-1)) { | |
rowTxt += ','; | |
} | |
} | |
rowTxt += '\n';// adding new line at end of row | |
} | |
// creating output file in application data directory | |
var outputFile = Ti.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'output.csv'); | |
// writing data in output file | |
outputFile.write(rowTxt); | |
if (outputFile.exists) { | |
alert("CSV generated!!!"); | |
} | |
// return output file path | |
return outputFile.nativePath; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var csv = require('exportCsvData'); | |
var win = Ti.UI.createWindow({ | |
backgroundColor:'#ccc', | |
title:'CSV Import Module' | |
}) | |
var createCsv = Titanium.UI.createButton({ | |
title:'Export CSV', | |
top:'140', | |
left:'110', | |
height:'40', | |
width:'115', | |
color:'black' | |
}); | |
win.add(createCsv); | |
win.open(); | |
createCsv.addEventListener('click', function(e){ | |
var input = [ | |
["sample data 0", "sample data 1", "sample data 2", "sample data 3"], | |
["sample data 0", "sample data 1", "sample data 2", "sample data 3"], | |
["sample data 0", "sample data 1", "sample data 2", "sample data 3"], | |
["sample data 0", "sample data 1", "sample data 2", "sample data 3"] | |
]; | |
var op = csv.exportCsvData(input); | |
alert("Output file path = "+ op); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment