Search code examples
google-apps-scriptgoogle-drive-apigoogle-appsgoogle-sitesgoogle-forms

Google Apps for Work - How to create an upload forms that send file to SuperAdmin Google Drive


I am in the 30day try period of Google Apps for Work and i need to create a FORM accesible from all User of Domain, but I can't do it with Google Forms or Google Sites so I follow this guide

I try this with my personal account and it works, but when i repeat the process with GApps SuperAdmin accounts it doesn't work, I got this error: Exception: Access Denied: DriveApp.

Google Script

function doGet(e) {
    return HtmlService.createHtmlOutputFromFile('form.html');
}

function uploadFiles(form) {
  try {

    var dropbox = "Student Files";
    var folder, folders = DriveApp.getFoldersByName(dropbox);

    if (folders.hasNext()) {
    folder = folders.next();
    } 
    else {
        folder = DriveApp.createFolder(dropbox);
    }

    var blob = form.myFile;    
    var file = folder.createFile(blob);    
    file.setDescription("Uploaded by " + form.myName);

    return "File uploaded successfully " + file.getUrl();
  } 
  catch (error) {
      return error.toString();
  }

}

HTML

    <form id="myForm">
    <input type="text" name="myName" placeholder="Your name..">
    <input type="file" name="myFile">
    <input type="submit" value="Upload File" 
           onclick="this.value='Uploading..';
                    google.script.run.withSuccessHandler(fileUploaded)
                    .uploadFiles(this.parentNode);
                    return false;">
    </form>

    <div id="output"></div>

    <script>
       function fileUploaded(status) {
          document.getElementById('myForm').style.display = 'none';
          document.getElementById('output').innerHTML = status;
       }
    </script>

  <style>
     input { display:block; margin: 20px; }
  </style>

This is My "Deploy as Web App" configuration: here

I am open to every advice it is not necessary to use Google Script if there is a more reliable road


Solution

  • I tried the code, and it worked for me after turning on the Google drive apps @Greg mentioned in one of the comments.

    For this setting in the Admin console go to: Google Apps > Drive > General Settings > and then check the box of "Allow users to install Google Drive apps"

    Also with this code the files will be stored in the current user's drive and not in the admin's drive. To fix it you have to to the following:

    • Create the file in the admin's drive
    • Copy the folder ID
    • In the Appscript use the method 'var folder = DriveApp.getFolderById('Folder ID');' -you won't need the code to create the folder as it's already done here
    • The folder need to be shared with the users
    • The users will need to request permission access

    Hope that helps.