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

回答編集履歴

2

a

2019/10/27 14:01

投稿

papinianus
papinianus

スコア12705

answer CHANGED
@@ -15,8 +15,8 @@
15
15
  const query = 'label:(00_hp問合せ-10-23資料dw)';
16
16
  const threads = GmailApp.search(query, 0, 30);
17
17
  const messages = GmailApp.getMessagesForThreads(threads)
18
- .map(function(mth){ return [ mth.map(messageToArray)];});
18
+ .reduce(function(a,c){ return a.concat(c.map(messageToArray));},[]);
19
- shee.getRange(sheet.getLastRow() + 1, 1, messages.length, 12).setValues(messages);
19
+ sheet.getRange(sheet.getLastRow() + 1, 1, messages.length, 12).setValues(messages);
20
20
  }
21
21
 
22
22
  function messageToArray(m) {

1

a

2019/10/27 14:01

投稿

papinianus
papinianus

スコア12705

answer CHANGED
@@ -6,4 +6,39 @@
6
6
  [GAS (Google Apps Script)で数値を文字としてsetValueする方法についてあれこれ模索した結果](https://memorandums.hatenablog.com/entry/2017/04/21/135927)
7
7
  マウス操作で考えると、appendRowは範囲を指定して右クリック→挿入みたいな動作なのだと推測します。
8
8
 
9
- lastRowを得ているのですから(ここで得るのはどうかと思いますが)、`Range.setValues`で書き込めば書式は維持されると想像します。
9
+ lastRowを得ているのですから(ここで得るのはどうかと思いますが)、`Range.setValues`で書き込めば書式は維持されると想像します。
10
+
11
+ ```javascript
12
+ function myFunction() {
13
+ const sheet = SpreadsheetApp.getActiveSheet();
14
+
15
+ const query = 'label:(00_hp問合せ-10-23資料dw)';
16
+ const threads = GmailApp.search(query, 0, 30);
17
+ const messages = GmailApp.getMessagesForThreads(threads)
18
+ .map(function(mth){ return [ mth.map(messageToArray)];});
19
+ shee.getRange(sheet.getLastRow() + 1, 1, messages.length, 12).setValues(messages);
20
+ }
21
+
22
+ function messageToArray(m) {
23
+ const body = m.getPlainBody();
24
+ return [
25
+ m.getDate(),
26
+ m.getFrom(),
27
+ fetchData(body, '会社名:', '\r'),
28
+ fetchData(body, 'お名前(姓):', '\r'),
29
+ fetchData(body, 'お名前(名):', '\r'),
30
+ fetchData(body, '電話番号:', '\r'),
31
+ fetchData(body, 'メールアドレス:', '\r'),
32
+ fetchData(body, '携帯電話:', '\r'),
33
+ fetchData(body, '登録者:', '\r'),
34
+ fetchData(body, '顧客主担当:', '\r'),
35
+ fetchData(body, '担当エリア:', '\r'),
36
+ fetchData(body, '資料:', '\r'),
37
+ ];
38
+ }
39
+
40
+ function fetchData(str, pre, suf) {
41
+ const res = str.match(new RegExp(pre + '(.*?)' + suf));
42
+ return res === null ? '' : res[1];
43
+ }
44
+ ```