Any way to share google docs programmatically?

故事扮演 提交于 2019-12-04 15:45:23

You can do it using DocsList.addViewers(viewersEmails[])

here is an example that takes a list of viewers as parameter, add them as viewers and sends an email with the link :

function addviewers(viewers) { // pass emails as a string in CSV
  var viewerssArray = viewers.replace(/, /g,",").split(",");// the 'replace' is there to remove any spaces that could be in the string
  Logger.log(viewersArray.length+'  :  '+viewersArray);
  var file = DocsList.getFileById('your doc ID').addViewers(viewersArray);
  var docurl=file.getUrl();    
      for (nn=0;nn<viewersArray.length;++nn){
        MailApp.sendEmail(viewersArray[nn], 'your document', docurl);
          }
}

I first used 'editor' as variable name, now changed to 'viewers' to avoid confusion.

If you really need to include everybody in your domain to get access I guess the best solution would be to create a group with all users to simplify the process.

EDIT : there is maybe a simpler solution that is to move the file in a shared folder : all items in a folder have the same sharing parameters as the folder itself.

example :

function sharebyFolder(){
    var file = DocsList.getFileById('docId');
    var folder = DocsList.getFolderById('shared folder Id');
    file.addToFolder(folder)
      }    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!