質問編集履歴

1

書式の改善

2022/01/06 06:19

投稿

k_k
k_k

スコア19

test CHANGED
File without changes
test CHANGED
@@ -26,49 +26,143 @@
26
26
 
27
27
  ```GAS
28
28
 
29
- //スプレッドシートの指定の列指定の文字列が入力されたとに指定のSlackへ通知
30
-
31
- function getActiveCell() {
32
-
33
- const SS = SpreadsheetApp.getActiveSpreadsheet(); //スプレッドシート取得
34
-
35
- const Sh = SS.getActiveSheet(); // アクティブシート取得
36
-
37
- if(Sh.getName() != "errordata_gmail"){ //シート名取得 
38
-
39
- return;
40
-
41
- }
42
-
43
- const ActiveCell = Sh.getActiveCell(); //アクティブセル取得
44
-
45
-
46
-
47
- if(ActiveCell.getColumn() == 10 && ActiveCell.getValues() == "SlackID生成済み"){ //条件:J列(=10)で SlackID生成済み が入力されたとき通知
48
-
49
- const InputRow = ActiveCell.getRow(); //条件一致の行を取得
50
-
51
- //以下条件一致のセルを取得。取得したい値を変更したい場合は列数を変更。定数は適宜変更。
52
-
53
- const staffErrorEmail = Sh.getRange(ActiveCell.getRow(), 5).getValues(); //5=E列(送付エラーアドレス)
54
-
55
- const staffName = Sh.getRange(ActiveCell.getRow(), 6).getValues(); //6=F列(スタッフ名)
56
-
57
- const temphireId = Sh.getRange(ActiveCell.getRow(), 7).getValues(); //7=G列(ID)
58
-
59
- const slackId = Sh.getRange(ActiveCell.getRow(), 9).getValues(); //9=I列(SlackID)
60
-
61
-
62
-
63
- const message = slackId + '\n' + staffName + '\n' + staffErrorEmail + temphireId ;
64
-
65
- postToSlack(message);
66
-
67
-
68
-
69
-
70
-
71
- }}
29
+ //送付エラーのタイムタン/件名/本文/メールIDをスプレッドシートに
30
+
31
+
32
+
33
+ function getGmailToSS() {
34
+
35
+ const ss = SpreadsheetApp.getActiveSpreadsheet();
36
+
37
+ const sheet = ss.getSheetByName('errordata_gmail');
38
+
39
+
40
+
41
+ const searchlabel = '"送付エラー"';
42
+
43
+
44
+
45
+ const threads = GmailApp.search('label:(' + searchlabel + ') -label:処理済み');
46
+
47
+
48
+
49
+ const msgIdArray = sheet.getRange("D:D").getValues().filter(String).flat();
50
+
51
+ let lastRow = msgIdArray.length + 1;
52
+
53
+
54
+
55
+ for(const n in threads){
56
+
57
+ const thread = threads[n];
58
+
59
+ const msgs = thread.getMessages();
60
+
61
+ msgs
62
+
63
+
64
+
65
+ for(m in msgs){
66
+
67
+ const msg = msgs[m];
68
+
69
+ const id = msg.getId();
70
+
71
+
72
+
73
+ if(msgIdArray.includes(id)) continue; //IDがD列に既に存在する場合はスルー
74
+
75
+
76
+
77
+
78
+
79
+ const msgData = [[msg.getDate(),msg.getSubject(),msg.getPlainBody(),id]];
80
+
81
+ sheet.getRange(lastRow,1,1,4).setValues(msgData); //まとめて書き込み
82
+
83
+
84
+
85
+ lastRow++
86
+
87
+ }
88
+
89
+
90
+
91
+
92
+
93
+ const label = GmailApp.getUserLabelByName('処理済み');
94
+
95
+ thread.addLabel(label);
96
+
97
+ }
98
+
99
+
100
+
101
+
102
+
103
+ // スプレッドシートから必要なデータを読み取り、スラックに投稿する。
104
+
105
+ const staffErrorEmail = sh.getRange(writeRow, 5).getValue(); //5=E列(送付エラーアドレス)
106
+
107
+ const staffName = sh.getRange(writeRow, 6).getValue(); //6=F列(スタッフ名)
108
+
109
+ const temphireId = sh.getRange(writeRow, 7).getValue(); //7=G列(ID)
110
+
111
+ const slackId = sh.getRange(writeRow, 9).getValue(); //9=I列(SlackID)
112
+
113
+
114
+
115
+ const message = slackId + '\n' + staffName + '\n' + staffErrorEmail + temphireId;
116
+
117
+ postToSlack(message);
118
+
119
+
120
+
121
+ }
122
+
123
+
124
+
125
+
126
+
127
+ function postToSlack(message){
128
+
129
+ const postToMDS = "通知先のWebhooksURL"; //
130
+
131
+
132
+
133
+ const jsonData =
134
+
135
+ {
136
+
137
+ "text" : message,
138
+
139
+ };
140
+
141
+
142
+
143
+ const payload = JSON.stringify(jsonData);
144
+
145
+
146
+
147
+ const options =
148
+
149
+ {
150
+
151
+ "method" : "post",
152
+
153
+ "contentType" : "application/json",
154
+
155
+ "payload" : payload,
156
+
157
+ };
158
+
159
+
160
+
161
+ // リクエスト
162
+
163
+ UrlFetchApp.fetch(postToMDS, options);
164
+
165
+ }
72
166
 
73
167
 
74
168
 
@@ -76,58 +170,6 @@
76
170
 
77
171
 
78
172
 
79
- ```GAS
80
-
81
-
82
-
83
- function postToSlack(message){
84
-
85
-
86
-
87
- const postToMDS = "通知先のWebhooksURL";
88
-
89
-
90
-
91
- const jsonData =
92
-
93
- {
94
-
95
- "text" : message,
96
-
97
- };
98
-
99
-
100
-
101
- const payload = JSON.stringify(jsonData);
102
-
103
-
104
-
105
- const options =
106
-
107
- {
108
-
109
- "method" : "post",
110
-
111
- "contentType" : "application/json",
112
-
113
- "payload" : payload,
114
-
115
- };
116
-
117
-
118
-
119
- // リクエスト
120
-
121
- UrlFetchApp.fetch(postToMDS, options);
122
-
123
- }
124
-
125
-
126
-
127
- ```
128
-
129
-
130
-
131
173
  ### 補足情報
132
174
 
133
175