谷歌应用程序脚本,以电子邮件谷歌电子表格Excel版本 [英] Google apps script to email google spreadsheet excel version

查看:83
本文介绍了谷歌应用程序脚本,以电子邮件谷歌电子表格Excel版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个应用程序脚本,通过电子邮件发送我的Google电子表格的Excel版本。我知道我可以将电子表格保存为Excel文件。我不确定是否可以使用脚本将Excel版本作为附件发送出去。如何做到这一点?

解决方案

在另一篇近期文章中回答 Thomas van Latum ),我尝试了建议的doc api并得到了一个有趣的结果......这里是我使用的测试代码,除了文件是xlsx格式,不是xls,而是这些日子不一定是个问题:

  function googleOAuth_(name,scope){
var oAuthConfig = UrlFetchApp.addOAuthService名称);
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};


function test(){
var id ='spreadsheet_ID'
var url ='https://docs.google.com/feeds/';
var doc = UrlFetchApp.fetch(url +'download / spreadsheets / Export?key ='+ id +'& exportFormat = xls',
googleOAuth _('docs',url))。getBlob()
DocsList.createFile(doc).rename('newfile.xls')
}

注意:如果你不重命名它,它的默认名称是 Export.xlsx ,它可能更有用得到它的ID以后使用它...
,所以最后一行可以是这样的:

  var xlsfileID = DocsList.createFile(doc).getId() 






编辑:触发授权程序,尝试像这样的小函数,从脚本编辑器运行它

$ p $函数autorise(){
//函数调用授权googleOauth
var id = SpreadsheetApp.getActiveSpreadsheet()。getId();
var url ='https://docs.google.com/feeds/';
var doc = UrlFetchApp.fetch(url +'download / documents / Export?exportFormat = html& format = html& id ='+ id,
googleOAuth _('docs',url))。getContentText() ;
}


I would like to write an apps script to email an excel version of my Google Spreadsheet. I know I can save the spreadsheet as an Excel file. I am not sure if I can use the script to email the excel version out as an attachment. How can this be done?

解决方案

After an answer on another recent post (Thomas van Latum), I tried the suggested doc api and get an interesting result... here is the test code I used and that is working nicely except the file is in xlsx format, not in xls but this is not necessarily an issue these days :

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 test(){
var id = 'spreadsheet_ID'
var url = 'https://docs.google.com/feeds/';
var doc = UrlFetchApp.fetch(url+'download/spreadsheets/Export?key='+id+'&exportFormat=xls',
googleOAuth_('docs',url)).getBlob()
DocsList.createFile(doc).rename('newfile.xls')
}

note : if you don't rename it, its default name is Export.xlsx , it might be more usefull to get its ID to use it later... so the last line could be like this instead :

var xlsfileID = DocsList.createFile(doc).getId()


EDIT : to trigger the authorization process, try a small function like this, run it from the script editor

function autorise(){
  // function to call to authorize googleOauth
  var id=SpreadsheetApp.getActiveSpreadsheet().getId();
  var url = 'https://docs.google.com/feeds/';
  var doc = UrlFetchApp.fetch(url+'download/documents/Export?exportFormat=html&format=html&id='+id,
                              googleOAuth_('docs',url)).getContentText();  
}

这篇关于谷歌应用程序脚本,以电子邮件谷歌电子表格Excel版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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