Search code examples
google-apps-scriptgoogle-drive-shared-drive

How can I check that my GoogleSpreadsheet is in my drive or in a teamdrive?


Like I said on the title, I would like to run a Google apps script linked to a google spreadsheet only if the Spreadsheet is located on the user's drive because I work with Spreadsheet template located on teamdrive and the rule is that the user must make a copy on their local Drive to run the script. So I would like to know if it's possible with the file id for exemple to check if I am in a teamdrive or not ?

Thanks for your answers


Solution

  • With the fileId itself, you cannot know whether it is part of a shared drive or not.

    However, you can use DriveApp to obtain the file itself, and most importantly, its permission. In order to check that the file is in a personal drive (and owned by the user issuing the request), you have to make sure the permission is in fact OWNER. You can do this with GAS as follows:

    function isInPersonalDrive(fileId) {
      var file = DriveApp.getFileById(fileId);
      return file.getAccess(Session.getActiveUser()) === DriveApp.Permission.OWNER;
    }