LoginSignup
3
2

More than 5 years have passed since last update.

Google DocumentsからExcelやPowerPointに一括変換する

Last updated at Posted at 2019-02-20

Google Documentsは超便利!

Google Documentsのスプレッドシートやスライド、本当に便利ですよね。チームで共有できて、更新がリアルタイムに反映されて、コメントを使って簡易的なToDo管理までできて。
一度使ってしまうと、ExcelやPowerPointのファイルで管理するのがものすごく面倒になってきます。

しかし、外部との共有が・・

作成したファイルを外部の組織にも共有する必要がある場合、Googleのアカウントを教えてもらって権限を与えるのが一般的です。

でも世の中にはまだまだこんな会社があるんです。

「ウチの会社はGoogle Driveは使用禁止です」
「ファイルはzipで圧縮してメールで送ってください。解凍パスワードは別のメールで送ってください」
「宅○ァイル便で送ってください」
「FTPサーバで送ってください」

宅フ○イル便は言うに及ばないですが、zipをメールで送るより二段階認証しているGoogleアカウントの方がずっと安全なのに・・・
まあ、共有したくない場合や静的なアーカイブとして保存しておきたい場合もありますね。

なので変換します

ファイル数が少ない場合には、手動でスプレッドシートからExcel、スライドからPowerPointに書き出しすれば問題ありません。
しかし、仕様書や定義書はどんどん数が増えるものです。
なので、一括して変換する方法を調査しました。

まず、新規でスプレッドシートを作成し、下記のように変換したいドキュメントのドキュメントIDを1列目に入れておきます。
ドキュメントIDは、ファイルを開いたときのURLからすぐにわかります。

https://docs.google.com/spreadsheets/d/(ドキュメントID)/edit

2列目には、スプレッドシートなら「xlsx」、スライドなら「pptx」と入力します。

スクリーンショット 2019-02-20 23.18.35.png

そして、「ツール>スクリプトエディタ」を選択し、下記のコードを入力します。

function exportFiles()
{
  var sheet = SpreadsheetApp.getActiveSheet();
  var rowIndex = 2;
  var documentId,outputFileName,type;

  while(true)
  { 
    documentId = sheet.getRange(rowIndex,1).getValue();  
    type = sheet.getRange(rowIndex,2).getValue();  

    if (!documentId || !type) break;

    var file = DriveApp.getFileById(documentId);
    outputFileName = file.getName();

    convertFile(documentId,outputFileName,type);

    rowIndex++;
  }
}

function convertFile(documentId,outputFileName,type) {
  outputFileName += "." + type;
  var url;

  if(type == "pptx")
  {
    url = 'https://docs.google.com/presentation/d/' + documentId + '/export/pptx?access_token=' + ScriptApp.getOAuthToken();
  }
  else if(type == "xlsx")
  {
    url = 'https://docs.google.com/spreadsheets/d/' + documentId + '/export?access_token=' + ScriptApp.getOAuthToken();
  }
  else
  {
    return;
  }  

  var rootFolder = DriveApp.getRootFolder();
  var response = UrlFetchApp.fetch(url);
  var blob = response.getBlob();
  var result = rootFolder.createFile(blob.setName(outputFileName));
}

そして、スクリプトエディタからexportFiles関数を実行すると、Google Driveのルートディレクトリに、xlsx形式やpptx形式のファイルが生成されます。

ToDo

Docs形式に対応
タイプの自動判別
書き出しフォルダを指定
変換対象のファイルをフォルダ単位で指定

3
2
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
3
2