-
Notifications
You must be signed in to change notification settings - Fork 0
/
googleFiles.js
61 lines (53 loc) · 1.33 KB
/
googleFiles.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
// Utility functions for google documents
/**
* Move a file from its current location to a target folder
*
* @param sourceFileId { String }
* @param targetFolderId { String }
*/
function moveFileById(sourceFileId, targetFolderId) {
var file = DriveApp.getFileById(sourceFileId);
file
.getParents()
.next()
.removeFile(file);
DriveApp.getFolderById(targetFolderId).addFile(file);
}
/**
* Provided a file id, create and return the PDF version of the file
*
* @param sourceFileId { String }
*
* return PDF file
*/
function saveAsPdfById(sourceFileId) {
var driveSourceFile = DriveApp.getFileById(sourceFileId);
return DriveApp.createFile(driveSourceFile.getAs('application/pdf'));
}
/**
* Provided a file id, remove the file
*
* @param fileId { String }
*/
function removeFileById(fileId) {
var file = DriveApp.getFileById(fileId);
file
.getParents()
.next()
.removeFile(file);
}
/**
* @param url { String } URL of the form https://docs.google.com/spreadsheets/d/179rba3_kgZLp_16iJRcMCd7j53-u6LuG9oHFp304uDQ/edit#gid=165078932
*/
function getIdFromUrl(url) {
return url.split('/')[5];
}
/**
* Returns the google id of the copy
* @param {String} googleId id of a file in google drive
*/
function copyFileById(googleId) {
return DriveApp.getFileById(googleId)
.makeCopy()
.getId();
}