There is nothing wrong with your code.
Google can not guarantee 100% service availability.
Explanation:
The error tells you that you need to wait a little bit before your execute that function again.
A potential workaround solution would be to use a try...catch statement and within the catch
brackets include a code to automatically execute the function after some time.
For example, you can create a time-driven trigger that executes CreateDirectory()
after some time (e.g. 1 minute) if the function failed the first time.
Solution:
In the following solution the logic is to manually execute the toRun()
function. The latter will try to execute CreateDirectory()
. If an error occurs, it will create a time-driven trigger that will execute CreateDirectory()
after a minute (modify that to your needs). The clearTrigger()
function is responsible for clearing all the previous triggers (if there are any) that are created because of that code.
function toRun(){
try{
CreateDirectory();
}
catch (e){
clearTrigger(); // clear previous created triggers
ScriptApp.newTrigger("CreateDirectory")
.timeBased()
.after(1 * 60 * 1000) // execute CreateDirectory after 1 minute
.create();
}
}
function CreateDirectory() {
var folderName="Example";
var Directory;
var fi = DriveApp.getFoldersByName(folderName);
if (fi.hasNext()) {
Directory = fi.next();
} else {
Directory = DriveApp.createFolder(folderName);
}
}
function clearTrigger(){
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
if (triggers[i].getHandlerFunction() == "CreateDirectory") {
ScriptApp.deleteTrigger(triggers[i]);
}
}
}