Created
August 23, 2014 04:36
-
-
Save jsoizo/580d65b2de2e96fa32b5 to your computer and use it in GitHub Desktop.
GASで文字コード指定してファイルを書き出す ref: http://qiita.com/jsoizo/items/948e23231417d203eefa
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 outputSheetToCsvFile() { | |
// 定数 | |
var fileName = "μ's.csv"; | |
var contentType = "text/csv"; | |
var charSet = "Shift_JIS"; | |
var lineDelimiter = ","; | |
var newLineChar = "\r\n"; | |
// 開いているシートからデータを取得 | |
var range = SpreadsheetApp.getActiveSheet().getDataRange(); | |
var values = range.getValues(); | |
// 2次元配列になっているデータをcsvのstringに変換 | |
var csvString = underscoreGS._map( | |
values, | |
function(row){return row.join(lineDelimiter);} | |
).join(newLineChar); | |
// Shift_JISなBlobに変換 | |
var blob = Utilities.newBlob("", contentType, fileName).setDataFromString(csvString, charSet); | |
// Blobをファイルに出力 | |
DocsList.createFile(blob); | |
} | |
function onOpen() { | |
// メニューバーにカスタムメニューを追加 | |
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var entries = [{ | |
name : "シートをCSV(Shift_JIS)で出力", | |
functionName : "outputSheetToCsvFile" | |
}]; | |
spreadsheet.addMenu("CUSTOM MENU", entries); | |
}; |
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 blob = Utilities.newBlob("", contentType, fileName); | |
blob.setDataFromString(string, charSet); | |
DocsList.createFile(blob); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment