Skip to content

Instantly share code, notes, and snippets.

@soundTricker
Last active December 16, 2015 06:39
Show Gist options
  • Save soundTricker/5393483 to your computer and use it in GitHub Desktop.
Save soundTricker/5393483 to your computer and use it in GitHub Desktop.
SpreadsheetからDriveへデータの書き込み(Date型データのフォーマット付き)
function writeSpreadsheetDataAsCsv() {
//Spreadsheetからデータ取得
var data = SpreadsheetApp.openById('id').getSheetByName('sheetName').getDataRange().getValues();
//二次配列→カンマ区切り文字列の一次配列
var writeDataArray = [];
for(var r = 0; r < data.length; r++) {
var row = data[r];
//Date型のデータをフォーマット
for(var c = 0; c < row.length; c++) {
if(row[c] instanceof Date) {
row[c] = Utilities.formatDate(row[c], 'Asia/Tokyo', 'yyyy/MM/dd');
}
}
writeDataArray.push(row.join(','));
}
//カンマ区切り文字列の一次配列→カンマ区切りで改行(\n)ありなテキストへ
var writeString = writeDataArray.join('\n');
//Blobデータ作成
var dataBlob = Utilities.newBlob(writeString, 'text/plain', Utilities.formatDate(new Date(), 'Asia/Tokyo', 'yyyyMMddHHmm') + '.csv');
//Driveにファイルを作成
DocsList.createFile(dataBlob);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment