LoginSignup
17
15

More than 5 years have passed since last update.

Google SpreadSheet のシートを Shift JIS の CSV 形式でダウンロードする GAS

Last updated at Posted at 2018-03-01

「ダウンロード」メニューの CSV (Shift JIS) を選ぶとダイアログが表示され、ダウンロードリンクをクリックすると SJIS 形式で DL できる。
動作確認は Chrome でしかしてない。

download.gs:

function onOpen() {
  const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  const entries = [{
    name : 'CSV (Shift JIS)',
    functionName : 'downloadSJIS_CSV'
  }];
  spreadsheet.addMenu('ダウンロード', entries);
}

function downloadSJIS_CSV() {
  // 開いてるシートのデータを取得
  const sheet = SpreadsheetApp.getActiveSheet();
  const sheetName = sheet.getName();
  const values = sheet.getDataRange().getValues();

  // 文字列のままテンプレートに渡して html を表示
  const template = HtmlService.createTemplateFromFile('download_dialog');
  template.values = JSON.stringify(values)
  template.name = sheetName;
  SpreadsheetApp.getUi().showModalDialog(template.evaluate(), 'CSV (Shift JIS)');
}

download_dialog.html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <a id="download" href="#" download="<?= name ?>.csv" target="_blank">ダウンロード</a>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.3.7/papaparse.js" integrity="sha256-QvijOmxLUGxTuoxamQikzFRUJ5hFoDOG71tl2M+LULw=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/encoding-japanese/1.0.28/encoding.min.js" integrity="sha256-vvOjQJ89zzmmngTqmXugSMR1JFKbDZuOxHMP3Ia8EdA=" crossorigin="anonymous"></script>
    <script>
      const values = JSON.parse(<?= values ?>)

      // CSV 文字列にする
      const csv = Papa.unparse(values)

      // encoding.js を使って csv 文字列を sjis array に変換
      const sjisCodeArray = Encoding.convert(csv, {
        from: 'UNICODE',
        to: 'SJIS',
        type: 'array'
      })

      // sjis array を blob に変換
      const uint8Array = new Uint8Array(sjisCodeArray)
      const blob = new Blob([ uint8Array ], { type: 'text/csv' })

      // URL を a タグにセット
      window.URL = window.URL || window.webkitURL
      document.getElementById("download").href = window.URL.createObjectURL(blob)
    </script>
  </body>
</html>

GAS タグついてるけど全然 GAS 書いてない :smirk:

GAS でやってるのはシートの二次元配列を JSON にしてテンプレートに渡すだけで、あとの処理はダイアログで開いた HTML でクライアント側にやらせちゃってる。
自分で言うのもなんだけど、なかなかおもしろいアプローチで他にも使える場面あるんじゃないかと思うし、きっちりメンテされてる JS のライブラリをそのまんま使えるので筋もいいと思う。

17
15
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
17
15