-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportContactDay.gs
141 lines (104 loc) · 4.76 KB
/
importContactDay.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
function importContactDay() {
// zipDownload.sh, contactDay.shに続いて、2:00 - 3:00に自動実行
// 実行結果はスプレッドシートに入り、データポータルで自動更新
// https://datastudio.google.com/reporting/069598a2-3f01-4b51-b023-cdb478992182
// 実行結果に応じた内容をメールする
const toAdress = "送り先メールアドレス"; //送り先アドレス
const subject = "ContactDay";//メールの題目
const name = "Google Apps Script by 送り主";//送り主の名前
const today = new Date();
// ContactDay.txtのfolderをfolderIDで特定
const folderID = 'ContactDay.txtが格納されたGoogleDriveのID' //ContactDay.txtのfolderID
const targetFolder = DriveApp.getFolderById(folderID); //ContactDay.txtが保存されるフォルダ
const ContactDayFiles = targetFolder.searchFiles('title contains "cDay" and title contains "\.csv"');
let lastCreatedDate = new Date('2020/1/1 0:0:0');
let createdDateArray = []; //ログ用。
if (!ContactDayFiles.hasNext()) {
console.log('file not found')
const body =
"ContactDayログ\n" +
"file not found" + "\n";
MailApp.sendEmail({to:toAdress, subject:subject, name:name, body:body});
return;
}
//■■■ ダウンロードされた複数のcDayファイルから更新日が最終のcDayファイルを選定 ■■■
while (ContactDayFiles.hasNext()) {
const cDayFile = ContactDayFiles.next();
const fileName = cDayFile.getName();
const createdDate = cDayFile.getDateCreated();
const createdDateFileId = cDayFile.getId();
if (createdDate.getTime() - lastCreatedDate.getTime() > 0) {
console.log('上書き')
targetFileId = cDayFile.getId();
targetFileName = cDayFile.getName();
lastCreatedDate = createdDate;
}else{
console.log('上書きせず');
}
createdDateArray.push(createdDate,createdDateFileId);
console.log('FileName: ', fileName, 'Date: ', createdDate, 'Id: ', createdDateFileId);
console.log('lastDate:', lastCreatedDate);
console.log('lastId:', targetFileId);
}
//■■■ 選定したファイルをスプレッドシートに上書き ■■■
// 読み書きの対象のSpreadSheetを定義
const ssID = 'ファイルをインポートする先のスプレッドシートID'
const ssName = 'ContactDaySheet';
const ss = SpreadsheetApp.openById(ssID);
const sh = ss.getSheetByName(ssName);
const lastRow = sh.getLastRow();
let lastValues = [];
// 最新の最終行を配列に読み込む
lastValues = sh.getRange(lastRow, 1, 1, 4).getValues();
console.log("lastValues:",lastValues);
const lastZIPNUM = lastValues[0][1];
console.log("last Zip Number", lastZIPNUM);
// 対象のCSVファイル(cDayYYYYMMDD)
const file = DriveApp.getFileById(targetFileId);
const data = file.getBlob().getDataAsString();
const csv = Utilities.parseCsv(data,',');
const csvlength = csv.length;
console.log(csvlength);
// 読み出したCSVからzip番号が新しい行を取り出してtodayCsvにいれる
const todayCsv = [];
let i = 0;
let j = 0;
while (i < csvlength) {
if (csv[i][1] > lastZIPNUM) {
todayCsv[j] = csv[i];
console.log("i:", i, "j:",j, "todayCsv[i][1]",todayCsv[j])
j++;
}
i++;
}
// todayCsvを対象スプレッドシートの最終行以下に追記
if (todayCsv.length == "") {
console.log('new TEK not found')
const body =
"ContactDayログ\n" +
"new TEK not found" + "\n";
MailApp.sendEmail({to:toAdress, subject:subject, name:name, body:body});
return;
}
sh.getRange(lastRow+1,1,todayCsv.length,todayCsv[0].length).setValues(todayCsv);
// 追記した行にセルの書式のみコピー(Google DataStudio用)
const range = sh.getRange(lastRow, 1, 1, todayCsv[0].length);
range.copyFormatToRange(sh, 1, todayCsv[0].length, lastRow+1, lastRow+todayCsv.length);
// https://arukayies.com/gas/copyformattorange
// 処理内容をlogシートに書き込み
const shLog = ss.getSheetByName('log');
const lastRowLog = shLog.getLastRow();
console.log("lastRowLog:", lastRowLog);
let logData = [lastCreatedDate, targetFileId, targetFileName, j, lastZIPNUM, todayCsv[0][1], todayCsv[j-1][1]];
console.log(logData);
console.log('logData.length:',logData.length);
// 元データを削除せず上書き
shLog.getRange(lastRowLog+1, 1, 1, logData.length).setValues([logData]);
// メール送信
const body =
"ContactDayログ\n" +
logData + "\n" +
"https://docs.google.com/spreadsheets/d/”ここにシートID”/edit?usp=sharing";
MailApp.sendEmail({to:toAdress, subject:subject, name:name, body:body});
// https://qiita.com/YusukeKameyama/items/5ae840ec8d4382a215db
}