google-apps-scriptgoogle-drive-apifile-sharing

Google Apps Script, get rid of copy/download option


I am wondering if there's a way to use Google Apps Script to share a Google Doc or Google Sheet, but programmatically disable the copy/download feature for viewers/commentors on the document? When share a Google Doc or Sheet normally, you can go to the settings icon and deselect those options:

enter image description here

But can I do that through Google Apps Script?

As a follow-up to that, is there any way to disable these options for editors too? I'd love to be able to share a document with someone, make them an editor, but prevent them from sharing it with anyone else. Thank you.


Solution

  • In this case, how about using Drive API? I think that you can set them using Drive API. In this answer, the method of "Files: update" of Drive API v3 is used.

    Sample script:

    Before you use this script, please enable Drive API at Advanced Google services.

    function myFunction() {
      const fileId = "###";  // Please set the file ID.
    
      const url = "https://www.googleapis.com/drive/v3/files/" + fileId;
      const res = UrlFetchApp.fetch(url, {
        method: "patch",
        headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()},
        contentType: "application/json",
        payload: JSON.stringify({viewersCanCopyContent: false, writersCanShare: false}),
      });
      console.log(res.getContentText())
      
      // DriveApp.createFile(blob)  // This comment line is used for automatically detecting the scope of "https://www.googleapis.com/auth/drive"
    }
    

    Note:

    References: