-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeader.ts
More file actions
53 lines (47 loc) · 2.11 KB
/
Copy pathHeader.ts
File metadata and controls
53 lines (47 loc) · 2.11 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
export class Header {
//takes in an array representing a header row and returns
//an object mapping field ID to its index (starting at 0)
static fieldToIndex(sheet: GoogleAppsScript.Spreadsheet.Sheet, requiredFields:string[]): any {
let headerRow = Header.getHeaderRow(sheet);
let missingFields = requiredFields.slice();
let header = {}
for (let i = 0; i < headerRow.length; i++) {
let fieldName = headerRow[i]
header[fieldName] = i + 1;
// if the parsed field name is in the required fields list, remove it from the
// missing fields list so it won't trigger a validation error
let requiredFieldIndex = missingFields.indexOf(fieldName)
if (requiredFieldIndex >= 0)
missingFields.splice(requiredFieldIndex, 1)
}
if (missingFields.length != 0) {
throw new MissingHeaderFieldsError(missingFields, sheet.getParent().getUrl(), sheet.getParent().getName(), sheet.getName())
}
return header
}
// returns just the header row for a given tab and sheet id
static getHeaderRow(sheet: GoogleAppsScript.Spreadsheet.Sheet): any[] {
//cred https://mashe.hawksey.info/2018/02/google-apps-script-patterns-getting-a-google-sheet-header-row/
// todo: some kinda checking to see if there is any data in this row?
return sheet.getDataRange().offset(0, 0, 1).getValues()[0]
}
}
class MissingHeaderFieldsError extends Error {
missingHeaderFields: string[]
url: string
fileName: string
tabName: string
constructor(missingHeaderFields: string[], url: string, fileName: string, tabName: string) {
super(`missing required header fields -- a column name has been erroneously modified! \nMissing Fields: ${missingHeaderFields.join('|')}\nFilename: ${fileName}\nURL: ${url}`)
this.missingHeaderFields = missingHeaderFields
this.url = url
this.fileName = fileName
this.tabName = tabName
}
}
interface HeaderCheck {
changed: boolean
column?: number
oldFieldID?: string
newFieldID?: string
}