dav*_*872 4 excel google-docs xlsx google-sheets google-apps-script
该图显示了更新的代码.

var"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')
}
Mog*_*dad 10
使用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');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7451 次 |
| 最近记录: |