Search code examples
javascriptgoogle-picker

Google picker make a copy when uploading


I have replicated the google file picker in a web app.

My question is how would I tell the google picker to make a copy of the file when uploading it to google drive ?

Any help would be much appreciated.


Solution

  • I realised that google picker could not do what I needed so Google App Script to the rescue.

    • Firstly you need to create a new google spreadsheet
    • Under "Tools->Script Editor" add the following code

    Thought trial an error I came up with the following.

    function copyFiles() {
    
       var dApp = DriveApp; // Get the drive app
       var folderIter = dApp.getFoldersByName('parentFolder'); //Get folder my id
       var folder = folderIter.next(); // Get the first folder
       var fileIter = folder.getFiles(); // File iterator
    
       var storageFolder = folder.getFoldersByName('childFolder').next(); // Get target folder
       var subFolderIter = storageFolder.getFiles(); // sub folder File iterator
    
       while(fileIter.hasNext()) { 
    
          var file = fileIter.next();  // Get the current File
          var fileName = file.getName();
    
         // Check if the current file exists in a coped folder
         // If false make a copy  else do noting
         // This check prevents duplicate, Becasue makeCopy generates uniques ID everytime
         if(!storageFolder.getFilesByName(fileName).hasNext()) {
             //Logger.log('Files does NOT exists');
            file.makeCopy(storageFolder);
         } else {
          //Logger.log('Files does exists');
         }
    
       }
    }
    

    Hope this helps anyone who might need.

    Note: You will need to set up triggers for the script to run when required