如何将Google文档文件转换为Excel文件(XLSX)
此图显示更新的代码。 变量“xlsFile”未定义,为什么?如何使用(Googledocs)ScriptEditor将Googledocs文件转换为Excel文件 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 = '#' exportToXls(id) } function 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') } googleOAuth函数(名称、范围){ 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.setConsumerCret('anonymous'); 返回{oAuthServiceName:name,oAuthUseToken:“始终”}; } 功能测试(){ 变量id='#' 出口类毒素(id) } 函数exportToXls(id){ var mute={muteHttpExceptions:true}; var name=DriveApp.getFileById(id).getName() var url='1〕https://docs.google.com/feeds/'; var doc=UrlFetchApp.fetch(url+'download/spreadsheets/Export?key='+id+'&exportFormat=xls',静音).getBlob() var xlsfile=DocsList.createFile(doc).rename(name+'.xlsx')如何将Google文档文件转换为Excel文件(XLSX),excel,google-apps-script,google-sheets,google-docs,xlsx,Excel,Google Apps Script,Google Sheets,Google Docs,Xlsx,此图显示更新的代码。 变量“xlsFile”未定义,为什么?如何使用(Googledocs)ScriptEditor将Googledocs文件转换为Excel文件 function googleOAuth_ (name, scope) { var oAuthConfig = UrlFetchApp.addOAuthService(name); oAuthConfig.setRequestTokenUrl("https://www.google.com/account
} 使用driveapi,我们可以获得比DriveApp方法更多的文件信息。查看导出链接,尤其是
导出链接
。这些链接包含了让我们获得XLS文件的魔力。(为了好玩,在文件
分配后放置一个断点,并检查您必须使用的信息。)
此脚本使用。中提供了一个带有错误检查的更完整版本
/**
*以Excel文件的形式下载具有给定文件id的电子表格。
*使用必须启用的高级驱动器服务。*遇到错误时抛出。
*
*@param{String}fileId驱动器上图纸文件的文件ID。
*/
函数下载XLS(文件ID){
var file=Drive.Files.get(fileId);
var url=file.exportLinks[MimeType.MICROSOFT_EXCEL];
变量选项={
标题:{
授权:“承载者”+ScriptApp.getOAuthToken()
},
muteHttpExceptions:true///获取失败结果
}
var response=UrlFetchApp.fetch(url,选项);
var status=response.getResponseCode();
var result=response.getContentText();
如果(状态!=200){
//根据格式获取其他错误消息信息
if(result.toUpperCase().indexOf(“下面的代码使用了oAuthConfig,现在已被弃用。请改用Mogsdad answer。importXLS函数使用驱动器API,并且仍然可以工作
你会发现很多帖子说这是不可能的,(少数)其他人说你可以…显然你可以
Mogsdad在这里的回答(同时)带来了一个使用驱动器服务的优雅解决方案,这里是另一个解决方案,您可以选择;-)
作为奖励,我添加了相反的过程,如果你需要的话
使用类似于我在测试函数中使用的函数调用使其工作
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
});
}
}
}
要转换为的MIME类型。对于大多数Blob,“application/pdf”是唯一有效的选项。getAs()不支持此MIME类型(Excel)。你可以在这个链接上找到更多信息:这里有一个类似的问题,有最新的答案。比你快5分钟!你只是输入的速度更快,我运行查询时出现的错误。Fehler bei der Anfrage für。Folgender代码wurde zurückgeben:404。Gekürzte Serverantwort:你是如何尝试这个代码的?你完全按照了吗我描述的过程?我只想要一个Google Docs文件保存为Excel文件,这有这么难吗?@MogsdadMogsdad,我认为UrlFetch需要一个令牌来下载转换后的文件。请看一看,谢谢@HenriqueAbreu。完全更新了我的生产脚本。