-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdocs_and_gdrive
93 lines (84 loc) · 3.22 KB
/
gdocs_and_gdrive
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
// concatenate google docs. Takes an array of doc Ids and appends all docs to the first in the array
function mergeGoogleDocs(docIdsList) {
var baseDoc = DocumentApp.openById(docIdsList[0]);
var body = baseDoc.getActiveSection();
for (var i = 1; i < docIdsList.length; ++i) {
var otherBody = DocumentApp.openById(docIdsList[i]).getActiveSection();
var totalElements = otherBody.getNumChildren();
for (var j = 0; j < totalElements; ++j) {
var element = otherBody.getChild(j).copy();
var type = element.getType();
if (type == DocumentApp.ElementType.PARAGRAPH)
body.appendParagraph(element);
else if (type == DocumentApp.ElementType.TABLE)
body.appendTable(element);
else if (type == DocumentApp.ElementType.LIST_ITEM)
body.appendListItem(element);
else
throw new Error("Unknown element type: " + type);
}
}
}
// generate PDFs out of every file/GDoc in inFolder and put into outFolder if given, otherwise into source folder
function generatePdfs(inFolderId, outFolderId) {
var infolder = DriveApp.getFolderById(inFolderId);
var fileList = infolder.getFiles();
var outfolder;
if (outFolderId) {
outfolder = DriveApp.getFolderById(outFolderId);
} else {
outfolder = infolder;
}
while (fileList.hasNext()) {
var file = fileList.next();
var pdfFile = DriveApp.createFile(file.getAs('application/pdf'));
outfolder.addFile(pdfFile);
}
}
// copying files on google drive... this one does not work on team drives, there is a second version below that does.
// use our own function to make file copies, as apps script solution is utter sh**... (1.6s for one file!!!)
function copyFile(originFileId, copyTitle, targetFolderId) {
var response = Drive.Files.copy({
"title": copyTitle,
"parents": [
{
"kind": "drive#fileLink",
"id": targetFolderId
}
],
"mimeType": "application/vnd.google-apps.document"
}, originFileId
);
return response; // get fileId of new file and other metadata from response body
}
// teamdrive version
function copyFile(originFileId, copyTitle, targetFolderId) {
var response = Drive.Files.copy({
"title": copyTitle,
"parents": [
{
"kind": "drive#fileLink",
"id": targetFolderId
}
],
"mimeType": "application/vnd.google-apps.document"
}, originFileId, {"supportsTeamDrives": true}
);
return response; // get fileId of new file and other metadata from response body
}
// generalized version for both normal gdrive and teamdrive
// defaults to normal gdrive, pass bTeam as true for teamdrive support
function copyFile(originFileId, copyTitle, targetFolderId, bTeam = false) {
var response = Drive.Files.copy({
"title": copyTitle,
"parents": [
{
"kind": "drive#fileLink",
"id": targetFolderId
}
],
"mimeType": "application/vnd.google-apps.document"
}, originFileId, {"supportsTeamDrives": bTeam}
);
return response; // get fileId of new file and other metadata from response body
}