LoginSignup
2
3

More than 5 years have passed since last update.

Spreadsheetをパースしてjsonとしてダウンロードする

Last updated at Posted at 2018-02-16

できるかぎりミニマムに。
Blob化してObjectURL化してanchorのdownload属性に設定することでダウンロードさせる。

できるもの

SS_ 2018-02-16 20.00.38.png

コード

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));
}
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3