Last active
October 26, 2022 08:26
-
-
Save phillypb/d3d08093cc21e89333bfc08dd2b9ab24 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 file ID 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: | |
{"Jane Doe":["1-kh1Vp6It4LvVNSahUMkP529hu4-_es9","1z8O6jnm8INdZf9NWZBCoWTvB0zTEQDDe","1MnenQwueMKWssWlBOEaM8lcozB9ghYEn"],"Micky Mouse":["1BRv0IhTxYxhsYbDN85DkpMqrTPyJHXyK","1dmwVbPdwNQbOg9nfO98RD213QpKOLuqk"]} | |
*/ | |
function readFiles() { | |
// get folder of files | |
var folder = DriveApp.getFolderById("ENTER ID HERE"); | |
// 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]; // collected but not going to be used in this example | |
var tutor = items[1]; | |
var group = items[2]; // collected but not going to be used in this example | |
// 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 first file Id in there. | |
obj[tutor] = [theFileId]; | |
} | |
else { | |
// the Object knows about this tutor | |
// so add one (the current file Id) onto the existing array. | |
obj[tutor].push(theFileId); | |
} | |
// see if the tutor already exists in the JavaScript Object ********** | |
} | |
} | |
// 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 fileIds) 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 Ids | |
var arrayToIterate = Object.entries(obj); | |
Logger.log(arrayToIterate); | |
for (var [tutor, fileIds] of arrayToIterate) { | |
// the is where the Google Sheet file would be created for the tutor | |
// loop through the file Ids for each tutor *************** | |
for (fileId of fileIds) { | |
Logger.log(tutor + "\t" + fileId); | |
// open the file and read its data | |
// create a tab in the Google Sheet for this given file | |
// put the data into this tab | |
} | |
// loop through the file Ids for each tutor *************** | |
// delete the 'Template' tab and sort order | |
} | |
// now that all files have been read, do something with this object ********************* | |
for (tutor of arrayToIterate) { | |
// log array for each tutor and their corresponding file Ids | |
Logger.log(tutor); | |
// log array of all file Ids for given tutor | |
var fileIds = tutor[1]; | |
Logger.log(fileIds); | |
// the is where the Google Sheet file would be created for the tutor | |
// loop through the file Ids for each tutor *************** | |
for (var i = 0; i < fileIds.length; i++) { | |
// extract specific fileId | |
var fileId = fileIds[i]; | |
Logger.log('single fileId is: ' + fileId); | |
// open the file and read its data | |
// create a tab in the Google Sheet for this given file | |
// put the data into this tab | |
} | |
// loop through the file Ids 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