-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.ts
More file actions
110 lines (99 loc) · 3.22 KB
/
convert.ts
File metadata and controls
110 lines (99 loc) · 3.22 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import SchemaFile = GoogleAppsScript.Drive.Schema.File;
import File = GoogleAppsScript.Drive.File;
import FileIterator = GoogleAppsScript.Drive.FileIterator;
const CONVERTED_PROPERTY_KEY = 'converted';
const CONVERTED_FROM_PROPERTY_KEY = 'convertedFrom';
const CONVERTED_PROPERTIES_SEARCH_STRING = 'properties has { key=\'' + CONVERTED_PROPERTY_KEY + '\' and value=\'true\' and visibility=\'PRIVATE\' }';
const LANGUAGE = 'en-NZ';
function convertInvoices(): void {
convertInvoicesInFolder(invoicesFolder());
}
function invoicesFolder(): Folder {
const endDate = getEndDate();
const financialYear = endDate.getYear();
const financeFolder = getFinanceFolder(financialYear);
return getChildFolder(financeFolder, INVOICES_FOLDER);
}
function convertInvoicesInFolder(folder): void {
const files = folder.searchFiles('not ' + CONVERTED_PROPERTIES_SEARCH_STRING);
const converted = convertedInvoiceIds(folder);
while (files.hasNext()) {
const file = files.next();
const id = file.getId();
if (arrayContains(converted, id)) {
// @ts-ignore
console.log('Converted already: %s (%s)', file.getName(), id);
} else {
convertInvoice(file , file.getName() + '.txt');
}
}
const childFolders = folder.getFolders();
while (childFolders.hasNext()) {
convertInvoicesInFolder(childFolders.next());
}
}
function convertedInvoiceIds(folder: Folder): string[] {
const args = {
q: '\'' + folder.getId() + '\' in parents and trashed=false and ' + CONVERTED_PROPERTIES_SEARCH_STRING
};
const conversions = Drive.Files.list(args);
return conversions.items.map(convertedFromProperty);
}
function convertedFromProperty(file: SchemaFile): string {
const property = file.properties.find((property): boolean => property.key === CONVERTED_FROM_PROPERTY_KEY)
if (property) {
return property.value
} else {
throw 'File "' + file.id + '" did not contain the ' + CONVERTED_FROM_PROPERTY_KEY + ' property but it has been converted.';
}
}
function convertInvoice(file: File, name: string): void {
const id = file.getId();
// @ts-ignore
console.log('Converting file: %s (%s)', file.getName(), id);
const body = {
title: name,
properties: [
{
key: CONVERTED_PROPERTY_KEY,
value: 'true'
},
{
key: CONVERTED_FROM_PROPERTY_KEY,
value: id
}
]
};
const args = {
ocr: true,
ocrLanguage: LANGUAGE
};
const result = Drive.Files.copy(body, id, args);
// @ts-ignore
console.log('Converted file to: %s (%s)', name, result.id);
}
class Invoices {
expense: string
folder: Folder
files: FileIterator
constructor(expense: string, folder: Folder, files: FileIterator) {
this.expense = expense
this.folder = folder
this.files = files
}
}
function getConvertedExpenseInvoices(folder: Folder): {} {
const loop = (folder: Folder, results: {}): {} => {
const expense = folder.getName()
const files = folder.searchFiles(CONVERTED_PROPERTIES_SEARCH_STRING)
if (files.hasNext()) {
results[expense] = new Invoices(expense, folder, files)
}
const childFolders = folder.getFolders()
while (childFolders.hasNext()) {
loop(childFolders.next(), results)
}
return results
}
return loop(folder, {})
}