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

回答編集履歴

1

a

2019/10/28 14:40

投稿

papinianus
papinianus

スコア12705

answer CHANGED
@@ -1,26 +1,29 @@
1
1
  [過去質問](https://teratail.com/questions/219231)参照のこと。
2
2
 
3
3
  ```javascript
4
- function q219571(){
4
+ function q219571() {
5
5
  const mailQuery = 'subject:お問い合わせ';
6
6
  const threads = GmailApp.search(mailQuery);
7
- const messages = GmailApp.getMessagesForThreads(threads).reduce(function (a,c) { return a.concat(c.map(mailToArray))},[]);
7
+ const messages = GmailApp.getMessagesForThreads(threads).reduce(function (a, c) {
8
+ return a.concat(c.map(mailToArray))
9
+ }, []);
8
10
  const sheet = SpreadsheetApp.getActiveSheet();
9
11
  const r = sheet.getLastRow();
10
- sheet.getRange(r+1, 1 , messages.length, 3).setValues(messages);
12
+ sheet.getRange(r + 1, 1, messages.length, 3).setValues(messages);
11
13
 
12
14
  }
15
+
13
16
  function messageToArray(m) {
14
17
  const body = m.getPlainBody();
15
18
  return [
16
- fetchData(body, '【 お名前 】', '\r'),
19
+ fetchDataInSplit(body, '【 お名前 】'),
17
- fetchData(body, '【 メールアドレス 】', '\r'),
20
+ fetchDataInSplit(body, '【 メールアドレス 】'),
18
- fetchData(body, '【 電話番号 】', '\r'),
21
+ fetchDataInSplit(body, '【 電話番号 】'),
19
22
  ];
20
23
  }
21
-
22
- function fetchData(str, pre, suf) {
24
+ function fetchDataInSplit(str, header) {
23
- const res = str.match(new RegExp(pre + '(.*?)' + suf));
25
+ const text = str.split(/(.+?)/);
26
+ const headerIndex = text.indexOf(header);
24
- return res === null ? '' : res[1];
27
+ return text[headerIndex+1].trim();
25
28
  }
26
29
  ```