通过Google脚本将附件作为Word文档发送 [英] Send attachement as Word document via Google Script

查看:109
本文介绍了通过Google脚本将附件作为Word文档发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Google电子表格我喜欢发送附件。附件是Google Doc。



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



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



我试过了:application / vnd.openxmlformats-officedocument.wordprocessingml.document( http://en.wikipedia.org/wiki/Internet_media_type ),但这是行不通的。



亲切的问候

Stefan

解决方案

Google Apps脚本本身不能导出除pdf外的任何内容,但文档API会执行此操作,因此您可以使用带参数的urlFetch来获取所需内容(不要更改这些参数,请求是匿名的并且可以留下来)。



这里是代码,它不需要太多的解释,除了urlFetch必须被授权(过程不是通常的,你)
$ b

  function emailDocTestasDocx(){
var id ='1I9KIVTLieQbNnmz09zfOBSBNwZ9Tp7B0kfpysaf- ooY'; // 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]});
}

函数googleOAuth_(name,scope){
var oAuthConfig = UrlFetchApp.addOAuthService(name);
oAuthConfig.setRequestTokenUrl(https://www.google.com/accounts/OAuthGetRequestToken?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};
}


Via Google Spreadsheet I like to send an attachment. The attachment is a Google Doc.

I can sent it as PDF file.getAs('application/PDF') -- however I need it as a Word document (or something that opens up in Word).

It is possible to do send an email manually as Word attachment, however this solution requires to do this automatically.

I tried: application/vnd.openxmlformats-officedocument.wordprocessingml.document (http://en.wikipedia.org/wiki/Internet_media_type) but that doesn't work.

Kind regards

Stefan

解决方案

Google Apps Script can't export anything else than pdf natively but the document API does so you can use urlFetch with parameters to get what you want (don't change these parameters, the request is anonymous and can stay so).

Here is the code, it doesn't require too much explanations except that the urlFetch must be authorized (the process is not the usual one, you'll get a second authorization for this script.)

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"};
}

这篇关于通过Google脚本将附件作为Word文档发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆