如何将Google文档转换为Excel文件(XLSX)
该图显示了更新的代码。
var“xlsFile”是未定义的,为什么? 如何使用(GoogleDocs)ScriptEditor将Googledocs-File转换为Excel文件
函数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( '匿名'); oAuthConfig.setConsumerSecret( '匿名'); 返回{oAuthServiceName:name,oAuthUseToken:“always”}; } functiontesting(){ var id ='#' exportToXls(ID) } 函数exportToXls(id){ var mute = {muteHttpExceptions:true}; var name = DriveApp.getFileById(id).getName() var url ='https://docs.google.com/feeds/'; var doc = UrlFetchApp.fetch(url +'download / spreadsheets / Export?key ='+ id +'&exportFormat = xls',mute).getBlob() var xlsfile = DocsList.createFile(doc).rename(name +'。xlsx') }
使用Drive API,我们可以获取有关文件的更多信息,而不是通过DriveApp方法提供的信息。 查看文件数据 ,特别是exportLinks
。 这些链接包含让我们获得XLS文件的魔法。 (为了好玩,在分配file
之后放置一个断点,并检查你必须使用哪些信息。)
此脚本使用必须启用的高级驱动器服务 。 这个要点提供了一个更完整的版本,包括错误检查。
/** * Downloads spreadsheet with given file id as an Excel file. * Uses Advanced Drive Service, which must be enabled. * Throws if error encountered. * * @param {String} fileId File ID of Sheets file on Drive. */ function downloadXLS(fileId) { var file = Drive.Files.get(fileId); var url = file.exportLinks[MimeType.MICROSOFT_EXCEL]; var options = { headers: { Authorization:"Bearer "+ScriptApp.getOAuthToken() }, muteHttpExceptions : true /// Get failure results } var response = UrlFetchApp.fetch(url, options); var status = response.getResponseCode(); var result = response.getContentText(); if (status != 200) { // Get additional error message info, depending on format if (result.toUpperCase().indexOf("<HTML") !== -1) { var message = strip_tags(result); } else if (result.indexOf('errors') != -1) { message = JSON.parse(result).error.message; } throw new Error('Error (' + status + ") " + message ); } var doc = response.getBlob(); //DocsList.createFile(doc).rename(file.title + '.xlsx') // Deprecated DriveApp.createFile(doc).setName(file.title + '.xlsx'); }
下面的代码使用oAuthConfig,现在已经弃用了。 改用Mogsdad回答。 importXLS函数使用驱动器API并仍然有效。
你会发现许多post说这是不可能的,(几个)别人说,你可以…显然你可以!
Mogsdad在这里(同时)的答案带来了一个使用驱动器服务的优雅的解决scheme,这里是另一个,所以你有一个select;-)
作为奖励,我添加了相反的过程,如果你需要的话。
使用类似于我在testing函数中使用的函数调用来使其工作。
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' exportToXls(id) } function exportToXls(id){ var name = DriveApp.getFileById(id).getName() var url = 'https://docs.google.com/feeds/'; var doc = UrlFetchApp.fetch(url+'download/spreadsheets/Export?key='+id+'&exportFormat=xls', googleOAuth_('docs',url)).getBlob() var xlsfile = DocsList.createFile(doc).rename(name+'.xls') } function importXLS(){ var files = DriveApp.searchFiles('title contains ".xls"'); while(files.hasNext()){ var xFile = files.next(); var name = xFile.getName(); if (name.indexOf('.xls')>-1){ var ID = xFile.getId(); var xBlob = xFile.getBlob(); var newFile = { title : name+'_converted', key : ID } file = Drive.Files.insert(newFile, xBlob, { convert: true }); } } }