有没有办法通过谷歌脚本创建文档并自动将其放入预先指定的共享文件夹中,以便其他人可以打开它?我正在使用表单电子表格中的结果来制作此文档,但我在 google 脚本的这一方面遇到了麻烦。
问问题
300 次
2 回答
0
我不久前编写了这个示例脚本来演示DocumentApp和DocsList服务中可用的所有功能,我认为其中包含了您描述的用例所需的所有内容(请参阅代码中的注释)
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]});
}
于 2013-07-12T07:33:42.187 回答
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});
于 2013-07-10T01:25:11.567 回答