Let me first say I realize this question has been asked before, so sincere apologies for bringing it up again. However, I have tried some of the suggestion and can't get it to work out. I am trying to write a script that will take a Google Spreadsheet, convert it to Excel XLS/XLSX format and then email the converted file as an attachment. Here's the coding that tries to create the converted file.
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();
}
I have read that the authorizing procedure is the primary culprit. I have granted authorization and it appears to run to the line below and then errors.
var doc = UrlFetchApp.fetch(url+'download/spreadsheets/Export?key='+id+'&exportFormat=xls',
googleOAuth_('docs',url)).getBlob()
The error message states the following:
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")
If anyone has any ideas, I would greatly appreciate it.