导出xls并通过电子邮件发送

时间:2014-03-31 07:41:09

标签: google-apps-script

此代码正常工作至2014年2月20日。 现在它报告错误302«暂时移动»。这个问题似乎是在添加身份验证和重定向,但我没有找到正确的答案。请帮我找到解决此问题的解决方案或解决方法。

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

function sendDailyNewsEmailReport(){
  var emails = "my@mail.net";
  var sourceDocument = SpreadsheetApp.create("DailyReport");
  var sourceDocumentId = sourceDocument.getId();
  sourceDocument.getUrl();
  var id = sourceDocumentId;
  var url = 'https://docs.google.com/feeds/';
  var doc = UrlFetchApp.fetch(url+'download/spreadsheets/Export?key='+id+'&exportFormat=xls', googleOAuth_('docs',url)).getBlob();
  var myMail = "Automated Report " + reportDate + ".";
  MailApp.sendEmail(emails, "DailyReport (" + reportDate + ")" , "", {htmlBody: myMail, attachments: doc.setName("DailyNewsReport(" + reportDate + ").xls")});
}

2 个答案:

答案 0 :(得分:0)

将此作为googleOAuth_ function的最后一行

  return {oAuthServiceName:name, oAuthUseToken:"always",muteHttpExceptions:true};

答案 1 :(得分:0)

我在新的脚本文档中尝试了这段代码:

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", muteHttpExceptions:true};
}

function sendDailyNewsEmailReport(){
  var emails = "enter@your.mail";
  var sourceDocument = SpreadsheetApp.create("DailyReport");
  var sourceDocumentId = sourceDocument.getId();
  sourceDocument.getUrl();
  var id = sourceDocumentId;
  var url = 'https://docs.google.com/feeds/';
  var doc = UrlFetchApp.fetch(url+'download/spreadsheets/Export?key='+id+'&exportFormat=xls', googleOAuth_('docs',url)).getBlob();
  var myMail = "Automated Report TEST";
  MailApp.sendEmail(emails, "DailyReport" , "", {htmlBody: myMail, attachments: doc.setName("DailyNewsReport.xls")});
}

被要求两次授权,然后我再次获得“暂时移动”的文件......