Created
June 9, 2014 07:22
-
-
Save shinriyo/95ad065ffc70fb66f777 to your computer and use it in GitHub Desktop.
Google Spread SheetでCSVをダウンロードするためのGoogle Apps Script
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
function downloadDoubleQuateCsvFile() { | |
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); | |
var maxColumn = ss.getLastColumn(); | |
var maxRow = ss.getLastRow(); | |
var data = ss.getRange(1, 1, maxRow, maxColumn).getValues(); | |
if (data.length > 1) { | |
var csv = ""; | |
for (var row = 0; row < data.length; row++) { | |
for (var col = 0; col < data[row].length; col++) { | |
if (data[row][col].toString().indexOf(",") != - 1) { | |
data[row][col] = "\"" + data[row][col] + "\""; | |
} | |
} | |
if (row < data.length - 1) { | |
csv += data[row].join(",") + "\r\n"; | |
} else { | |
csv += data[row]; | |
} | |
} | |
csvFile = csv; | |
} | |
makeCSV(csvFile); | |
} | |
function makeCSV(csvString) { | |
var csvFileName = 'test.csv'; | |
var output = ContentService.createTextOutput(); | |
output.setMimeType(ContentService.MimeType.CSV); | |
output.setContent(csvString); | |
output.downloadAsFile(csvFileName); | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment