用于通过电子邮件发送 google 电子表格 Excel 版本的 Google 应用脚本 [英] Google apps script to email google spreadsheet excel version
问题描述
我想编写一个应用脚本来通过电子邮件发送我的 Google 电子表格的 Excel 版本.我知道我可以将电子表格另存为 Excel 文件.我不确定是否可以使用该脚本将 excel 版本作为附件通过电子邮件发送出去.这怎么办?
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?
推荐答案
经过 在最近的另一篇文章中回答(Thomas van Latum),我尝试了建议的doc api 并得到一个有趣的结果......这是我使用的测试代码,除了文件是 xlsx 格式,而不是 xls 之外,它运行良好,但这不一定是这些天的问题:
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')
}
注意:如果你不重命名它,它的默认名称是 Export.xlsx
,获取它的 ID 以后使用它可能更有用......所以最后一行可能是这样的:
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();
}
这篇关于用于通过电子邮件发送 google 电子表格 Excel 版本的 Google 应用脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!