概要
紙の受付票や問診票など“構造化できる紙フォーム”を、構造化データ(JSON/JSオブジェクト)を元に Google フォームへ自動生成する実践ガイドです。実行可能な Google Apps Script(GAS)をそのまま貼ってあるので、コピペで動かせます。
ポイント(要約)
- フォームは「DSL(構造化定義)→GAS→FormApp」で自動生成可能
- 選択肢(Choice)は Item オブジェクトの
createChoice()を使う(FormApp.createChoice()は存在しない) - 既存フォームの差分更新や質問ID固定は工夫が必要(再作成前提がシンプル)
注意(匿名化)
本文は一般的な受付票を例にしています。個別の診療所名や個人情報は含めていません。
フォーム定義(サンプル:そのまま使えるJSオブジェクト)
以下は「紙→フォーム」に変換するためのスキーマ例です。必要に応じて項目を追加・編集してください。
const FORM_SCHEMA = {
title: "受付票(サンプル)",
description: "紙の受付票をフォーム化するサンプル",
sections: [
{
title: "基本情報",
items: [
{ type: "text", title: "ふりがな" },
{ type: "text", title: "氏名", required: true },
{ type: "multipleChoice", title: "性別", choices: ["男", "女"] },
{ type: "date", title: "生年月日" },
{ type: "text", title: "住所" },
{ type: "text", title: "携帯番号" },
{ type: "text", title: "同居の家族構成", help: "例:祖母・母・父・弟6歳" }
]
},
{
title: "問診(例)",
items: [
{
type: "checkbox",
title: "いかがされましたか?",
choices: ["健診","フッ素","虫歯予防","クリーニング","歯並びが気になる","虫歯","外傷","その他"]
},
{ type: "text", title: "最後に歯科医院に行かれたのはいつ頃ですか?" },
{ type: "text", title: "歯科医院名" },
{ type: "multipleChoice", title: "今回がはじめてですか?", choices: ["はじめて","わからない"] },
{ type: "multipleChoice", title: "現在痛みはありますか?", choices: ["ない","ある"] },
{ type: "text", title: "いつ頃から痛みがありますか?" }
]
},
{
title: "病歴・薬剤・アレルギー",
items: [
{ type: "multipleChoice", title: "通院中・治療中の病気はありますか?", choices: ["ない","ある"] },
{ type: "text", title: "病名/服用中の薬剤名" },
{ type: "multipleChoice", title: "アレルギーはありますか?", choices: ["ない","ある"] },
{ type: "text", title: "アレルギー内容" },
{ type: "multipleChoice", title: "フッ素を使用してよいですか?", choices: ["はい","いいえ"] }
]
},
{
title: "来院理由・希望",
items: [
{ type: "checkbox", title: "来院の動機(複数選択可)", choices: ["近所","紹介","看板を見て","Instagramで検索","Googleで検索","その他"] },
{ type: "text", title: "検索ワード(任意)" },
{ type: "text", title: "要望・注意事項(任意)" },
{ type: "multipleChoice", title: "夕方/土曜以外での通院は可能ですか?", choices: ["はい","いいえ"] },
{ type: "text", title: "希望の曜日・時間帯", help: "例:火曜日" }
]
},
{
title: "治療希望・同意",
items: [
{ type: "checkbox", title: "興味のある治療(複数選択可)", choices: ["矯正","予防歯科"] },
{ type: "multipleChoice", title: "キャンセルポリシーに同意しますか?", choices: ["はい","いいえ"], required: true }
]
},
{
title: "保護者確認(必要時)",
items: [
{ type: "text", title: "ご自身が最後に歯科医院にかかったのはいつ頃ですか?" }
]
}
]
};
実行可能な GAS スクリプト(コピペして使える)
以下を Google Apps Script のエディタに貼り付けて run() を実行してください。新しいフォームが作成され、ログに編集用URLと公開用URLが出ます。
function createFormFromSchema(schema) {
const form = FormApp.create(schema.title);
if (schema.description) form.setDescription(schema.description);
schema.sections.forEach(section => {
form.addSectionHeaderItem().setTitle(section.title);
section.items.forEach(item => {
addItem(form, item);
});
});
Logger.log('Edit URL: ' + form.getEditUrl());
Logger.log('Response URL: ' + form.getPublishedUrl());
}
function addItem(form, item) {
let q;
switch (item.type) {
case 'text':
q = form.addTextItem();
break;
case 'multipleChoice':
q = form.addMultipleChoiceItem();
q.setChoices(item.choices.map(c => q.createChoice(c)));
break;
case 'checkbox':
q = form.addCheckboxItem();
q.setChoices(item.choices.map(c => q.createChoice(c)));
break;
case 'date':
q = form.addDateItem();
break;
default:
throw new Error('Unknown item type: ' + item.type);
}
q.setTitle(item.title);
if (item.help) q.setHelpText(item.help);
if (item.required) q.setRequired(true);
if (item.other) {
// multipleChoice や checkbox に「その他」を付ける
try {
q.setHasOtherOption(true);
} catch (e) {}
}
}
function run() {
createFormFromSchema(FORM_SCHEMA);
}
// ペーストしたらこれも忘れずに
// const FORM_SCHEMA = ... (上の定義を同じスクリプトに貼る)
実務Tips
- 既存フォームの微修正だけを行いたい場合は、質問タイトルでマッチして上書きするロジックを追加すると安全です。完全差分はやや手間。
- OCR→スキーマ変換を作れば、紙運用から自動化までワンパスで行えます。
- 回答の受け口は
Form#getResponses()で取得できます。必要ならスプレッドシート連携や Webhook 経由で別システムに流せます。
次のステップ提案
- OCR の結果をこの
FORM_SCHEMAに自動変換するサンプルを作る(この記事の続編) - スプレッドシートをフォームDSLとして扱う実装
- 既存フォームとの差分更新(質問ID固定を工夫)
以上。必要なら私のほうで英語版も用意して、予約投稿しておきます。