CONTEXT
In my Drive there's the following structure:
Ops folder || Sales folder
Ops contains 3 folders: Process1 / Process2 / Process3
Sales contains 3 folders: Customer1 / Customer2 / Customer3
TARGET
I'm asking for the names of the folders in Drive > SALES
In a Sheet I have a list of Names of some Prospects. I'm checking one by one if the name of the prospect matches with any folder in my Drive.
In case there's a folder that matches the prospect name then do nothing.
In case there's no folder for that Prospect I want to create a new folder in Sales with the prospect name
function prepararPropuesta() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var ssAS = ss.getActiveSheet()
var ssProspects = ss.getSheetByName('Prospects')
var lr = ssProspects.getLastRow()
var lc = ssProspects.getLastColumn()
// I'm looking for the column 'Prospect' in the Sheet
for(i=2; i<lc; i++){
var columnas = ssProspects.getRange(2, i).getValue()
Logger.log(columnas)
if(columnas == 'Nombre Prospect'){
var colProspect = ssProspects.getRange(2, i).getColumn()
Logger.log(colProspect)
}
}
/*
Now I'm looking for the names of the folders in my Drive
This is looking in all my Drive. It would be nice to filter the
search and look just in SubFolders(Sales)
*/
var folders = DriveApp.searchFolders('"me" in owners');
while (folders.hasNext()) {
var folder = folders.next();
Logger.log(folder.getName());
/* The first Prospect Name is in row 2 and I'm creating a loop to
get each Name
If the name matches an existing Folder then ends the function
If the Prospect Name is not found in the Folders list then creates a new Folder under the name prospectName
*/
for(i=2; i<lr; i++){
var prospectName = ssProspects.getRange(i, colCliente).getValue()
var folderName = folder.getName()
if(folderName = prospectName){
return
}
}
DriveApp.createFolder(prospectName)
}
}
In between the lines in the code you will find a description step by step
(1) I'm finding the column in which the prospect names are -> This works fine
(2) I'm using WHILE to get all the name folders in my Drive This log
Logger.log(folder.getName())
Returns
[18-04-21 06:33:35:985 PDT] FolderIterator
Is it getting the names correctly?
(3) I created the variables folderName and prospectName and looped them to check if there's a match -> This part doesn't work correctly. I guess that my I'm not using the right methods although I researched in developers.google. How could it be done?
Thnx!