首页 文章

用于发送电子邮件的Google脚本

提问于
浏览
0

有没有办法通过谷歌脚本创建文档并自动将其放在预先指定的共享文件夹中,以便其他人可以打开它?我正在使用表单电子表格中的结果来制作此文档,但我在使用Google脚本的这一方面时遇到了问题 .

2 回答

  • 0

    您可以向电子邮件的收件人授予编辑文件的权限:

    file.addEditor(emailtogoto);
    

    ......或者只是查看它:

    file.addViewer(emailtogoto);
    

    如果共享文件夹的名称是'OUR FILE FOLDER',则可能并不意味着使用 DocsList.getFolderById() . 您应该使用DocsList.getFolder(path)代替:

    var sharedFolder = DocsList.getFolder('OUR FILE FOLDER');
    

    您无法直接附加Google文档,但可以附加pdf版本 .

    var pdf = file.getAs("application/pdf"); 
    MailApp.sendEmail(emailtogoto, 'Thank you for your using this form!  Here is your file', file.getName()+'\n'+file.getUrl(), {attachments: pdf});
    
  • 0

    我前一段时间编写了这个示例脚本来演示DocumentAppDocsList服务中可用的所有功能,我认为它包含了您描述的用例所需的所有内容(请参阅代码中的注释)

    function testDocCreate(){
      try{
        var folder = DocsList.getFolder("testFolder"); // check if shared folder already exist, if not just do it !
      }catch(err){
        folder = DocsList.createFolder("testFolder").addEditor('editorEmailAdress');// or addViewer ...
      }
      // now that our shared testfolder exists we van create files in it that will inherit all sharing properties
      var fileId = DocumentApp.create('testFileName').getId();// create as a text document
      var fileUrl = DocsList.getFileById(fileId).getUrl();
      var file = DocsList.getFileById(fileId);
    
      file.addToFolder(folder); // share it by moving into the right folder
      file.removeFromFolder(DocsList.getRootFolder());// remove the newly created file from your root folder so it won't appear twice
    
      Logger.log(fileUrl); // send this url to your user and eventually add a pdf copy to your email like below
    
      var pdf = DocsList.getFileById(fileId).getAs('application/pdf').getBytes(); // this is the pdf file
    
      var attach_to_send = {fileName: 'testFileName.pdf',content:pdf, mimeType:'application/pdf'}; // create an attachment object
    
      MailApp.sendEmail('editorEmailAdress', "here is the document I've shared with you (url & pdf copy)", 'Your doc available here : '+fileUrl, {attachments:[attach_to_send]});
    }
    

相关问题