m1m02530125 commited on
Commit
ca861f0
·
1 Parent(s): 40fc7c0

feat: Google フォームの UX 向上のため、出勤可否に応じたセクション分岐付きフォームの自動生成機能を実装

Browse files
Files changed (2) hide show
  1. utils/forms_csv_parser.py +28 -8
  2. utils/google_forms_api.py +119 -17
utils/forms_csv_parser.py CHANGED
@@ -279,30 +279,50 @@ def _process_new_fmt(
279
  result: "ImportResult",
280
  name: str,
281
  ) -> "Optional[ShiftRequest]":
282
- """新形式(GASフォーム)の1日分を処理して ShiftRequest を返す。"""
283
  avail_val = row.get(cols["avail"], "").strip()
284
  if not avail_val:
285
  return None # 未回答 = スキップ
286
 
287
- if avail_val == "休み":
288
  return ShiftRequest(
289
  employee_id=emp.id, date=date_str,
290
  pattern_id=None, custom_start=None, custom_end=None, note=note,
291
  )
292
 
 
 
 
 
293
  if "ダブル" in avail_val:
294
  return ShiftRequest(
295
  employee_id=emp.id, date=date_str,
296
  pattern_id="double", custom_start=None, custom_end=None, note=note,
297
  )
 
 
 
 
298
 
299
- # 朝食のみ or ディナーのみ → 対応する時間列を読む
300
- if "朝食" in avail_val:
301
- time_val = row.get(cols.get("b", ""), "").strip()
302
- else:
303
- time_val = row.get(cols.get("d", ""), "").strip()
304
 
305
- return _resolve_time_value(time_val, date_str, custom_times, note, emp, result, name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
 
307
 
308
  def _process_direct_fmt(
 
279
  result: "ImportResult",
280
  name: str,
281
  ) -> "Optional[ShiftRequest]":
282
+ """新形式(GAS/API分岐フォーム)の1日分を処理して ShiftRequest を返す。"""
283
  avail_val = row.get(cols["avail"], "").strip()
284
  if not avail_val:
285
  return None # 未回答 = スキップ
286
 
287
+ if avail_val in ("いいえ(休み)", "いいえ", "休み"):
288
  return ShiftRequest(
289
  employee_id=emp.id, date=date_str,
290
  pattern_id=None, custom_start=None, custom_end=None, note=note,
291
  )
292
 
293
+ b_val = row.get(cols.get("b", ""), "").strip() if "b" in cols else ""
294
+ d_val = row.get(cols.get("d", ""), "").strip() if "d" in cols else ""
295
+
296
+ # 以前の「朝食のみ」「ディナーのみ」「ダブル」の選択肢だった場合の互換処理
297
  if "ダブル" in avail_val:
298
  return ShiftRequest(
299
  employee_id=emp.id, date=date_str,
300
  pattern_id="double", custom_start=None, custom_end=None, note=note,
301
  )
302
+ elif "朝食" in avail_val and "ディナー" not in avail_val:
303
+ return _resolve_time_value(b_val, date_str, custom_times, note, emp, result, name)
304
+ elif "ディナー" in avail_val and "朝食" not in avail_val:
305
+ return _resolve_time_value(d_val, date_str, custom_times, note, emp, result, name)
306
 
307
+ # 新形式(はい ➔ 朝食・ディナー両方をチェック)
308
+ has_b = bool(b_val and b_val != "休み(出勤不可)" and "休み" not in b_val and "休み" not in b_val)
309
+ has_d = bool(d_val and d_val != "休み(出勤不可)" and "休み" not in d_val and "休み" not in d_val)
 
 
310
 
311
+ if has_b and has_d:
312
+ return ShiftRequest(
313
+ employee_id=emp.id, date=date_str,
314
+ pattern_id="double", custom_start=None, custom_end=None, note=note,
315
+ )
316
+ elif has_b:
317
+ return _resolve_time_value(b_val, date_str, custom_times, note, emp, result, name)
318
+ elif has_d:
319
+ return _resolve_time_value(d_val, date_str, custom_times, note, emp, result, name)
320
+ else:
321
+ # 時間帯に有効な値がない、または「休み」が選ばれている場合
322
+ return ShiftRequest(
323
+ employee_id=emp.id, date=date_str,
324
+ pattern_id=None, custom_start=None, custom_end=None, note=note,
325
+ )
326
 
327
 
328
  def _process_direct_fmt(
utils/google_forms_api.py CHANGED
@@ -1,3 +1,4 @@
 
1
  """Google Forms / Drive API Integration Helper"""
2
  import os
3
  import json
@@ -54,6 +55,7 @@ def create_google_form(
54
  ) -> tuple[str, str]:
55
  """
56
  指定期間用の Google フォームを自動作成し、(form_id, form_url) を返す。
 
57
  """
58
  creds = get_credentials_from_json(credentials_json_str)
59
  forms_service = build("forms", "v1", credentials=creds)
@@ -62,9 +64,8 @@ def create_google_form(
62
  description = (
63
  "【注意事項】\n"
64
  "・お名前をリストから正確に選んでください。\n"
65
- "・日付ごとに「出勤可否」および「希望シフト時間」を入力してください。\n"
66
- "・ダブルは朝食・ディナー両方フル出勤(時間帯は固定)を表します。\n"
67
- "・リストにない時間帯は「その他」を選択して、最後の備考欄に記入してください。"
68
  )
69
 
70
  # 1. フォームを作成
@@ -78,7 +79,7 @@ def create_google_form(
78
  form_id = form["formId"]
79
  responder_url = form["responderUri"]
80
 
81
- # 2. 設問バッチ作成リクエストを構築
82
  requests = []
83
 
84
  # 説明文の設定
@@ -118,9 +119,9 @@ def create_google_form(
118
  holidays = holiday_set(dates)
119
  dow_labels = ["月", "火", "水", "木", "金", "土", "日"]
120
 
121
- # 朝食/ディナーのシフトパターン選択肢
122
- b_options = []
123
- d_options = []
124
  for p in ALL_PATTERNS:
125
  if p.id not in ("custom", "double") and p.start and p.end:
126
  label = p.label
@@ -141,21 +142,31 @@ def create_google_form(
141
  if is_h:
142
  date_label += "【祝】"
143
 
144
- # A. 出勤可否 (ラジオボタン)
 
 
 
 
 
 
 
 
 
 
 
 
145
  requests.append({
146
  "createItem": {
147
  "item": {
148
  "title": f"{date_label}は出勤できますか?",
149
  "questionItem": {
150
  "question": {
151
- "required": False,
152
  "choiceQuestion": {
153
  "type": "RADIO",
154
  "options": [
155
- {"value": "朝食のみ"},
156
- {"value": "ディナーのみ"},
157
- {"value": "ダブル(朝食+ディナー両方)"},
158
- {"value": "休み"}
159
  ]
160
  }
161
  }
@@ -166,7 +177,19 @@ def create_google_form(
166
  })
167
  idx += 1
168
 
169
- # B. 朝食希望 (プルダウン)
 
 
 
 
 
 
 
 
 
 
 
 
170
  requests.append({
171
  "createItem": {
172
  "item": {
@@ -186,7 +209,7 @@ def create_google_form(
186
  })
187
  idx += 1
188
 
189
- # C. ディナー希望 (プルダウン)
190
  requests.append({
191
  "createItem": {
192
  "item": {
@@ -206,7 +229,19 @@ def create_google_form(
206
  })
207
  idx += 1
208
 
209
- # QLast: 備考 (長文記述式)
 
 
 
 
 
 
 
 
 
 
 
 
210
  requests.append({
211
  "createItem": {
212
  "item": {
@@ -225,12 +260,79 @@ def create_google_form(
225
  }
226
  })
227
 
228
- # batchUpdate を実行してフォームに設問を追加
229
  forms_service.forms().batchUpdate(
230
  formId=form_id,
231
  body={"requests": requests}
232
  ).execute()
233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
  return form_id, responder_url
235
 
236
 
 
1
+ # -*- coding: utf-8 -*-
2
  """Google Forms / Drive API Integration Helper"""
3
  import os
4
  import json
 
55
  ) -> tuple[str, str]:
56
  """
57
  指定期間用の Google フォームを自動作成し、(form_id, form_url) を返す。
58
+ ユーザー体験(UX)向上のため、「出勤可否 (はい/いいえ)」によるセクション分岐付きの構成で生成します。
59
  """
60
  creds = get_credentials_from_json(credentials_json_str)
61
  forms_service = build("forms", "v1", credentials=creds)
 
64
  description = (
65
  "【注意事項】\n"
66
  "・お名前をリストから正確に選んでください。\n"
67
+ "・日付ごとに「出勤可否」を確認します。出勤可能な日のみ希望シフト時間が表示されます。\n"
68
+ "・「その他」を選んだ場合は、最後の備考欄に希望する時間帯を記入してください。"
 
69
  )
70
 
71
  # 1. フォームを作成
 
79
  form_id = form["formId"]
80
  responder_url = form["responderUri"]
81
 
82
+ # 2. 設問バッチ作成リクエストを構築 (ステップ1: すべてのアイテムを作成)
83
  requests = []
84
 
85
  # 説明文の設定
 
119
  holidays = holiday_set(dates)
120
  dow_labels = ["月", "火", "水", "木", "金", "土", "日"]
121
 
122
+ # 朝食/ディナーのシフトパターン選択肢 (先頭に「休み」を追加)
123
+ b_options = [{"value": "休み"}]
124
+ d_options = [{"value": "休み"}]
125
  for p in ALL_PATTERNS:
126
  if p.id not in ("custom", "double") and p.start and p.end:
127
  label = p.label
 
142
  if is_h:
143
  date_label += "【祝】"
144
 
145
+ # A. 出勤希望確認セクション (pageBreakItem)
146
+ requests.append({
147
+ "createItem": {
148
+ "item": {
149
+ "title": f"{date_label} 出勤希望",
150
+ "pageBreakItem": {}
151
+ },
152
+ "location": {"index": idx}
153
+ }
154
+ })
155
+ idx += 1
156
+
157
+ # B. 出勤可否質問 (ラジオボタン)
158
  requests.append({
159
  "createItem": {
160
  "item": {
161
  "title": f"{date_label}は出勤できますか?",
162
  "questionItem": {
163
  "question": {
164
+ "required": True,
165
  "choiceQuestion": {
166
  "type": "RADIO",
167
  "options": [
168
+ {"value": "はい(出勤可能)"},
169
+ {"value": "いいえ(休み)"}
 
 
170
  ]
171
  }
172
  }
 
177
  })
178
  idx += 1
179
 
180
+ # C. 希望時間帯選択セクション (pageBreakItem)
181
+ requests.append({
182
+ "createItem": {
183
+ "item": {
184
+ "title": f"{date_label} 希望時間帯選択",
185
+ "pageBreakItem": {}
186
+ },
187
+ "location": {"index": idx}
188
+ }
189
+ })
190
+ idx += 1
191
+
192
+ # D. 朝食希望 (プルダウン)
193
  requests.append({
194
  "createItem": {
195
  "item": {
 
209
  })
210
  idx += 1
211
 
212
+ # E. ディナー希望 (プルダウン)
213
  requests.append({
214
  "createItem": {
215
  "item": {
 
229
  })
230
  idx += 1
231
 
232
+ # QLast: 備考セクション (pageBreakItem)
233
+ requests.append({
234
+ "createItem": {
235
+ "item": {
236
+ "title": "その他連絡事項",
237
+ "pageBreakItem": {}
238
+ },
239
+ "location": {"index": idx}
240
+ }
241
+ })
242
+ idx += 1
243
+
244
+ # QLast: 備考質問 (長文記述式)
245
  requests.append({
246
  "createItem": {
247
  "item": {
 
260
  }
261
  })
262
 
263
+ # batchUpdate を実行して設問を追加
264
  forms_service.forms().batchUpdate(
265
  formId=form_id,
266
  body={"requests": requests}
267
  ).execute()
268
 
269
+ # 3. 遷移先IDを設定する (ステップ2: 作成されたアイテムを取得してIDを特定)
270
+ form_info = forms_service.forms().get(formId=form_id).execute()
271
+ items = form_info.get("items", [])
272
+
273
+ date_avail_q_ids = {}
274
+ date_avail_section_ids = {}
275
+ note_section_id = None
276
+
277
+ for item in items:
278
+ title = item.get("title", "")
279
+
280
+ if title == "その他連絡事項" and "pageBreakItem" in item:
281
+ note_section_id = item["itemId"]
282
+ continue
283
+
284
+ for d in dates:
285
+ ds = d.isoformat()
286
+ dow = dow_labels[d.weekday()]
287
+ is_h = ds in holidays
288
+ date_label = f"{d.month}月{d.day}日({dow})"
289
+ if is_h:
290
+ date_label += "【祝】"
291
+
292
+ if title == f"{date_label} 出勤希望" and "pageBreakItem" in item:
293
+ date_avail_section_ids[ds] = item["itemId"]
294
+ elif title == f"{date_label}は出勤できますか?" and "questionItem" in item:
295
+ date_avail_q_ids[ds] = item["questionItem"]["question"]["questionId"]
296
+
297
+ # 4. 2回目の batchUpdate で「いいえ(休み)」の遷移先 (goToSectionId) を設定
298
+ update_requests = []
299
+ for i, d in enumerate(dates):
300
+ ds = d.isoformat()
301
+ q_id = date_avail_q_ids.get(ds)
302
+ if not q_id:
303
+ continue
304
+
305
+ if i < len(dates) - 1:
306
+ next_ds = dates[i+1].isoformat()
307
+ next_section_id = date_avail_section_ids.get(next_ds)
308
+ else:
309
+ next_section_id = note_section_id
310
+
311
+ if not next_section_id:
312
+ continue
313
+
314
+ update_requests.append({
315
+ "updateQuestion": {
316
+ "question": {
317
+ "questionId": q_id,
318
+ "choiceQuestion": {
319
+ "type": "RADIO",
320
+ "options": [
321
+ {"value": "はい(出勤可能)"},
322
+ {"value": "いいえ(休み)", "goToSectionId": next_section_id}
323
+ ]
324
+ }
325
+ },
326
+ "updateMask": "choiceQuestion.options"
327
+ }
328
+ })
329
+
330
+ if update_requests:
331
+ forms_service.forms().batchUpdate(
332
+ formId=form_id,
333
+ body={"requests": update_requests}
334
+ ).execute()
335
+
336
  return form_id, responder_url
337
 
338