Search code examples
emailgoogle-apps-scriptsharing

Setting the privileges of a new slide created using Google App Script


I created a new slide using a Google App script using

var targetDocument = SlidesApp.create("New Doc");
  var targetDocumentUrl = targetDocument.getUrl();

After making some modifications to the targetDocument (all of which work), I want to email the targetDocumentUrl to a particular person using the script

function sendEmail(recipient,subject,fileUrl){
  var file = DriveApp.getFileById(getIdFromUrl(fileUrl));
  GmailApp.sendEmail(recipient, subject, 'Here is the file' + fileUrl + ' body of message', {
     attachments: [file.getAs(MimeType.PDF)],name: 'Auto Emailer'
 });  
}

While the email happens properly, the Url is not a public url. How do I ensure that the url is accessible by any recipient? Did not find any attribute of the access privileges on google app script.


Solution

  • Solution:

    To create a public-shareable link of a particular file you can do the following:

    function createSlide() {
      
      const name = "New Doc" 
      const targetDocument = SlidesApp.create(name);
      const targetDocumentUrl = targetDocument.getUrl();
      const file = DriveApp.getFileById(targetDocument.getId());
    
    //file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT);
    file.setSharing(DriveApp.Access.DOMAIN_WITH_LINK, DriveApp.Permission.EDIT);  
      
    }
    

    If you are from a G Suite account use:

    file.setSharing(DriveApp.Access.DOMAIN_WITH_LINK, DriveApp.Permission.EDIT);
    

    otherwise you should be able to use:

    file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT);
    

    Requirements:

    Don't forget to activate Drive API: Resouces => Advanced Google services: enable Drive API:

    Enable Drive API

    References:

    Enum Access