回答編集履歴
5
メール送信ではなく、下書きをつくるようにしました
test
CHANGED
@@ -34,7 +34,8 @@
|
|
34
34
|
htmlBody: html,
|
35
35
|
attachments: [tableImage, tablePDF],
|
36
36
|
};
|
37
|
+
// GmailApp.sendEmail(recipient, subject, body, options);
|
37
|
-
GmailApp.
|
38
|
+
GmailApp.createDraft(recipient, subject, body, options);
|
38
39
|
}
|
39
40
|
// ---- BCCでの送信の例ここまで
|
40
41
|
});
|
4
メールの例を画像で加えました
test
CHANGED
@@ -128,4 +128,4 @@
|
|
128
128
|
return html;
|
129
129
|
}
|
130
130
|
```
|
131
|
-
|
131
|
+

|
3
いろんな方法の例にしてみました
test
CHANGED
@@ -1,30 +1,28 @@
|
|
1
1
|
ご質問のBCCと表のメール送信のコードの例を参考まで。
|
2
|
-
> メールの内容は表を貼り付けするので、書式が保たれるようにしたいです。
|
3
|
-
|
4
|
-
表をメールに貼り付ける(?)書式を保つ(?)ようにはなっていません。
|
5
|
-
|
6
2
|
|
7
3
|
```JavaScript
|
8
4
|
function createMailDraft() {
|
9
5
|
// 初期設定
|
10
|
-
const sheetName = 'シート1';
|
6
|
+
const sheetName = 'シート1'; // アドレス、件名等のシート
|
11
7
|
const firstColumn = 'A'; // データの開始列
|
12
8
|
const lastColumn = 'D'; // データの終了列
|
13
9
|
const firstRow = 2; // データの開始行(1行目は見出し)
|
14
|
-
const recipient = 'xxxxxxxxxx@xxxx
|
10
|
+
const recipient = 'xxxxxxxxxx@xxxxx.com'; // メールの送信先
|
15
|
-
|
16
|
-
//
|
11
|
+
const tableSheetName = 'シート2'; // 送信する表のシート
|
17
|
-
const table = [
|
18
|
-
|
12
|
+
const tableRange = 'A1:F30'; // 表のセル範囲
|
19
|
-
['項目1', '項目2', '項目3',],
|
20
|
-
['内容1', '内容2', '内容3',],
|
21
|
-
];
|
22
13
|
|
23
14
|
const ss = SpreadsheetApp.getActiveSpreadsheet();
|
24
15
|
const sheet = ss.getSheetByName(sheetName);
|
16
|
+
const tableSheet = ss.getSheetByName(tableSheetName);
|
25
17
|
const values = sheet
|
26
18
|
.getRange(firstColumn + firstRow + ':' + lastColumn + sheet.getLastRow())
|
27
19
|
.getValues();
|
20
|
+
// メールに添付する表の要素の例(実際は、シートから配列として取得?)
|
21
|
+
const range = tableSheet.getRange(tableRange);
|
22
|
+
const table = range.getDisplayValues();
|
23
|
+
const tableImage = makeTableImage(table);
|
24
|
+
const tablePDF = makePDF(range);
|
25
|
+
|
28
26
|
values.forEach(v => {
|
29
27
|
const [check, address, subject] = v;
|
30
28
|
const body = makePlainBody(table);
|
@@ -34,6 +32,7 @@
|
|
34
32
|
const options = {
|
35
33
|
bcc: address,
|
36
34
|
htmlBody: html,
|
35
|
+
attachments: [tableImage, tablePDF],
|
37
36
|
};
|
38
37
|
GmailApp.sendEmail(recipient, subject, body, options);
|
39
38
|
}
|
@@ -41,51 +40,62 @@
|
|
41
40
|
});
|
42
41
|
}
|
43
42
|
|
43
|
+
// 表のPDFファイル作成の例
|
44
|
+
function makePDF(range) {
|
45
|
+
const pdfRange = range.getA1Notation().replace(':', '%3A');
|
46
|
+
const sheet = range.getSheet();
|
47
|
+
const sheetId = sheet.getSheetId();
|
48
|
+
const ssId = sheet.getParent().getId();
|
49
|
+
const pdfOptions = {
|
50
|
+
exportFormat: 'pdf',
|
51
|
+
format: 'pdf',
|
52
|
+
size: 'A4', // 出力サイズ
|
53
|
+
portrait: 'true', // PDFの向き true:縦 false:横
|
54
|
+
fitw: 'true', // ページフィット true:フィット false:原寸大
|
55
|
+
scale: '2', // スケール 1:標準100% 2:幅に合わせる 3:高さに合わせる 4:ページに合わせる
|
56
|
+
top_margin: '0.100', // 上余白(単位:cm)
|
57
|
+
bottom_margin: '0.100', // 下余白
|
58
|
+
left_margin: '0.100', // 左余白
|
59
|
+
right_margin: '0.100', // 右余白
|
60
|
+
sheetnames: 'false', // シート名 true:有 false:無
|
61
|
+
printtitle: 'false', // タイトル true:有 false:無
|
62
|
+
pagenum: 'RIGHT', // ページ番号位置
|
63
|
+
gridlines: 'false', // グリッドライン true:有 false:無
|
64
|
+
fzr: 'true', // 行見出し true:含める false:含めない
|
65
|
+
fzc: 'true', // 列見出し true:含める false:含めない
|
66
|
+
range: pdfRange, // 選択範囲(A1形式で':'を’%3A'で置換してある)
|
67
|
+
gid: sheetId // シートID
|
68
|
+
};
|
69
|
+
//各出力オプションを'&'で接続してURL用のオプション文字列を作成
|
70
|
+
const options = Object.entries(pdfOptions).map(v => v[0] + '=' + v[1]).join('&');
|
71
|
+
//PDFファイルを作成
|
72
|
+
const pdfUrl = 'https://docs.google.com/spreadsheets/d/' + ssId + '/export?' + options;
|
73
|
+
const authParams = { headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() }, muteHttpExceptions: true };
|
74
|
+
const pdf = UrlFetchApp.fetch(pdfUrl, authParams).getBlob();
|
75
|
+
|
76
|
+
return pdf;
|
77
|
+
}
|
78
|
+
|
79
|
+
// 表の添付ファイル作成の例
|
80
|
+
function makeTableImage(table) {
|
81
|
+
const tbl = table.slice();
|
82
|
+
const titles = tbl.shift();
|
83
|
+
const dataTable = Charts.newDataTable();
|
84
|
+
titles.forEach(v => dataTable.addColumn(Charts.ColumnType.STRING, v));
|
85
|
+
tbl.forEach(v => dataTable.addRow(v));
|
86
|
+
const tableImage = Charts.newTableChart()
|
87
|
+
.setDataTable(dataTable.build())
|
88
|
+
.setDimensions(table[0].length * 100, (table.length) * 21)
|
89
|
+
.build()
|
90
|
+
.getBlob();
|
91
|
+
|
92
|
+
return tableImage;
|
93
|
+
}
|
94
|
+
|
95
|
+
|
44
96
|
// テキスト形式のメール本文作成の例
|
45
97
|
function makePlainBody(table) {
|
46
|
-
const body = `
|
47
|
-
|${table[0][0]}|${table[0][1]}|${table[0][2]}|
|
48
|
-
|${table[1][0]}|${table[1][1]}|${table[1][2]}|
|
49
|
-
|${table[2][0]}|${table[2][1]}|${table[2][2]}|
|
50
|
-
`;
|
51
|
-
|
52
|
-
return body;
|
53
|
-
}
|
54
|
-
|
55
|
-
// HTML形式のメール本文作成の例
|
56
|
-
function makeHtmlBody(table) {
|
57
|
-
// 表に関する説明がないので単純なサンプル
|
58
|
-
const html = `
|
59
|
-
<body>
|
60
|
-
<table border = "2" style="border-collapse: collapse">
|
61
|
-
<tr>
|
62
|
-
<th>${table[0][0]}</th>
|
63
|
-
<th>${table[0][1]}</th>
|
64
|
-
<th>${table[0][2]}</th>
|
65
|
-
</tr>
|
66
|
-
<tr>
|
67
|
-
<th>${table[1][0]}</th>
|
68
|
-
<th>${table[1][1]}</th>
|
69
|
-
<th>${table[1][2]}</th>
|
70
|
-
</tr>
|
71
|
-
<tr>
|
72
|
-
<th>${table[2][0]}</th>
|
73
|
-
<th>${table[2][1]}</th>
|
74
|
-
<th>${table[2][2]}</th>
|
75
|
-
</tr>
|
76
|
-
</table>
|
77
|
-
</body>
|
78
|
-
`;
|
79
|
-
|
80
|
-
return html;
|
81
|
-
}
|
82
|
-
```
|
83
|
-
|
84
|
-
繰り返し処理を利用して表を作成する例
|
85
|
-
```JavaScript
|
86
|
-
// テキスト形式のメール本文作成の例
|
87
|
-
function makePlainBody(table) {
|
88
|
-
let body = '';
|
98
|
+
let body = '表の例です\r\n';
|
89
99
|
table.forEach(tr => {
|
90
100
|
tr.forEach(th => {
|
91
101
|
body += '|';
|
@@ -99,7 +109,9 @@
|
|
99
109
|
|
100
110
|
// HTML形式のメール本文作成の例
|
101
111
|
function makeHtmlBody(table) {
|
112
|
+
// 表に関する説明がないので単純なサンプル
|
102
113
|
let html = '<body>';
|
114
|
+
html += '<p>表の例です</p>';
|
103
115
|
html += '<table border = "2" style="border-collapse: collapse">';
|
104
116
|
table.forEach(tr => {
|
105
117
|
html += '<tr>';
|
2
test
CHANGED
@@ -1,4 +1,8 @@
|
|
1
1
|
ご質問のBCCと表のメール送信のコードの例を参考まで。
|
2
|
+
> メールの内容は表を貼り付けするので、書式が保たれるようにしたいです。
|
3
|
+
|
4
|
+
表をメールに貼り付ける(?)書式を保つ(?)ようにはなっていません。
|
5
|
+
|
2
6
|
|
3
7
|
```JavaScript
|
4
8
|
function createMailDraft() {
|
1
繰り返し処理の例を付記しました
test
CHANGED
@@ -77,3 +77,39 @@
|
|
77
77
|
}
|
78
78
|
```
|
79
79
|
|
80
|
+
繰り返し処理を利用して表を作成する例
|
81
|
+
```JavaScript
|
82
|
+
// テキスト形式のメール本文作成の例
|
83
|
+
function makePlainBody(table) {
|
84
|
+
let body = '';
|
85
|
+
table.forEach(tr => {
|
86
|
+
tr.forEach(th => {
|
87
|
+
body += '|';
|
88
|
+
body += th;
|
89
|
+
})
|
90
|
+
body += '|\r\n';
|
91
|
+
})
|
92
|
+
|
93
|
+
return body;
|
94
|
+
}
|
95
|
+
|
96
|
+
// HTML形式のメール本文作成の例
|
97
|
+
function makeHtmlBody(table) {
|
98
|
+
let html = '<body>';
|
99
|
+
html += '<table border = "2" style="border-collapse: collapse">';
|
100
|
+
table.forEach(tr => {
|
101
|
+
html += '<tr>';
|
102
|
+
tr.forEach(th => {
|
103
|
+
html += '<th>';
|
104
|
+
html += th;
|
105
|
+
html += '</th>';
|
106
|
+
});
|
107
|
+
html += '</tr>';
|
108
|
+
});
|
109
|
+
html += '</table>';
|
110
|
+
html += '</body>';
|
111
|
+
|
112
|
+
return html;
|
113
|
+
}
|
114
|
+
```
|
115
|
+
|