-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertFiles-cp.js
72 lines (58 loc) · 3.12 KB
/
ConvertFiles-cp.js
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
/*
Script Title: Google Housekeeping - ConvertFiles to PDF
Version: 1.0
Creation Date: 5/22/2020
Author: Robert C. Tucker
Description: The Google Drive Housekeeping - ConvertFiles to PDF script is a google script designed to cull through a folder on Google Drive and convert Google Docs to PDF files
for syncing to local server.
Notes: This Script must be run at script.google.com, unless you have a standard hosted google project with drive api access.
*/
function ConvertFilesToPDF(folder, file) {
//Declare all FolderIDs iin arrSearchFolderIDs. Replace with IDs of the folders you wish to search within.
//This can be obtained by going to your desired folder and copying everything in the URL after https://drive.google.com/drive/folders/
var arrSearchFolderIDs = [
//'FolderID', //uncomment to add additional search folder IDs.
'FolderID'
];
for (var i = 0; i < arrSearchFolderIDs.length; i++) {
var SearchFolder = DriveApp.getFolderById(arrSearchFolderIDs[i]);
//Set Folder as a variable containing folders within search folder.
var Folders = SearchFolder.getFolders();
//While statment to search for all folders within the search folder.
while (Folders.hasNext()) {
var Folder = Folders.next();
var FolderID = Folder.getId();
var FolderName = Folder.getName();
var hasFolder = Folder.getFolders().hasNext(); // get the list of folders in the root of that folder
//Replace Templates with folders you would like the script to ignore and not remove.
if (FolderName != "Templates"){
//Get all google docs by File Type
var Files = Folder.getFilesByType(MimeType.GOOGLE_DOCS);
while (Files.hasNext()){
Logger.log ('\n' + "Search Folder: " + FolderName)
Logger.log ('\n' + FolderName + "Folder has no child folders.")
var gdoc = Files.next();
var FileID = gdoc.getId();
var FileName = gdoc.getName();
Logger.log ('\n' + FileName + " is being converted to PDF for sync to Server." + '\n' + "File ID: " + FileID)
//Convert to PDF
var docPDF = DocumentApp.openById(FileID).getAs('application/pdf').setName(FileName);
Folder.createFile(docPDF);
var PDFName = docPDF.getName();
Logger.log ('\n' + "PDF Converted" + '\n' + "PDF Name: " + PDFName + ".pdf");
//Delete original File
gdoc.setTrashed(true);
}
if (hasFolder){
//Logger.log ('\n' + FolderName + "Folder has child folders. Added to Search Array")
arrSearchFolderIDs.push(FolderID);
}
}
}
}
//Send email to Administrator - Replace email with that of the desired contact. Email will come from the google account associated with the script.
var recipient = "[email protected]";
var subject = 'Files have been converted to PDFs.';
var body = Logger.getLog();
MailApp.sendEmail(recipient, subject, body);
}