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

时间:2013-07-08 00:22:25

标签: forms google-apps-script

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

2 个答案:

答案 0 :(得分:0)

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

file.addEditor(emailtogoto);

...或者只是查看它:

file.addViewer(emailtogoto);

如果共享文件夹的名称是“我们的文件夹”,您可能并不意味着使用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}); 

答案 1 :(得分: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]});
}