使用脚本将Google Spreadsheet转换为Excel XLS

让我先说我意识到这个问题之前已经被问过了,真诚的道歉再次提出来。 但是,我已经尝试了一些build议,无法解决这个问题。 我正在尝试编写一个脚本,将其转换为Google Spreadsheet,将其转换为Excel XLS / XLSX格式,然后将转换后的文件作为附件发送给您。 这是试图创build转换后的文件的编码。

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 = DocsList.getFileById('FILE_ID_HERE'); 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') } function autorise(){ // function to call to authorize googleOauth var id = SpreadsheetApp.getActiveSpreadsheet().getId() var url = 'docs.google.com/feeds/'; var doc = UrlFetchApp.fetch(url+'download/documents/Export?exportFormat=html&format=html&id='+id, googleOAuth_('docs',url)).getContentText(); } 

我已经读过授权程序是主要的罪魁祸首。 我已经授权,它似乎运行到下面的行,然后错误。

 var doc = UrlFetchApp.fetch(url+'download/spreadsheets/Export?key='+id+'&exportFormat=xls', googleOAuth_('docs',url)).getBlob() 

错误消息指出以下内容:

 Request failed for returned code 404. Truncated server response: <!DOCTYPE html><html lang="en" ><head><meta name="description" content="Web word processing, presentations and spreadsheets"><link rel="shortcut ic... (use muteHttpExceptions option to examine full response) (line 50, file "Code") 

如果有人有任何想法,我将不胜感激。

您显示的代码(不参考源代码)适用于旧版电子表格。

它不适用于新版本。

阅读驱动器高级服务的文档 ,你会发现所有的解释去做你想要的。

这篇文章应该有助于: 在Google Apps脚本中将工作表导出为XLS时出现OAuth错误