できるかぎりミニマムに。
Blob化してObjectURL化してanchorのdownload属性に設定することでダウンロードさせる。
できるもの
コード
ExportDialog.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script type="text/javascript">
window.onload = function () {
var json = "<?= data.json ?>"
var blob = new Blob([ json ], { "type" : "application/json" });
document.getElementById("dl-link").href = window.URL.createObjectURL(blob);
return true
}
</script>
<style>
.button {
background-color: #009688;
border-radius: 5px;
color: white;
padding: .5em;
text-decoration: none;
}
.button:focus,
.button:hover {
background-color: #00796B;
}
</style>
</head>
<body>
<a id="dl-link" class="button" href="" download="<?= data.name ?>">
Download <?= data.name ?>
</a>
</body>
</html>
lib.gs
function showExportJsonDialog(name, json) {
data = {
name: name,
json: json
}
var html = HtmlService
.createTemplateFromFile('ExportDialog')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(200)
.setHeight(50);
SpreadsheetApp.getUi().showModalDialog (html, 'Export');
}
example.gs
function main() {
var users = [
{name: '太郎', age: 16},
{name: '二郎', age: 12},
{name: '三郎', age: 7}
];
showExportJsonDialog('users.json', JSON.stringify(users, null, 2));
}