Created
October 26, 2022 12:54
-
-
Save phillypb/3c094632dbc0e76caa61e8d7c919aa91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Loop through a folder of CSV files. | |
Capture tutor name (from file name) and code the file ID / Module Code / Group No with it in a JavaScript Object. | |
Collates/sorts all files together for each individual tutor. | |
End result is ability to loop through JavaScript Object and action CSV files | |
belonging to each tutor in-turn. | |
Collated data will look like this: | |
{Micky Mouse=[{fileId=1BRv0IhTxYxhsYbDN85DkpMqrTPyJHXyK, group=Grp 05.csv, moduleCode=CDE}, {moduleCode=CDE, fileId=1dmwVbPdwNQbOg9nfO98RD213QpKOLuqk, group=Grp 01.csv}], Jane Doe=[{group=Grp 04.csv, fileId=1z8O6jnm8INdZf9NWZBCoWTvB0zTEQDDe, moduleCode=ABC}, {moduleCode=ABC, fileId=1MnenQwueMKWssWlBOEaM8lcozB9ghYEn, group=Grp 03.csv}, {fileId=1-kh1Vp6It4LvVNSahUMkP529hu4-_es9, moduleCode=ABC, group=Grp 02.csv}]} | |
*/ | |
function readFiles2() { | |
// get folder of files | |
var folder = DriveApp.getFolderById("ENTER FOLDER ID"); | |
// get all files in folder | |
var files = folder.getFiles(); | |
// create empty JavaScript Object | |
var obj = {}; | |
// loop through each file in folder *********************************** | |
while (files.hasNext()) { | |
// get the next file | |
var theFile = files.next(); | |
// get the file name | |
var theFileName = theFile.getName(); | |
// get the file Id | |
var theFileId = theFile.getId(); | |
// check file name ends with '.csv' to avoid picking up the Google Sheet file in the process | |
if (theFileName.endsWith(".csv")) { | |
// split the file name into 3 chunks to extract relevant info | |
var items = theFileName.split(" - "); | |
var moduleCode = items[0]; | |
var tutor = items[1]; | |
var group = items[2]; | |
// see if the tutor already exists in the JavaScript Object ********** | |
if (obj[tutor] == null) { | |
// the Object has not seen this tutor yet | |
// create an array in that tutor with file info. | |
obj[tutor] = [{ fileId: theFileId, moduleCode: moduleCode, group: group }]; | |
} | |
else { | |
// the Object knows about this tutor | |
// so add the current file info onto the existing array. | |
obj[tutor].push({ fileId: theFileId, moduleCode: moduleCode, group: group }); | |
} | |
// see if the tutor already exists in the JavaScript Object ********** | |
} | |
else { | |
// not .csv file so skip | |
}; | |
} | |
// loop through each file in folder *********************************** | |
// now that all files have been read, do something with this JavaScript Object ********************* | |
/* | |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of - the 'for ... of' statement executes a loop that operates on a sequence of values sourced from an iterable object (eg an array). | |
2 variables are created (tutor and fileObjs) as the Object is then iterated over as an array. | |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries - the 'Object.entries()' method returns an array of a given object's own enumerable string-keyed property [key, value] pairs. | |
*/ | |
// get Object as an array for iterating through for tutors and corresponding file info | |
var arrayToIterate = Object.entries(obj); | |
Logger.log(arrayToIterate); | |
for (var [tutor, fileObjs] of arrayToIterate) { | |
// the is where the Google Sheet file would be created for the tutor | |
// loop through the file Objects for each tutor *************** | |
for (fileObj of fileObjs) { | |
// extract File ID | |
var fileId = fileObj.fileId; | |
// extract Module Code | |
var modCode = fileObj.moduleCode; | |
// extract Group | |
var group = fileObj.group; | |
Logger.log(tutor + "\t" + fileId + " " + modCode + " " + group); | |
} | |
} | |
// now that all files have been read, do something with this object ********************* | |
for (tutor in obj) { | |
// return the JavaScript Object as an Array to loop through each tutor | |
var theArrayOfObjects = obj[tutor]; | |
// the is where the Google Sheet file would be created for the tutor | |
// loop through the file info for each tutor *************** | |
for (var i = 0; i < theArrayOfObjects.length; i++) { | |
// get the Object for specific tutor | |
var fileObj = theArrayOfObjects[i]; | |
// log the tutor | |
Logger.log('Tutor is: ' + tutor); | |
// extract file ID | |
var fileID = fileObj.fileId; | |
Logger.log('single fileID is: ' + fileID); | |
// extract module code | |
var modCode = fileObj.moduleCode; | |
Logger.log('single modCode is: ' + modCode); | |
// extract group | |
var group = fileObj.group; | |
Logger.log('single group is: ' + group); | |
// insert Line Break for Logging | |
Logger.log('\n'); | |
} | |
// loop through the file info for each tutor *************** | |
// delete the 'Template' tab and sort order | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment