google-apps-scriptpermissionsgoogle-drive-apifile-sharing

Google apps script Permissions on created file in shared folder


I have a script which creates a file in a shared Google Drive folder, this is the script:

  var spr = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Klantenlijst'); 
  var data = spr.getDataRange().getValues(); 
  var klanNumbers = data; //some var declared before this piece of code
  var file = DriveApp.createFile(fileName, JSON.stringify(klanNumbers));

This file needs to be updated frequently, to do so I remove the existing file and create a new one to replace it (with the new data). The problem is that when I try to perform the setTrashed action as a user other than the file owner, this error pops up:

You do not have authorization to perform that action.

Any ideas on how to fix that? :)

Thanks!

EDIT: I can delete the file in drive manually with the other users. I have seen this article but I totally disagree with the conclusion that the problem is "too localized". Look around on Google and you will find cases with the same problem without a decent solution.

Workaround for this moment:

I will not delete this post so people can add other ideas here.


Solution

  • You can only trash files that you own. When you delete the file manually (using the GUI to trash the file), it appears that you've trashed the file, but you are not actually setting the trashed flag on it. Rather, you are removing it from view in your own Google Drive, without affecting anyone else. The owner still sees it shared with you, and any other collaborators are unaffected. In fact, you can still see the file if you search for it by its full name, or use one of the alternate views such as the "Recent" file list, or use the file's URL.

    To get the same effect from a script, use removeFile().

    Here's a utility that will treat a file differently for owners than collaborators, to either trash or remove it.

    /**
     * Remove the given file from view in the user's Drive.
     * If the user is the owner of the file, it will be trashed,
     * otherwise it will be removed from all of the users' folders
     * and their root. Refer to the comments on the removeFile()
     * method:
     *
     *   https://developers.google.com/apps-script/reference/drive/drive-app#removeFile(File)
     *
     * @param {File} file  File object to be trashed or removed.
     */
    function deleteOrRemove( file ) {
      var myAccess = file.getAccess(Session.getActiveUser());
      if (myAccess == DriveApp.Permission.OWNER) {
        // If I own the file, trash it.
        file.setTrashed(true);
      }
      else {
        // If I don't own the file, remove it.
        var parents = file.getParents();
        while (parents.hasNext()) {
          // Remove the file from the current folder.
          parents.next().removeFile(file);
        }
    
        // Remove the given file from the root of the user's Drive.
        DriveApp.removeFile(file);
      }
    }
    

    Example:

    function test_deleteOrRemove() {
      var files = DriveApp.getFilesByName('536998589.mp3');
      while (files.hasNext()) {
        var file = files.next();
        deleteOrRemove( file );
      }
    }