UrlFetchApp.fetch(webhookUrl, options);
}
+
+ /**
+ * スプレッドシート開閉時にカスタムメニューを作成
+ */
+ function onOpen() {
+ const ui = SpreadsheetApp.getUi();
+ ui.createMenu('🤖 アンケートシステム')
+ .addItem('⚙️ 初期設定', '初期設定')
+ .addSeparator()
+ .addItem('🔗 Webhook URL登録', 'メニューからWebhook登録')
+ .addItem('🧪 Discord接続テスト', 'Discord接続テスト')
+ .addSeparator()
+ .addItem('📊 アンケート項目をMD出力', 'アンケート項目MD出力')
+ .addItem('📝 現在の設定確認', '設定確認')
+ .addSeparator()
+ .addItem('🗑️ 設定リセット', '設定リセット')
+ .addToUi();
+ }
+
+ /**
+ * メニューからWebhook URLを登録
+ */
+ function メニューからWebhook登録() {
+ const ui = SpreadsheetApp.getUi();
+ const result = ui.prompt(
+ '🔗 Discord Webhook URL登録',
+ 'DiscordのWebhook URLを入力してください:',
+ ui.ButtonSet.OK_CANCEL
+ );
+
+ if (result.getSelectedButton() === ui.Button.OK) {
+ const url = result.getResponseText().trim();
+ if (!url) {
+ ui.alert('❌ エラー', 'URLが入力されていません', ui.ButtonSet.OK);
+ return;
+ }
+
+ if (!url.includes('discord.com/api/webhooks/')) {
+ ui.alert('❌ エラー', '正しいDiscord Webhook URLを入力してください', ui.ButtonSet.OK);
+ return;
+ }
+
+ // スクリプトプロパティに保存
+ PropertiesService.getScriptProperties().setProperty('DISCORD_WEBHOOK_URL', url);
+
+ // スプレッドシートにも記録
+ const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
+ sheet.getRange('B3').setValue(url);
+
+ ui.alert('✅ 成功', `Webhook URLを登録しました:\n${url}`, ui.ButtonSet.OK);
+ }
+ }
+
+ /**
+ * 設定確認
+ */
+ function 設定確認() {
+ const ui = SpreadsheetApp.getUi();
+ const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
+ const webhookUrl = PropertiesService.getScriptProperties().getProperty('DISCORD_WEBHOOK_URL');
+ const formUrl = sheet.getRange('B1').getValue();
+
+ let message = '📋 現在の設定状況:\n\n';
+ message += `📝 フォームURL: ${formUrl ? '✅ 設定済み' : '❌ 未設定'}\n`;
+ message += `🔗 Webhook URL: ${webhookUrl ? '✅ 設定済み' : '❌ 未設定'}\n`;
+
+ if (webhookUrl) {
+ message += `\nWebhook URL: ${webhookUrl}`;
+ }
+
+ ui.alert('⚙️ 設定確認', message, ui.ButtonSet.OK);
+ }
+
+ /**
+ * 設定リセット
+ */
+ function 設定リセット() {
+ const ui = SpreadsheetApp.getUi();
+ const result = ui.alert(
+ '🗑️ 設定リセット',
+ '本当にすべての設定をリセットしますか?\n(この操作は取り消せません)',
+ ui.ButtonSet.YES_NO
+ );
+
+ if (result === ui.Button.YES) {
+ // スクリプトプロパティをクリア
+ PropertiesService.getScriptProperties().deleteProperty('DISCORD_WEBHOOK_URL');
+
+ // スプレッドシートの値をクリア
+ const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
+ sheet.getRange('B1').clearContent();
+ sheet.getRange('B3').clearContent();
+
+ ui.alert('✅ 完了', '設定をリセットしました', ui.ButtonSet.OK);
+ }
+ }
+
+ /**
+ * アンケート項目をMarkdownファイルとして出力
+ */
+ function アンケート項目MD出力() {
+ const ui = SpreadsheetApp.getUi();
+
+ try {
+ // 現在の日時を取得
+ const now = new Date();
+ const timestamp = Utilities.formatDate(now, Session.getScriptTimeZone(), 'yyyy-MM-dd_HH-mm-ss');
+
+ // アンケート項目を定義
+ const surveyItems = [
+ {
+ type: 'テキスト項目',
+ title: 'Discord名(匿名可)',
+ required: false,
+ description: 'Discord上での表示名を入力してください。匿名での回答も可能です。'
+ },
+ {
+ type: '選択式(単一回答)',
+ title: 'コミュニティの満足度(5段階)',
+ required: true,
+ choices: ['1', '2', '3', '4', '5'],
+ description: '現在のコミュニティに対する満足度を5段階で評価してください。'
+ },
+ {
+ type: '選択式(単一回答)',
+ title: 'このコミュニティにいる理由',
+ required: false,
+ choices: [
+ 'さとりさんのコミュニティだから',
+ '火曜日のユニコ講座が聞きたいから',
+ 'AI講座が聞きたいから',
+ 'ディスコードで情報が取りたいから',
+ 'オフ会に参加したいから',
+ 'その他'
+ ],
+ description: 'このコミュニティに参加している主な理由を選択してください。'
+ },
+ {
+ type: '段落テキスト',
+ title: '今後期待していること',
+ required: false,
+ description: 'コミュニティに対する今後の期待や要望を自由に記入してください。'
+ }
+ ];
+
+ // Markdownコンテンツを生成
+ let mdContent = `# コミュニティ満足度アンケート項目\n\n`;
+ mdContent += `生成日時: ${Utilities.formatDate(now, Session.getScriptTimeZone(), 'yyyy年MM月dd日 HH:mm:ss')}\n\n`;
+ mdContent += `## 概要\n\n`;
+ mdContent += `このアンケートは、コミュニティの現状把握と改善のために実施されています。\n`;
+ mdContent += `回答は任意であり、匿名での参加も可能です。\n\n`;
+ mdContent += `## アンケート項目一覧\n\n`;
+
+ surveyItems.forEach((item, index) => {
+ mdContent += `### ${index + 1}. ${item.title}\n\n`;
+ mdContent += `- **項目タイプ**: ${item.type}\n`;
+ mdContent += `- **必須項目**: ${item.required ? 'はい' : 'いいえ'}\n`;
+ mdContent += `- **説明**: ${item.description}\n`;
+
+ if (item.choices) {
+ mdContent += `- **選択肢**:\n`;
+ item.choices.forEach(choice => {
+ mdContent += ` - ${choice}\n`;
+ });
+ }
+ mdContent += `\n`;
+ });
+
+ mdContent += `## 技術仕様\n\n`;
+ mdContent += `- **プラットフォーム**: Google Forms\n`;
+ mdContent += `- **データ保存**: Google Spreadsheets\n`;
+ mdContent += `- **通知システム**: Discord Webhook\n`;
+ mdContent += `- **自動化**: Google Apps Script\n\n`;
+ mdContent += `## 管理機能\n\n`;
+ mdContent += `- 初期設定の自動化\n`;
+ mdContent += `- Webhook URL の管理\n`;
+ mdContent += `- Discord接続テスト\n`;
+ mdContent += `- リアルタイム通知\n`;
+ mdContent += `- 設定状況の確認\n`;
+ mdContent += `- 設定のリセット機能\n\n`;
+ mdContent += `---\n`;
+ mdContent += `*このファイルは Google Apps Script により自動生成されました*\n`;
+
+ // Google Driveにファイルを作成
+ const fileName = `アンケート項目_${timestamp}.md`;
+ const blob = Utilities.newBlob(mdContent, 'text/plain', fileName);
+ const file = DriveApp.createFile(blob);
+
+ // ファイルのURLを取得
+ const fileUrl = file.getUrl();
+
+ ui.alert(
+ '✅ MD出力完了',
+ `アンケート項目をMarkdownファイルとして出力しました。\n\nファイル名: ${fileName}\nGoogle Drive URL: ${fileUrl}`,
+ ui.ButtonSet.OK
+ );
+
+ return { fileName, fileUrl, content: mdContent };
+
+ } catch (error) {
+ ui.alert('❌ エラー', `MD出力に失敗しました:\n${error.message}`, ui.ButtonSet.OK);
+ throw error;
+ }
+ }