-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.gs
More file actions
executable file
·128 lines (104 loc) · 4.16 KB
/
Graph.gs
File metadata and controls
executable file
·128 lines (104 loc) · 4.16 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
class Module {
constructor(name, rowNo, colNo, resourceList = [], representative = null, prereqs = new Set(), category = "") {
this.name = name; // string
this.rowNo = rowNo; // int
this.colNo = colNo; // int
this.resourceList = resourceList; // list of resources
this.representative = representative; // a resource
this.prereqs = prereqs; // set of modules
this.category = category; // string
}
prereq_fold_left(reducer, initialValue) {
this.prereqs = this.resourceList.reduce(reducer, initialValue);
}
}
function union(accumulator, curr){
return new Set([...accumulator,...(curr.prereqModules)]);
}
function intersection(accumulator, curr){
new Set([...accumulator].filter(item => curr.prereqModules.has(item)));
}
class Resource {
constructor(name, link, moduleClassification = null, prereqModules = new Set(), category = "", tags = []) {
this.name = name; // string
this.link = link; // URL
this.moduleClassification = moduleClassification; // instance of Module
this.prereqModules = prereqModules; // set of modules -> this is strings at generation because we don't know that all modules have been generated yet, might want to modify this later
this.category = category; // string
this.tags = tags; // list of strings
}
// Method to convert prereqModules from strings to module objects
convertPrereqModules(moduleMap) {
// Create a new set to store the converted module objects
const convertedModules = new Set();
this.prereqModules.forEach(moduleName => {
if (moduleMap.has(moduleName)) {
convertedModules.add(moduleMap.get(moduleName)); // Add the corresponding module object
} else {
console.warn(`Module "${moduleName}" not found in moduleMap.`);
}
});
// Replace the current prereqModules set with the converted set
this.prereqModules = convertedModules;
}
}
let graph_resource_list = [];
const moduleMap = new Map();
function generateGraphResourceList() {
// Open the Google Sheet by its name or ID
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Resources');
const data = sheet.getDataRange().getValues();
// Clear the global resource list
graph_resource_list = [];
// Process the header and data rows
const headers = data[0]; // Assuming first row is the header
const rows = data.slice(1); // Skip header row
rows.forEach((row, index) => {
const resourceName = row[headers.indexOf('Resource Name')];
const link = row[headers.indexOf('Link')];
const category = row[headers.indexOf('Category')];
const moduleNames = row[headers.indexOf('Module')].split(',').map(tag => tag.trim());
const tags = row[headers.indexOf('Tags')].split(',').map(tag => tag.trim());
const prereqModules = row[headers.indexOf('Prerequisite module(s)')].split(',').map(prereq => prereq.trim());
const level = row[headers.indexOf('Level')]; // Optional if needed
// Ensure the module exists in the map
moduleNames.forEach((moduleName) => {
let moduleInstance = moduleMap.get(moduleName);
if (!moduleInstance) {
moduleInstance = new Module(moduleName, null, null, [], null, new Set(), category);
moduleMap.set(moduleName, moduleInstance);
}
// Create the resource
const resource = new Resource(
resourceName,
link,
moduleInstance,
new Set(prereqModules),
category,
tags
);
// Add the resource to the module's resource list
moduleInstance.resourceList.push(resource);
// Add the resource to the global list
graph_resource_list.push(resource);
});
});
// Convert all prereqmodules from strings to the actual objects
graph_resource_list.forEach(resource => {
resource.convertPrereqModules(moduleMap);
});
// Log the global resource list (for debugging)
console.log(graph_resource_list);
return graph_resource_list;
}
function myFunction() {
generateGraphResourceList();
let iter = moduleMap.values();
for(i = 0; i < 3; i++){
iter.next();
}
const module1 = iter.next().value;
console.log(module1.name);
module1.prereq_fold_left(union, new Set([]));
console.log(module1.prereqs);
}