Announcements
This site is in read only until July 22 as we migrate to a new platform; refer to this community post for more details.

How to check the success/failure of a file creation on Google Drive by a bot?

Hello,

One of my bot is in charge of creating a structure of several folders in Google Drive.

The idea is to copy an existing file to a new (as yet non-existent) location. This creates a folder and copies the file (to and from Google Drive).

Sometimes this process ends with an error like this one:

Stop executing after step Create folder RD0-DR, because the request took too long to process.
Execution Time (In seconds) : 1077

The execution time is variable, from 567 to 1077 seconds.

Between 20 and 30 folders are created every day and today 3 folders have not been created. In previous weeks/months, we had between 1 and 2 folders not created per week. It was still possible to manage this "by hand". This is no longer possible.

My question is to be able to run a regular check, by a bot, for the folders that have not been created. But how can I do this control from Appsheet in google drive?

Thank you for your suggestions.

Solved Solved
0 2 959
1 ACCEPTED SOLUTION

At last, I decide to use Apps Script to check the structure in the folder of my structure. Currently, this main folder contains around 3000 folders. Each folder need to have 3 sub folders.

In my table (a Google Spreadsheet), every item has a unique name (RFP) and it is possible to find/search the folder with this name, and (it is important) a date of creation of the item.

We have two solutions. First to call directly, from Appsheet, the Apps Script with the list of folder name to check. Second to call a scheduler (in Apps Script) every 15 minutes or every hour.

Here is the part of the script in charge of the check in Google Drive. (the main folder is referenced by an ID)

/* 
 * To be call directly from Appsheet
 * 
 */
function checkTheExistingProjectFolder(listRFPprojects) {

  // Get the folder of projects of Appsheet XXX
  var projectsFolder = DriveApp.getFolderById( projectsFolderGoogleDriveID );
  

  console.log("List of RFP project to check: " + listRFPprojects);
  // Create the list of projects number to check as folder 
  var data = new Array;
  if ( listRFPprojects != null) {
    //var lstRfp  = listRFPprojects.split(',');
    //for (var j = 0; j < lstRfp.length; j++) {
    //  data.push( lstRfp[j] );
    //}
    data = listRFPprojects.split(',');
    
  }
  
  // Search, one by one the project folder (this method is more quick than to get all existing subfolder)
  for (var i = 0; i < data.length; i++) {

    // search the exact name of one project 
    var listFolderFound = projectsFolder.searchFolders( "title = '"+ data[i] +"'" );
    
    // check if the project exists
    if (listFolderFound.hasNext()){
      //console.log(data[i] + "- already exist");
      var childFolder = listFolderFound.next(); // Get the current existing project folder object
      findSubStructure_(childFolder); // Check sub folder and create if necessary

    } else {
      console.log("Project folder not exists: " + data[i] );
      var newFolderID = projectsFolder.createFolder( data[I] ).getId();
      var childFolder = DriveApp.getFolderById(newFolderID); // Get the new project folder object
      console.log("Project folder created: " + data[i]);
      findSubStructure_(childFolder); // Check sub folder and create if necessary

    }

  }

  console.log("End of check.");
}

/* 
 * Test the sub folder of a project
 * Hidden function 
 */
function findSubStructure_(objFolder) {

  // Get the list of all existing folders
  var listFolders = objFolder.getFolders();
  
  // create a list of name of existing folder
  var childNames = [];
  while (listFolders.hasNext()) {
    var child = listFolders.next();
    childNames.push([child.getName()]);
  }
   
  // check the structure
  for (var j = 0; j < subProjectFolders.length; j++) {

    // check if the required folder is in the list of existing folder
    if (childNames.flat().includes(subProjectFolders[j])) {
      //console.log(subData[j] + " - already exist");

    } else {
      console.log(" - Sub folder not exists:" + subProjectFolders[j]);
      // Create the new required folder in the current folder of the checked project
      var newFolderID = objFolder.createFolder( subProjectFolders[j] ).getId();
      DriveApp.getFolderById(newFolderID); // This ask Google to verify him job !!!
      console.log(" - Sub folder created:" + subProjectFolders[j]);

    }

  } 
}

/*
 * To be call from the App Script scheduler
 * 
 */
function scheduledCheck(){
  extractDataFromSpreadsheedKSA_();
}

 

View solution in original post

2 REPLIES 2

At last, I decide to use Apps Script to check the structure in the folder of my structure. Currently, this main folder contains around 3000 folders. Each folder need to have 3 sub folders.

In my table (a Google Spreadsheet), every item has a unique name (RFP) and it is possible to find/search the folder with this name, and (it is important) a date of creation of the item.

We have two solutions. First to call directly, from Appsheet, the Apps Script with the list of folder name to check. Second to call a scheduler (in Apps Script) every 15 minutes or every hour.

Here is the part of the script in charge of the check in Google Drive. (the main folder is referenced by an ID)

/* 
 * To be call directly from Appsheet
 * 
 */
function checkTheExistingProjectFolder(listRFPprojects) {

  // Get the folder of projects of Appsheet XXX
  var projectsFolder = DriveApp.getFolderById( projectsFolderGoogleDriveID );
  

  console.log("List of RFP project to check: " + listRFPprojects);
  // Create the list of projects number to check as folder 
  var data = new Array;
  if ( listRFPprojects != null) {
    //var lstRfp  = listRFPprojects.split(',');
    //for (var j = 0; j < lstRfp.length; j++) {
    //  data.push( lstRfp[j] );
    //}
    data = listRFPprojects.split(',');
    
  }
  
  // Search, one by one the project folder (this method is more quick than to get all existing subfolder)
  for (var i = 0; i < data.length; i++) {

    // search the exact name of one project 
    var listFolderFound = projectsFolder.searchFolders( "title = '"+ data[i] +"'" );
    
    // check if the project exists
    if (listFolderFound.hasNext()){
      //console.log(data[i] + "- already exist");
      var childFolder = listFolderFound.next(); // Get the current existing project folder object
      findSubStructure_(childFolder); // Check sub folder and create if necessary

    } else {
      console.log("Project folder not exists: " + data[i] );
      var newFolderID = projectsFolder.createFolder( data[I] ).getId();
      var childFolder = DriveApp.getFolderById(newFolderID); // Get the new project folder object
      console.log("Project folder created: " + data[i]);
      findSubStructure_(childFolder); // Check sub folder and create if necessary

    }

  }

  console.log("End of check.");
}

/* 
 * Test the sub folder of a project
 * Hidden function 
 */
function findSubStructure_(objFolder) {

  // Get the list of all existing folders
  var listFolders = objFolder.getFolders();
  
  // create a list of name of existing folder
  var childNames = [];
  while (listFolders.hasNext()) {
    var child = listFolders.next();
    childNames.push([child.getName()]);
  }
   
  // check the structure
  for (var j = 0; j < subProjectFolders.length; j++) {

    // check if the required folder is in the list of existing folder
    if (childNames.flat().includes(subProjectFolders[j])) {
      //console.log(subData[j] + " - already exist");

    } else {
      console.log(" - Sub folder not exists:" + subProjectFolders[j]);
      // Create the new required folder in the current folder of the checked project
      var newFolderID = objFolder.createFolder( subProjectFolders[j] ).getId();
      DriveApp.getFolderById(newFolderID); // This ask Google to verify him job !!!
      console.log(" - Sub folder created:" + subProjectFolders[j]);

    }

  } 
}

/*
 * To be call from the App Script scheduler
 * 
 */
function scheduledCheck(){
  extractDataFromSpreadsheedKSA_();
}

 

Here, the follow-up of this morning :

12 déc. 2023, 07:46:37DébogageList of RFP project to check: 233993, 234610, 234789, 234807, 234403, 233915, 234645, 234452, 234978, 234570, 234910, 234758, 234797, 232545, 235084, 234859, 235243, 231043,235073,235247,233573,235083,233633,233994,234233
12 déc. 2023, 07:46:52Débogage234233 - Sub folder not exists:Reports
12 déc. 2023, 07:46:53Débogage234233 - Sub folder created:Reports
12 déc. 2023, 07:46:53Débogage234233 - Sub folder not exists:Client Documents
12 déc. 2023, 07:46:55Débogage234233 - Sub folder created:Client Documents
12 déc. 2023, 07:46:55DébogageEnd of check.
Top Labels in this Space