2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[GAS]Googleフォームの質問をスプレッドシートから追加する。

Last updated at Posted at 2020-06-27

やること

Googleフォームに質問を追加する際、選択肢(リストアイテム)が多い質問は手入力が大変なのでスプレッドシートから作成しました。

結果

下の画像のような質問を追加します。
このケースは、言語を選択してもらう質問です。選択肢として108言語用意しなければなりませんでしたので手入力では大変です。

image.png

image.png

準備

リストアイテムのソースとなるスプレッドシートを用意します。
下図のシートを用意しました。

image.png

Google Apps Scriptコード

下記のようなコードを書きました。
今回は、スプレッドシートのコンテナバインドスクリプトとして作成していますが、フォームのコンテナバインドから作成しても良いかと思います。その場合、SpreadsheetApp.getActive()から取得するのではなく、SpreadsheetApp.openByIdから取得するようにしてください。

function myFunction() {
  //activeなスプレッドシートの1枚目のシートを取得する。
  const srcst = SpreadsheetApp.getActive().getSheets()[0]; 
  //回答に使用するスプレッドシートのデータA列のみを取得する。
  let values = srcst.getRange(1, 1, srcst.getLastRow() , 1).getValues(); 

  //フォームをフォームIDから取得する。
  const fm = FormApp.openById(formId);

  //フォームにリストアイテム形式の質問を追加する。
  // question 1
  const q1 = fm.addListItem();
  q1.setTitle("翻訳前言語"); //質問のタイトル
  q1.setHelpText("翻訳前の言語を選択してください。"); //質問の説明文
  q1.setChoiceValues(values); //質問の選択肢
  q1.setRequired(true); //回答タイプが必須回答orオプショナル回答(true/false)

  // question 2
  const q2 = fm.addListItem();
  q2.setTitle("翻訳後言語");
  q2.setHelpText("翻訳後の言語を選択してください。");
  q2.setChoiceValues(values);         
  q2.setRequired(true);
}

スクリプトを実行する。

上記のコードを実行してフォームを確認してみると、質問が追加されるのがわかるかと思います。

詳細の解説

▼より詳細な解説は下記の記事にまとめていますのでよかったら
https://www.teijitaisya.com/gas-create-multiform-from-sheet/

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?