-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathSelectFilteredRowsFromTableAsJSON.ts
More file actions
134 lines (124 loc) · 4.51 KB
/
SelectFilteredRowsFromTableAsJSON.ts
File metadata and controls
134 lines (124 loc) · 4.51 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
function main(workbook: ExcelScript.Workbook): InterviewInvite[] {
console.log("Current date time: " + new Date().toUTCString())
const MEETING_DURATION = workbook.getNamedItem('MeetingDuration').getRange().getValue() as number;
const sheet = workbook.getWorksheet('Interviews');
const table = sheet.getTables()[0];
const dataRows: string[][] = table.getRange().getTexts();
// or use this if there's no table
// let dataRows = sheet.getUsedRange().getValues();
const selectedRows = dataRows.filter((row, i) => {
// Select header row and any data row with the status column equal to approach value
return (row[1] === 'FALSE' || i === 0)
})
const recordDetails: RecordDetail[] = returnObjectFromValues(selectedRows as string[][]);
const inviteRecords = generateInterviewRecords(recordDetails, MEETING_DURATION);
console.log(JSON.stringify(inviteRecords));
return inviteRecords;
}
/**
* This helper funciton converts table values into an object array.
*/
function returnObjectFromValues(values: string[][]): RecordDetail[] {
let objArray: BasicObj[] = [];
let objKeys: string[] = [];
for (let i = 0; i < values.length; i++) {
if (i === 0) {
objKeys = values[i]
continue;
}
let obj = {}
for (let j = 0; j < values[i].length; j++) {
obj[objKeys[j]] = values[i][j]
}
objArray.push(obj);
}
return objArray as RecordDetail[];
}
/**
* Generate interview records by selecting required columns
* @param records Input records
* @param mins Number of minutes to add to the start date-time
*/
function generateInterviewRecords(records: RecordDetail[], mins: number): InterviewInvite[] {
const interviewinvites: InterviewInvite[] = []
records.forEach((record) => {
// Interviewer 1
// If the start date-time is greather than current date-time, add to output records
if ((new Date(record['Start time1'])) > new Date()) {
console.log("selected " + new Date(record['Start time1']).toUTCString());
let startTime = new Date(record['Start time1']).toISOString();
// compute the finish time of the meeting
let finishTime = addMins(new Date(record['Start time1']), mins).toISOString();
interviewinvites.push({
ID: record.ID,
Candidate: record.Candidate,
CandidateEmail: record['Candidate email'] as string,
CandidateContact: record['Candidate contact'] as string,
Interviewer: record.Interviewer1,
InterviewerEmail: record['Interviewer1 email'],
StartTime: startTime,
FinishTime: finishTime
})
} else {
console.log("Rejected " + (new Date(record['Start time1']).toUTCString()))
}
// Interviewer 2
// If the start date-time is greather than current date-time, add to output records
if ((new Date(record['Start time2'])) > new Date()) {
console.log("selected " + new Date(record['Start time2']).toUTCString());
let startTime = new Date(record['Start time2']).toISOString();
// compute the finish time of the meeting
let finishTime = addMins(new Date(record['Start time2']), mins).toISOString();
interviewinvites.push({
ID: record.ID,
Candidate: record.Candidate,
CandidateEmail: record['Candidate email'] as string,
CandidateContact: record['Candidate contact'] as string,
Interviewer: record.Interviewer2,
InterviewerEmail: record['Interviewer2 email'],
StartTime: startTime,
FinishTime: finishTime
})
} else {
console.log("Rejected " + (new Date(record['Start time2']).toUTCString()))
}
})
return interviewinvites;
}
/**
* Add minutes to start date-time
* @param startDateTime Start date-time
* @param mins Minutes to add to the start date time
*/
function addMins(startDateTime: Date, mins: number) {
return new Date(startDateTime.getTime() + mins * 60 * 1000);
}
// Basic key-value pair object
interface BasicObj {
[key: string]: string | number | boolean
}
// Input record that matches the table data
interface RecordDetail extends BasicObj {
ID: string
'Invite to interview': string
Candidate: string
'Candidate email': string
'Candidate contact': string
Interviewer1: string
'Interviewer1 email': string
Interviewer2: string
'Interviewer2 email': string
'Start time1': string
'Start time2': string
}
// Output record
interface InterviewInvite extends BasicObj {
ID: string
Candidate: string
CandidateEmail: string
CandidateContact: string
Interviewer: string
InterviewerEmail: string
StartTime: string
FinishTime: string
}