0

I am having trouble adding files to the shared google drive folder as well as not being able to remove specific types of files of any kind within my script. The shared google drive file gives me issues. The code below works for me on my own personal google drive folder for adding and removing CSV's from one google drive folder to any another that I specify. However, it does not work with our general shared google drive folder. I have been authorized permission via the google cloud console API for Drive & Sheets but I am still having permission issues. Any help or clarification on this issue would be greatly appreciated.

Here are two different pieces of code. The first one with function moveFiles() works on my personal drive but not in the shared folders. Here is also some more code that I was playing around with to test the shared folders in a simpler manner. I was able to get the putFile() function to put a newDoc inside a shared google drive folder but not able to remove it.

function moveFiles(source_folder, dest_folder) 
{
// set current destination to grab the folder from
    var currentFolder=DriveApp.getFolderById("1emSsRay_WI_z_qBUpQIoccxQID28FvB0");
// grab only the csv's within current folder
    var docs = DriveApp.getFilesByType(MimeType.CSV);
// set target destination where we will store old csv's that have been processed & Analyzed 
    var destination = DriveApp.getFolderById("1wYG1Gd5z0-nucedSMOBn8ZJs68ZgR8Hb");
// iterate through the csv's files within the currentFolder, add them to the destination and remove them from the current folder
    while (docs.hasNext()) 
    { 
        var doc = docs.next();
        destination.addFile(doc); // get error "Cannot use this operation on a shared drive item(line 13, file "SharedDriveMove")
        currentFolder.removeFile(doc);
    }
}

function putFile()
{
    var newDoc = DocumentApp.create('Testing Team Drive MoveTo').getId();
    var file = DriveApp.getFileById(newDoc);
    var moveFile = DriveApp.getFolderById('1emSsRay_WI_z_qBUpQIoccxQID28FvB0').addFile(file);
}

function takeFile()
{
    var filesIterator = DriveApp.getFilesByName('Testing Team Drive MoveTo');
    while (filesIterator.hasNext()) 
    {
        var file = filesIterator.next();
    }
    var cleanup = DriveApp.getFolderById('1wYG1Gd5z0-nucedSMOBn8ZJs68ZgR8Hb').addFile(file,{"supportsAllDrives": true}); // get error "Cannot find method addFile(File,Object).(line 15,file"Code")

    var moveFile = DriveApp.getFolderById('1emSsRay_WI_z_qBUpQIoccxQID28FvB0').removeFile(file,{"supportsAllDrives": true});
}
bronesto
  • 11
  • 2
  • Shared Drives have their own set of rules. Check the official Google Sheets API (V2) documentation for details, especially the sections "[Overview](https://developers.google.com/drive/api/v2/about-shareddrives)", "[Implement Shared Drive support](https://developers.google.com/drive/api/v2/enable-shareddrives)", and "[Manage Shared Files](https://developers.google.com/drive/api/v2/manage-shareddrives)". – TheAddonDepot Sep 10 '19 at 23:10

1 Answers1

0

DriveApp is an Apps Script class whereby it has it's limitations among Drive. Also, as the documentation says:

This method does not delete the file, but if a file is removed from all of its parents, it cannot be seen in Drive except by searching for it or using the "All items" view.

You should do it with the Drive API instead:

Use "supportsAllDrives", otherwise it won't find the file if it's in a Shared Drive (until this is deprecated in 2020).

Drive.Files.remove("your file ID", {"supportsAllDrives": true});

You also have to authorize the Drive.File scope.

Jescanellas
  • 2,555
  • 2
  • 9
  • 20
  • thank you so much for the reply. Here's a little more details about what I'm doing. I'm using Google App Script to do all of this within google sheets. So importing all the data from a CSV into sheets within my google drive. I was looking into the Drive API and it doesn't look like I can use google app script for that kind of process? It seems like a little overkill for the problem that I'm trying to solve. I'm not sure if there is another way how to do this purely within App Script? Trying to still use the DriveApp class but if not possibly open to other suggestions. Thanks SM! – bronesto Sep 11 '19 at 23:29
  • You can do it by using DriveApp to get the CSV file, and then convert it to String with [parseCSV](https://developers.google.com/apps-script/reference/utilities/utilities#parsecsvcsv) to then copy the data to a Sheet. But you should open a new question about this :) – Jescanellas Sep 12 '19 at 07:28