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

質問編集履歴

1

書式の改善

2022/01/06 06:19

投稿

k_k
k_k

スコア19

title CHANGED
File without changes
body CHANGED
@@ -12,37 +12,58 @@
12
12
  ### 該当のソースコード
13
13
 
14
14
  ```GAS
15
- //スプレッドシートの指定の列に指定の文字列が入力されたときに指定のSlackへ通知する
16
- function getActiveCell() {
17
- const SS = SpreadsheetApp.getActiveSpreadsheet(); //スプレッドシート取得
18
- const Sh = SS.getActiveSheet(); // アクティブシート取得
15
+ //送付エラーのタイムスタンプ/件名/本文/メールIDをスプレッドシートに吐き出す
19
- if(Sh.getName() != "errordata_gmail"){ //シート名取得 
20
- return;
21
- }
22
- const ActiveCell = Sh.getActiveCell(); //アクティブセル取得
23
16
 
17
+ function getGmailToSS() {
24
- if(ActiveCell.getColumn() == 10 && ActiveCell.getValues() == "SlackID生成済み"){ //条件:J列(=10)で SlackID生成済み が入力されたとき通知
18
+ const ss = SpreadsheetApp.getActiveSpreadsheet();
25
- const InputRow = ActiveCell.getRow(); //条件一致の行を取得
19
+ const sheet = ss.getSheetByName('errordata_gmail');
26
- //以下条件一致のセルを取得。取得したい値を変更したい場合は列数を変更。定数は適宜変更。
27
- const staffErrorEmail = Sh.getRange(ActiveCell.getRow(), 5).getValues(); //5=E列(送付エラーアドレス)
28
- const staffName = Sh.getRange(ActiveCell.getRow(), 6).getValues(); //6=F列(スタッフ名)
29
- const temphireId = Sh.getRange(ActiveCell.getRow(), 7).getValues(); //7=G列(ID)
30
- const slackId = Sh.getRange(ActiveCell.getRow(), 9).getValues(); //9=I列(SlackID)
31
20
 
32
- const message = slackId + '\n' + staffName + '\n' + staffErrorEmail + temphireId ;
21
+ const searchlabel = '"送付エラー"';
33
- postToSlack(message);
34
-
35
-
36
- }}
37
22
 
38
- ```
23
+ const threads = GmailApp.search('label:(' + searchlabel + ') -label:処理済み');
39
24
 
25
+ const msgIdArray = sheet.getRange("D:D").getValues().filter(String).flat();
26
+ let lastRow = msgIdArray.length + 1;
27
+
28
+ for(const n in threads){
29
+ const thread = threads[n];
30
+ const msgs = thread.getMessages();
40
- ```GAS
31
+ msgs
41
32
 
33
+ for(m in msgs){
34
+ const msg = msgs[m];
35
+ const id = msg.getId();
36
+
37
+ if(msgIdArray.includes(id)) continue; //IDがD列に既に存在する場合はスルー
38
+
39
+
40
+ const msgData = [[msg.getDate(),msg.getSubject(),msg.getPlainBody(),id]];
41
+ sheet.getRange(lastRow,1,1,4).setValues(msgData); //まとめて書き込み
42
+
43
+ lastRow++
44
+ }
45
+
46
+
47
+ const label = GmailApp.getUserLabelByName('処理済み');
48
+ thread.addLabel(label);
49
+ }
50
+
51
+
52
+ // スプレッドシートから必要なデータを読み取り、スラックに投稿する。
53
+ const staffErrorEmail = sh.getRange(writeRow, 5).getValue(); //5=E列(送付エラーアドレス)
54
+ const staffName = sh.getRange(writeRow, 6).getValue(); //6=F列(スタッフ名)
55
+ const temphireId = sh.getRange(writeRow, 7).getValue(); //7=G列(ID)
56
+ const slackId = sh.getRange(writeRow, 9).getValue(); //9=I列(SlackID)
57
+
58
+ const message = slackId + '\n' + staffName + '\n' + staffErrorEmail + temphireId;
59
+ postToSlack(message);
60
+
61
+ }
62
+
63
+
42
64
  function postToSlack(message){
65
+ const postToMDS = "通知先のWebhooksURL"; //
43
66
 
44
- const postToMDS = "通知先のWebhooksURL";
45
-
46
67
  const jsonData =
47
68
  {
48
69
  "text" : message,