2013-07-08 69 views
0

有沒有辦法通過谷歌腳本創建一個文件,並自動把它放在一個預先指定的共享文件夾,以便其他人可以打開它?我使用表單電子表格的結果來製作這個文檔,但是我在Google腳本的這一方面遇到了麻煩。谷歌腳本發送電子郵件

回答

0

,您可以給電子郵件權限的收件人編輯文件:

file.addEditor(emailtogoto); 

...或只是查看:

file.addViewer(emailtogoto); 

如果名的共享文件夾的是'我們的文件夾',你可能不想用DocsList.getFolderById()。您應該使用DocsList.getFolder(path)代替:

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

您不能直接連接一個谷歌文檔,但您可以附加一個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]}); 
}