-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrive.ts
47 lines (40 loc) · 1.38 KB
/
drive.ts
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
import Folder = GoogleAppsScript.Drive.Folder;
const FINANCE_FOLDER = 'Finance'
function getFinanceFolder(financialYear: string): Folder {
const root = DriveApp.getRootFolder()
const financeFolder = getChildFolder(root, FINANCE_FOLDER)
return getChildFolder(financeFolder, financialYear)
}
function getChildFolder(folder: Folder, name: string): Folder {
const child = getMaybeChildFolder(folder, name)
if (child) {
return child
} else {
throw 'Folder "' + folder.getName() + '" does not have a child folder named "' + name + '".'
}
}
function getOrCreateChildFolder(folder: Folder, name: string): Folder {
const child = getMaybeChildFolder(folder, name)
return child ? child : folder.createFolder(name)
}
function getMaybeChildFolder(folder: Folder, name: string): null | Folder {
const children = folder.getFoldersByName(name)
if (children.hasNext()) {
const child = children.next()
if (children.hasNext()) {
throw 'Folder "' + folder.getName() + '" has multiple child folders named "' + name + '".'
} else {
return child
}
} else {
return null
}
}
function getOrCreateChildFolderPath(folder: Folder, path: string): Folder {
return path.split('/').reduce(getOrCreateChildFolder, folder)
}
function getDocumentText(docId: string): string {
const doc = DocumentApp.openById(docId);
const body = doc.getBody();
return body.getText();
}