The image shows the code who is updated.
The var "xlsFile" is undefined, why? How can I convert the Googledocs-File to an Excel-File with (GoogleDocs) ScriptEditor
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') }
Right-click the Google Sheet and select “Download” from the menu. You will see your computer's standard “save” window to save your Excel file. Here, select a folder to save your file, enter a name for your file, and click “Save.” And your Google Sheet is now available as an Excel file on your computer.
Google Drive has a convenient feature for users who import Excel files frequently, allowing you to automatically convert Excel or XLSX Spreadsheets to Google Sheets. Start by opening Settings from the main Google Drive interface.
Using the Drive API, we can get more information about files than is available through the DriveApp methods. Check out the file data, especially exportLinks
. Those links contain the magic that will let us get an XLS file. (For fun, put a breakpoint after file
is assigned, and check what information you have to play with.)
This script uses the Advanced Drive Service, which must be enabled. A more complete version, with error checking, is available in this gist.
/**
* 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');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With