2014-01-10 57 views
0

通过Google电子表格我喜欢发送附件。该附件是Google文档。通过Google脚本将附件作为Word文档发送

我可以将它发送为PDF file.getAs('application/PDF') - 但是我需要它作为一个Word文档(或者在Word中打开的东西)。

可以通过Word附件手动发送电子邮件,但是此解决方案需要自动执行此操作。

我试过了:application/vnd.openxmlformats-officedocument.wordprocessingml.document(http://en.wikipedia.org/wiki/Internet_media_type)但是不起作用。

亲切的问候

斯特凡

回答

2

谷歌Apps脚本不能出口任何东西比PDF原生,但该文件API并因此您可以使用网址提取与参数,以获得你想要的(不改变这些参数,请求是匿名的并且可以保留)。

下面是代码,它不需要除了网址提取必须经过授权过多的解释(这个过程是不是一般的一个,你会得到第二次授权此脚本。)

function emailDocTestasDocx() { 
    var id = '1I9KIVTLieQbNnmz09zfOBSBNwZ9Tp7B0kfpysaf-ooY';// an example of Google doc 
    var url = 'https://docs.google.com/feeds/'; 
    var doc = UrlFetchApp.fetch(url+'download/documents/Export?exportFormat=doc&format=doc&id='+id, 
           googleOAuth_('docs',url)).getBlob(); 
    var me = Session.getEffectiveUser().getEmail(); 
    MailApp.sendEmail(me, 'test', 'see attachment', {attachments:[doc]}); 
} 

function googleOAuth_(name,scope) { 
    var oAuthConfig = UrlFetchApp.addOAuthService(name); 
    oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope); 
    oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken"); 
    oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken"); 
    oAuthConfig.setConsumerKey('anonymous'); 
    oAuthConfig.setConsumerSecret('anonymous'); 
    return {oAuthServiceName:name, oAuthUseToken:"always"}; 
} 
+0

Thanx Serge。它可以工作,但是我喜欢根据Google Spreadsheet中的电子邮件地址发送电子邮件。当我这样做是行不通的(目标用户没有收到邮件)。任何想法? –

+0

我认为出了什么问题......它似乎很好地工作,谢谢。 –

+0

很高兴听到它,谢谢 –

相关问题