質問するログイン新規登録

質問編集履歴

3

スプレッドシートのシート名の更新

2025/07/10 13:25

投稿

hara_0129
hara_0129

スコア0

title CHANGED
File without changes
body CHANGED
@@ -4,8 +4,15 @@
4
4
  ### 発生している問題・分からないこと
5
5
  Google Apps Scriptで実装してみてもうまくいきません.
6
6
  ネットで検索して参考になりそうなもので試してみても, onFormSubmit関数が実行されてもエラーとなります.
7
+
8
+ シート名を"立替申請"に訂正すると, スプレッドシートの通し番号は振られました
9
+
10
+ スプレッドシートのGASに全て入力していました.
11
+ フォーム・スプレッドシートにそれぞれどのようにGASを記載すれば良いのでしょうか?
12
+
7
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2025-07-09/b77ed033-6530-43e2-8817-a12af26a278b.png)
13
+ 2025/07/10 22:16:19 エラー TypeError: Cannot read properties of undefined (reading 'getEditResponseUrl')
8
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2025-07-09/4916b87a-0811-4940-8383-a617eddcedbd.png)
14
+ at onFormSubmit(コード:29:30)
15
+
9
16
  ### 該当のソースコード
10
17
 
11
18
  ```

2

コードの変更・エラー画面のキャプチャ追加

2025/07/09 12:45

投稿

hara_0129
hara_0129

スコア0

title CHANGED
File without changes
body CHANGED
@@ -3,88 +3,53 @@
3
3
 
4
4
  ### 発生している問題・分からないこと
5
5
  Google Apps Scriptで実装してみてもうまくいきません.
6
- ネットで検索して参考になりそうなもので実行してみても, onFormSubmit関数がうまく実行されません.
6
+ ネットで検索して参考になりそうなものでしてみても, onFormSubmit関数が実行されてもエラーとなり.
7
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2025-07-09/b77ed033-6530-43e2-8817-a12af26a278b.png)
8
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2025-07-09/4916b87a-0811-4940-8383-a617eddcedbd.png)
9
+ ### 該当のソースコード
7
10
 
11
+ ```
12
+ const SHEET_NAME_CLAIM = "立替申請";
13
+ const ACCOUNTING_EMAIL = "******"; // 会計担当
8
14
 
15
+ // 年度(4月始まり)
16
+ function getFiscalYear(date) {
17
+ const y = date.getFullYear();
18
+ const m = date.getMonth() + 1;
19
+ return m >= 4 ? y : y - 1;
20
+ }
9
21
 
10
-
11
- ### 該当のソースコード
12
-
13
- ```
14
22
  function onFormSubmit(e) {
15
23
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("立替申請");
16
- const row = e.range.getRow();
24
+ const lastRow = sheet.getLastRow();
17
- const year = new Date().getFullYear().toString();
18
25
 
19
- // 通し番号生成
26
+ // 通し番号生成(I列 = 9)
27
+ const fiscalYear = getFiscalYear(new Date());
28
+ const serialCol = 9;
20
- const values = sheet.getRange(2, 9, sheet.getLastRow() - 1).getValues(); // I列: 通し番号
29
+ const serialData = sheet.getRange(2, serialCol, lastRow - 1).getValues().flat();
21
- let max = 0;
22
- values.forEach(([id]) => {
23
- if (id && id.toString().startsWith(`${year}-`)) {
30
+ const count = serialData.filter(v => typeof v === 'string' && v.startsWith(`${fiscalYear}-`)).length + 1;
24
- const num = parseInt(id.split("-")[1]);
25
- if (!isNaN(num)) max = Math.max(max, num);
26
- }
27
- });
28
- const currentId = `${year}-${String(max + 1).padStart(4, '0')}`;
31
+ const serialNumber = `${fiscalYear}-${String(count).padStart(4, '0')}`;
29
- sheet.getRange(row, 9).setValue(currentId); // I列に通し番号を記入
32
+ sheet.getRange(lastRow, serialCol).setValue(serialNumber);
30
33
 
31
- // 編集用リンク取得
34
+ // メール送信内容
35
+ const row = sheet.getRange(lastRow, 1, 1, sheet.getLastColumn()).getValues()[0];
36
+ const name = row[3]; // D列
37
+ const description = row[6]; // G列
38
+ const amount = row[7]; // H列
39
+ const email = row[2]; // C列
32
- const responseUrl = e.response.getEditResponseUrl();
40
+ const editUrl = e.response.getEditResponseUrl();
33
41
 
34
- // フォーム確認メッセージ更新(次回申請者向け)
42
+ // 本人への通知
43
+ const body = `${name} 様\n\n「${description}」について\n${amount}円で申請が行われました。\n\n通し番号:${serialNumber}\n訂正はこちら:\n${editUrl}\n\n受領報告フォーム:\nhttps://〜`;
35
- const form = FormApp.openById("**************************");
44
+ GmailApp.sendEmail(email, `【申請受付】${serialNumber}`, body);
36
- const nextId = `${year}-${String(max + 2).padStart(4, '0')}`;
37
- const nextFormUrl = "https://docs.google.com/forms/d/*************************/viewform";
38
45
 
39
- form.setConfirmationMessage(
40
- `✅ 申請完了!\n` +
41
- `通し番号:${currentId}\n\n` +
46
+ // 会計担当へ通知
42
- `🔗 [編集リンクはこちら](${responseUrl})\n` +
43
- `📄 [次の申請フォームへ](${nextFormUrl})\n\n` +
47
+ const accountingEmail = "***********";
48
+ const accBody = `${name} 様より\n通し番号:${serialNumber}\n「${description}」\n${amount}円の申請がありました。`;
44
- `※し番号は控えておき、問い合わせ時にご利用ください。`
49
+ GmailApp.sendEmail(accountingEmail, `【会計知】${serialNumber}`, accBody);
45
- );
50
+ }
46
51
 
47
- const formData = e.namedValues;
48
- const accountingEmail = "accounting@example.com"; // 会計担当者メール(変更してください)
49
- const applicantEmail = formData["メールアドレス"][0]; // 申請者メール
50
52
 
51
- // 会計担当へ通知メール
52
- const accountingBody = `
53
- 【新しい立替申請が届きました】
54
-
55
- 通し番号:${currentId}
56
- 名前:${formData["名前"]}
57
- 支払日:${formData["支払日"]}
58
- 支出項目:${formData["支出項目"]}
59
- 内容:${formData["内容"]}
60
- 金額:${formData["金額"]}
61
- レシート等:${formData["レシート等"]}
62
-
63
- 編集用リンク:
64
- ${responseUrl}
65
- `;
66
-
67
- GmailApp.sendEmail(accountingEmail, `立替申請 ${currentId}(${formData["名前"]})`, accountingBody);
68
-
69
- // 申請者本人へ編集リンクメール送信
70
- const applicantBody = `
71
- ${formData["名前"]} 様
72
-
73
- 立替申請ありがとうございます。
74
-
75
- 通し番号:${currentId}
76
-
77
- 以下のリンクから、申請内容の編集が可能です:
78
- ${responseUrl}
79
-
80
- ※ 通し番号は控えておいてください。
81
-
82
- よろしくお願いいたします。
83
- `;
84
-
85
- GmailApp.sendEmail(applicantEmail, `【立替申請受付】通し番号:${currentId}`, applicantBody);
86
- }
87
-
88
53
  ```
89
54
 
90
55
  ### 試したこと・調べたこと

1

誤字

2025/07/07 06:20

投稿

hara_0129
hara_0129

スコア0

title CHANGED
File without changes
body CHANGED
@@ -32,7 +32,7 @@
32
32
  const responseUrl = e.response.getEditResponseUrl();
33
33
 
34
34
  // フォーム確認メッセージ更新(次回申請者向け)
35
- const form = FormApp.openById("1cAc6c3vhxylS8BylANXw7zXuTUcd_RgHwrqrlP8HHcU");
35
+ const form = FormApp.openById("**************************");
36
36
  const nextId = `${year}-${String(max + 2).padStart(4, '0')}`;
37
37
  const nextFormUrl = "https://docs.google.com/forms/d/*************************/viewform";
38
38