-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathscript.js
More file actions
74 lines (64 loc) · 2 KB
/
script.js
File metadata and controls
74 lines (64 loc) · 2 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
// HOW TO USE:
// 1. Copy any rows from Google Sheets (Ctrl+C) and paste into input.tsv. The first row must be the header row.
// 2. Update the FIELDS_TO_EXTRACT array to specify which fields to extract from the sheet (You can use same tsv file header and pick).
// 3. Run this script with `node data/data-extract-script/script.js`.
// 4. The extracted data will be written to output.js.
// FIELDS TO EXTRACT: Update this array to specify which fields to extract from the sheet.
const FIELDS_TO_EXTRACT = [
'Email Address',
'Full name',
'Designation',
'Title of the Workshop/Talk:',
'Upload a Professional Photo',
'Bio for announcements (Max. 250 characters)',
'Website/Blog',
'Linkedin',
'Facebook',
'X',
'Github Repo',
'Others',
];
const fs = require('fs');
const path = require('path');
function getSheetData() {
const inputPath = path.join(__dirname, 'input.tsv');
if (!fs.existsSync(inputPath)) {
throw new Error(
'input.tsv not found. Paste copied Google Sheets data into input.tsv and re-run.'
);
}
const raw = fs.readFileSync(inputPath, 'utf-8');
return raw
.split('\n')
.filter((line) => line.trim() !== '')
.map((line) => line.split('\t'));
}
function extractFields(fields) {
const [headers, ...dataRows] = getSheetData();
const fieldIndices = fields.map((field) => {
const idx = headers.indexOf(field);
if (idx === -1)
console.warn(`Warning: Field "${field}" not found in sheet headers`);
return { field, idx };
});
return dataRows.map((row) =>
Object.fromEntries(
fieldIndices.map(({ field, idx }) => [
field,
idx !== -1 ? row[idx] : undefined,
])
)
);
}
function main() {
const result = extractFields(FIELDS_TO_EXTRACT);
const outputPath = path.join(__dirname, 'output.js');
fs.writeFileSync(
outputPath,
`export const OUTPUT = ${JSON.stringify(result, null, 2)};\n`,
'utf-8'
);
console.log(`\nWritten ${result.length} records to output.js`);
console.table(result);
}
main();