1
votes

Using the following apps script function i can successfully create folders in google drive:

function uploadFileToGoogleDrive(data, file, name, email) {  

    var parentFolderId = "FOLDER_ID";
    var parentFolder = DriveApp.getFolderById(parentFolderId);
    var subfolder;

        try {
    subfolder = parentFolder.getFoldersByName([name, email].join(" ")).next();
    }
    catch(e) {
    subfolder = parentFolder.createFolder([name, email].join(" "));
    }
      var contentType = data.substring(5,data.indexOf(';')),
        bytes = Utilities.base64Decode(data.substr(data.indexOf('base64,')+7)),
        blob = Utilities.newBlob(bytes, contentType, file),
        file = subfolder.createFile(blob);

          file = subfolder.createFile(file, data);
   Logger.log(subfolder);  
      return "OK";
}

i want to access the folder using the following script:

function downloadFile(e) {  
  var parentFolderId = "FOLDER_ID";
  var parentFolder = DriveApp.getFolderById(parentFolderId);

 var subfolderId;
 var subfolders = parentFolder.getFoldersByName([name, email].join(" "));

while (subfolders.hasNext()) {
  var folder = subfolders.next();
  Logger.log(folder.getId());
}
}

appsscript.json for uploadFileToGoogleDrive:

{
  "timeZone": "Europe/Bucharest",
  "dependencies": {
  },
  "webapp": {
    "access": "ANYONE_ANONYMOUS",
    "executeAs": "USER_DEPLOYING"
  },
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8"
}

appsscript.json for downloadFile :

{
  "timeZone": "Europe/Bucharest",
  "dependencies": {
  },
  "webapp": {
    "access": "ANYONE_ANONYMOUS",
    "executeAs": "USER_DEPLOYING"
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/drive.readonly"],
  "runtimeVersion": "V8"
}

The script can access ONLY the folders Created with google drive Web! But cannot get the folders created with other apps script.

Note that the two functions (upload and download) are in separate google apps scripts.

1
The question will be better if 1.Permissions/scopes granted to the each script are provided. 2. The exact error message is quoted instead of any subjective inference of the errorTheMaster
Thanks TheMaster agian for your help, the permission (https://www.googleapis.com/auth/drive.readonly and https://www.googleapis.com/auth/drive are granted for the second script, but when i grant the first it stops working. 2. there is no error, the script keeps waiting for logsuser3140792
Could you edit those details into your question? 2. It should be logged in view>stackdriver logs.TheMaster
In stackdriver logs: No logs are available for this execution.user3140792
You need to clear the filter at the top. Are you visiting view>stackdriver logging? Don't go to view> executions>logs. 3. Also it might take some time before the uploaded files show up. Try adding Logger.log(subfolders.hasNext()) before calling it in whileTheMaster

1 Answers

0
votes

Thanks @alberto vielma, As @TheMaster suggested, my problem was in name and email, a hidden "space" was by mistake in their string values which prevent them to match the folder name. Thanks all..