-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextBookCreator.gs
76 lines (69 loc) · 2.29 KB
/
textBookCreator.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
<script src="/styles.gs"></script>
var resultJson = 'result.json'; // result filename here; file must be present on Google Drive
function main() {
var body = DocumentApp.getActiveDocument().getBody();
body.clear();
setBodyParams(body);
var id = searchFiles(resultJson);
var lines = parseJson(id);
writeToFile(lines);
}
function parseJson(id) {
var file = DriveApp.getFileById(id);
var data = file.getBlob().getDataAsString();
var json = JSON.parse(data);
return json;
}
function searchFiles(str) {
var searchText = 'title contains "'+str+'" and trashed=false';
var files = DriveApp.searchFiles(searchText);
while (files.hasNext()) {
var file = files.next();
return file.getId();
}
}
function writeToFile(jsonFile) {
for (i in jsonFile) {
// save document after every 2000 entries
if (i%2000 == 0) {
DocumentApp.getActiveDocument().saveAndClose();
body = DocumentApp.getActiveDocument().getBody();
}
if (jsonFile[i].type === 'sms') {
applyParagraphStyle(body.appendParagraph(jsonFile[i].text.toString()), jsonFile[i].dir);
applyDateStyle(body.appendParagraph(jsonFile[i].date), jsonFile[i].dir);
} else if (jsonFile[i].type === 'mms') {
var table = jsonFile[i].content;
for (var j in table) {
for (var key in table[j]) {
if (key === "text/plain") {
applyParagraphStyle(body.appendParagraph(table[j][key].toString()), jsonFile[i].dir);
} else if (key.indexOf('image') !== -1) {
var imgId = searchFiles(table[j][key])
var img = DriveApp.getFileById(imgId).getBlob();
var imgPar = body.appendImage(img);
formatImage(imgPar);
applyParagraphStyle(imgPar.getParent(), jsonFile[i].dir)
} else {
applyParagraphStyle(body.appendParagraph('<<'+table[j][key]+'>>'), jsonFile[i].dir);
}
}
}
applyDateStyle(body.appendParagraph(jsonFile[i].date), jsonFile[i].dir);
}
}
}
function applyParagraphStyle(par, dir) {
if (dir === 'sent') {
setStyleSent(par);
} else if (dir === 'received') {
setStyleReceived(par);
}
}
function applyDateStyle(par, dir) {
if (dir === 'sent') {
setDateStyleSent(par);
} else if (dir === 'received') {
setDateStyleReceived(par);
}
}