google script スプレッドシートからのフォーム作成~5

GAS 公式サンプルコードの理解 Google Apps Script
この記事は約3分で読めます。
スポンサーリンク

フォームの送信ボタンがクリックされたとき起動する処理

作成したフォームの送信ボタンがクリックされたときに起動する処理。
スプレッドシートからユーザーが参加する会議情報を収集(Respons配列)する。
指定された名前とEmailアドレス宛へ収集した会議情報 (Respons配列) を送信(sendInvites_、sendDoc_関数)する。

Conference Setupがクリックされたときに起動するサンプルコード

 /**
 * A trigger-driven function that sends out calendar invitations and a
 * personalized Google Docs itinerary after a user responds to the form.
 *
 * @param {Object} e The event parameter for form submission to a spreadsheet;
 *     see https://developers.google.com/apps-script/understanding_events
 */
function onFormSubmit(e) {
  var user = {name: e.namedValues['Name'][0], email: e.namedValues['Email'][0]};

  // Grab the session data again so that we can match it to the user's choices.
  var response = [];
  var values = SpreadsheetApp.getActive().getSheetByName('Conference Setup')
     .getDataRange().getValues();
  for (var i = 1; i < values.length; i++) {
    var session = values[i];
    var title = session[0];
    var day = session[1].toLocaleDateString();
    var time = session[2].toLocaleTimeString();
    var timeslot = time + ' ' + day;

    // For every selection in the response, find the matching timeslot and title
    // in the spreadsheet and add the session data to the response array.
    if (e.namedValues[timeslot] && e.namedValues[timeslot] == title) {
      response.push(session);
    }
  }
  sendInvites_(user, response);
  sendDoc_(user, response);
} 


入力eは、イベントオブジェクト(トリガーを起動した コンテキスト)でここでは、アクティブスプレッドシートの情報を持つ。
namedValues:オブジェクトの値を取り出すメソッド。

次のサンプルコード

error: Content is protected !!
タイトルとURLをコピーしました