1

Two scripts below generate a new folder containing a new document which is based on answers from a Google form.

Needing help, if anyone knows how I can prevent a duplicate folder being created


CreateChannelFolder():

This is the first trigger on form submission and creates a folder that in my case is named after a 'Channel Name' which is in row 2 of the Google sheet linked to the form.

function createChannelFolder() {
var ss = SpreadsheetApp.getActive();
var names = ss.getSheetByName("SHEETNAME");
var ChannelName = names.getRange(names.getLastRow(), 2).getValue(); 
var parentFolder=DriveApp.getFolderById("FOLDERID");
return parentFolder.createFolder(ChannelName); 

}

AutoFillGoogleDocFromForm(e):'

This is the second trigger on form submission and creates a copy of a template document and then fills in answers from a Google form based on the rows of the Google sheet linked to the form.

function autoFillGoogleDocFromForm(e) {
  //e.values is an array of form values  
  var Timestamp = e.values[0];
  var Channel = e.values[1];
  var Name = e.values[2];;  
  var file = DriveApp.getFileById('FILEID'); 
  var folder = createChannelFolder(); 
  var copy = file.makeCopy(Channel + ',' + Name, folder); 
  var newId = copy.getId();
  var doc = DocumentApp.openById(newId); 
  var body = doc.getBody(); 
  body.replaceText('{{Timestamp}}', Timestamp); 
  body.replaceText('{{Channel}}', Channel);  
  body.replaceText('{{Name}}', Name);        
  doc.saveAndClose(); 
Marios
  • 26,333
  • 8
  • 32
  • 52
Ewan Farry
  • 49
  • 4

1 Answers1

2

Solution:

You can check if a folder with that name exists, otherwise don't create it twice. Modify createChannelFolder() like that:

function createChannelFolder() {
    var ss = SpreadsheetApp.getActive();
    var names = ss.getSheetByName("SHEETNAME");
    var ChannelName = names.getRange(names.getLastRow(), 2).getValue(); 

    var folders = DriveApp.getFoldersByName(ChannelName);
    while (folders.hasNext()) { 
    folder = folders.next();    
    if(folder.getName()==ChannelName){
     return folder;}
    }
    var parentFolder=DriveApp.getFolderById("FOLDERID");
    return parentFolder.createFolder(ChannelName);         
}

Reproducible example:

The following code snippet creates and returns a new folder with the name test. If the latter already exists, it does not create a new one and it returns the existing one.

function createChannelFolder() {

    var ChannelName = "test";
    var folders = DriveApp.getFoldersByName(ChannelName);
    while (folders.hasNext()) { 
    folder = folders.next();    
    if(folder.getName()==ChannelName){
     return folder;}
    }
    var parentFolder=DriveApp.getFolderById("folderID");
    return parentFolder.createFolder(ChannelName);         
}

Restrictions:

  • If you have multiple folders with the same name (ChannelName), then the code will return the first folder that it finds. In order for the code to work properly you need to delete all the duplicate folders which anyway you don't need since it is something you would like to avoid.
Marios
  • 26,333
  • 8
  • 32
  • 52
  • Thanks again Marios, but this seems not to work, i'm trying to find ways to avoid this but seeing some other similar reports that I can't implement the solutions from; https://stackoverflow.com/questions/11910734/how-do-i-create-a-file-in-a-folder https://stackoverflow.com/questions/44607607/how-to-avoid-creating-duplicate-folders-in-google-drive https://stackoverflow.com/questions/41648174/create-a-new-folder-in-drive-using-google-app-script – Ewan Farry Sep 05 '20 at 23:39
  • @EwanFarry I think the issue is that you have already duplicate folders. Please delete them before you use my solution. Check also my updated answer (I added the part with the restrictions). Please also, for documentation reasons, accept the answer if it was helpful and consider also voting up. – Marios Sep 06 '20 at 00:54
  • Hi @Marios I think I misunderstood the triggers function and had one set per script. Making just one trigger has fixed it! Thanks very much for all your help! – Ewan Farry Sep 06 '20 at 15:55