Last active
March 3, 2016 05:15
-
-
Save kei-p/b9517b901c32bbc622ed to your computer and use it in GitHub Desktop.
GAS spreadsheet to json
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
<style type="text/css"> | |
#json { | |
height: 320px; | |
overflow: scroll; | |
border:1px solid #ddd; | |
padding:10px; | |
} | |
</style> | |
<pre align="left" id="json"> | |
<? | |
output.append(body); | |
?> | |
</pre> | |
<div align="center"> | |
<? | |
output.append("<a href='" + url + "' target='_blank'>ダウンロード</a>"); | |
?> | |
</div> |
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 fetchData(){ | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
/* sheetを元にデータを作成 */ | |
return data; | |
} | |
function renderToJson(data) { | |
return JSON.stringify(data, null , 2); | |
} | |
function showDialogDowloadJson() { | |
var contentType = "text/plain"; | |
var charSet = "UTF-8"; | |
var lineDelimiter = ","; | |
var newLineChar = "\n"; | |
var fileName = 'hoge.json'; | |
var folderId = 'aaaaaaaaaaaaaaa' | |
var data = fetchData(); | |
var folder = DriveApp.getFolderById(folderId); | |
body = renderToJson(data); | |
var blob = Utilities.newBlob("", contentType, fileName).setDataFromString(body, charSet); | |
var fileId = DriveApp.getFolderById(folderId).createFile(blob).getId(); | |
url = "https://drive.google.com/uc?export=download&id=" + fileId; | |
var output = HtmlService.createTemplateFromFile('download'); | |
var html = output.evaluate().setHeight(420).setWidth(600); | |
var ui = SpreadsheetApp.getUi(); | |
ui.showModalDialog(html, fileName); | |
} | |
function onOpen() { | |
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var entries = [{ | |
name : "シートをJSONで出力", | |
functionName : "showDialogDowloadJson" | |
}]; | |
spreadsheet.addMenu("書き出し", entries); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment