Search code examples
google-app-maker

Creating folders using DriveApp.createFolder gives duplicates


I am using a for loop to iterate over 105 records in a Pupils model in App Maker. For each record in the model, I am extracting a folder name from a field in the model.

I am using the folder name as input to DriveApp.createFolder().

Each folder is generated within the same root folder so that I end up with a root folder that has 105 newly created folders within it.

For each created folder, I assign ownership of the folder to a specific user and remove editing rights from the folder creator.

The root folder in this case has editing rights given to anyone in the domain with the link, so the created folder inherits this setting and this is why I remove the folder creator as an editor.

To help with my debugging, I write to the console after each folder is created and indicate which record from the pupil model is being processed.

What happens is that after about 25 to 30 folders have been created in the root folder the for loop seems to start again, but also continues from where it left off at the same time!

Here is a snippet from the debug console...

[1]

You can see that the loop seems to start again.

Here is my code...

    function createEvidenceFolder(){
    var person = Session.getActiveUser().getEmail();
    var pupils = app.models.Pupils.newQuery();
    pupils.filters.EvidenceFolder._equals = null;
    pupils.filters.Roll._equals = "Current";
    var pupil = pupils.run();
    var folder = "XXXThe Root Folder IDXXX";
    for (var x = 0; x < pupil.length; x++){
    var fname = pupil[x].Folder_Name;    
  console.log("Processing record "+x+" for "+fname);    
 var root = DriveApp.getFolderById(folder);
 var createdfolder = root.createFolder(fname).getId();
 Utilities.sleep(1000);
 DriveApp.getFolderById(createdfolder).setOwner("The email address of the User").removeEditor(person);
  pupil[x].EvidenceFolder = createdfolder;   
  }
  app.saveRecords(pupil);   
  console.log("Processed "+x+" evidence folders");
}

I went back and added the Utilities.sleep(1000) as I was hunting for answers.

So why am I getting twice the number of folders that I should be?

Why does the for loop seem to run concurrently?

By the way, the script is triggered from an onclick event on a button. The onclick event immediately hides the button (widget.visible = false) before calling the script with google.script.run so I am hopefully removing the chance to actually click twice!

I am at a loss to explain this, but I am also very new to App Maker and programing.

Does anybody have any insights?

Edit.. Misread the comment section after the reply from Morfinismo!

widget.visible = false;
google.script.run.withFailureHandler(function(err,user){
  console.log("The error was "+err+" and the user was "+user);}).createEvidenceFolder();

The above code is from the Button that handles triggering the server script.


Solution

  • As far as I'm concerned, there should be no issue whatsoever if you decide to hide the button instead of disabling it. Without inspecting the app, it's almost impossible to tell what might be wrong. My best guess is that somewhere in the app there is some code triggering that behavior or less likely, a bug within appmaker's system. I believe the latter is very unlikely because I tried a set up similar to yours and nothing went wrong.

    Either or way, by trying to disable the widget instead of hiding might help:

    console.log("Starting process");
    widget.enabled= false; 
    google.script.run.withFailureHandler(function(err){
        console.log("The error was "+err);}
    ).withSuccessHandler(function(){
        widget.enabled=true; 
        console.log("Finished process");
    }).createEvidenceFolder();