LoginSignup
5
9

More than 3 years have passed since last update.

Google Formを使って、テストと受講証明pdfを自動発行

Last updated at Posted at 2020-04-23

承前

2019年度まで講演会を行って、その参加に対して受講証を発行していました。
残念ながらコロナ騒ぎで開催できなくなったので、替わりにウェブからクイズに答えてもらい受講証明を自動発行する仕組みを作りました。
参加者ごとの認証の仕組みを組織として持っていないので、わざわざ名前を入れた人は本人だろう、という性善説を採用しています。

準備するもの

  • Googleアカウント
  • Googleフォーム

クイズの作り方
自動で採点・結果通知もできる!GoogleフォームでWEBテストを無料作成する方法
などを参照。返信メールを送るために、メールアドレスは収集するように設定します。

  • スプレッドシート

Googleフォームが完成したら、集計用のスプレッドシートを作成します。
編集画面上よりにある回答タブをクリックし、その下にある回答をスプレッドシートに表示アイコンをクリック
image.png

4問のクイズがあるフォームを想定しています。
作成した後で、Jの列のところに"発行済み"列を新規に作っておきます。
image.png

  • 受講証の元になるドキュメントファイル

ドキュメントファイルの例

GASを書こう

スプレッドシートで、ツール>スクリプトエディタ
image.png

以下をコピペする

henshin.gs

function onFormSubmit() {
  /*データ取得*/
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheet       = spreadsheet.getSheetByName('フォームの回答 1');
const values      = spreadsheet.getDataRange().getDisplayValues();
console.log(values.length);

for(let i = 2; i < values.length; i++){
  if(values[i][9]!=='作成済' && values[i][2]=='4 / 4'){ 
//ここで満点かつ作成済となっていない行を取得する。満点のフラグにスペースがあることに注意
//クイズの数によって、"9"を書き換える

  const STime          = values[i][0]; //タイムスタンプを取得。0がA列、1がB列に対応
  const email     = values[i][1]; //メアドを取得
  const name     = values[i][3];  //名前を取得 ※フォームで設定した変数に依存する
  const department = values[i][4]; //所属を取得 ※フォームで設定した変数に依存する

  const fileName         = name +' '+ ' 発行日 ' +STime;
  console.log(fileName);

    /*作成済と入力*/
    const rangeNumber = i + 1;

    sheet.getRange(rangeNumber, 10) //上だと"9"だがここでは"10"で同じセルを指定していることになる!!!
    .clearContent()
    .setValue('作成済');

    console.log('rangeNumber = %s  範囲 %s',rangeNumber,sheet.getRange(rangeNumber, 8).getA1Notation());
    console.log(i);


    /*ファイルのID*/
  const sourceDocument   = DriveApp.getFileById('1SN1mjI-y-_O_b6pPSwW0DqSJT1CPBYrZi49T_OZe7Uo');//テンプレートドキュメントのID
  console.log(sourceDocument.getName());

    /*コピーしたファイルの保存先*/
  const copyDir          = DriveApp.getFolderById('********');//folderのID。自分で設定する

    /*コピーしたドキュメントの処理内容ここから*/
  const duplicateDocument   = sourceDocument.makeCopy(fileName, copyDir);//makeCopy(コピー後のリネーム、コピーしたファイルを保存する場所の指定)
  const duplicateDocumentId = duplicateDocument.getId();
  console.log(duplicateDocumentId);

    /*コピーしたドキュメントのURLを生成*/
  const createUrl           = 'https://docs.google.com/document/d/' + duplicateDocumentId + '/edit';
  console.log(createUrl);


    /*コピーしたドキュメントをIDで開く*/
  const targetDocument      = DocumentApp.openById(duplicateDocumentId);
  console.log(targetDocument.getName());

    /*ドキュメントの内容(placeholder1や、placeholder2)をフォームで取得した変数に差し替え*/
  const targetBody          = targetDocument.getBody();
  const replaceText         = targetBody
  .replaceText('placeholder1',STime)
  .replaceText('placeholder2',department)
  .replaceText('placeholder3',name)
  targetDocument.saveAndClose(); //この一行で上書きされる

  /*pdf化*/
  var pdf = targetDocument.getAs('application/pdf');
   copyDir.createFile(pdf);   


   /*作成したPDFファイルをメールに添付して送る*/
   /*本文は修正可能*/
  var mail = email
   var subject = '講習会受講票'
   var body = '添付ファイルにて送付いたします'
   MailApp.sendEmail(mail, subject, body, {attachments:pdf}); 
  }//if
    }//for
  }//end

修正が終わったら上書きする。
フォームが入力される度にこのスクリプトが動くように、時計マークをクリック。
image.png

クリック
image.png

イベントの種類を選択で、フォーム送信時に設定し、保存
image.png

あとは勝手にやってくれます。

参考

5
9
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
5
9