It was not indeed ! probably because the oAuth function didn't get the right authorization and also because the simple onEdit is not allowed to do this kind of operation.
You'll have to create an installable trigger (menu>ressource>current trigger>create).
Try this script below and run the authorize
function.
I changed also a few details : timeZone
is taken directly from the spreadsheet and id
is also taken from the active spreadsheet.
Note also that the newly created XLSX will not overwrite any previous file, you'll get many documents with the same name ! If you want to keep only the latest version then you should take care of that by yourself, getting all the docs names 'new file' and delete them using file.setTrashed(true)
before you create the new one.
This would be as simple as these 2 lines of code :
var oldVersions = DocsList.find('newfile.xls');
for(var d in oldVersions){oldVersions[d].setTrashed(true)};
The code :
function myOnEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var r = s.getActiveCell();
if( r.getColumn() != 1 ) { //checks the column
var row = r.getRow();
var time = new Date();
time = Utilities.formatDate(time, ss.getSpreadsheetTimeZone(), "MM/dd/yy, hh:mm:ss");
var id = ss.getId();
s.getRange('A' + row.toString()).setValue(time);
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 authorise(){
// function to call to authorize googleOauth
var id=SpreadsheetApp.getActiveSpreadsheet().getId();
var url = 'https://docs.google.com/feeds/';
var doc = UrlFetchApp.fetch(url+'download/spreadsheets/Export?key='+id+'&exportFormat=xls',
googleOAuth_('docs',url)).getBlob()
}
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"};
}
EDIT : following your comment, here is a version that saves only every 30 sec (or more if no edit are done). You can easily change the time value to another interval if necessary.
Re-run the authorize function to initialize the scriptProperty.
function myOnEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var r = s.getActiveCell();
if( r.getColumn() != 1 ) { //checks the column
var row = r.getRow();
var time = new Date();
time = Utilities.formatDate(time, ss.getSpreadsheetTimeZone(), "MM/dd/yy, hh:mm:ss");
var id = ss.getId();
s.getRange('A' + row.toString()).setValue(time);
var lastSaveTime = new Date(Utilities.jsonParse(ScriptProperties.getProperty('exportTime')));
var now = new Date().getTime();
Logger.log(now - lastSaveTime.getTime())
if (now - lastSaveTime.getTime() > 60000){ // delete every minute
var oldVersions = DocsList.find('newfile.xls');
for(var d in oldVersions){oldVersions[d].setTrashed(true)};
}
if (now - lastSaveTime.getTime() > 30000){ // save every 30"
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')
ScriptProperties.setProperty('exportTime',Utilities.jsonStringify(new Date()));
}
}
}
function authorise(){
// function to call to authorize googleOauth + initialize the TIMER
ScriptProperties.setProperty('exportTime',Utilities.jsonStringify(new Date()));
var id = SpreadsheetApp.getActiveSpreadsheet().getId();
var url = 'https://docs.google.com/feeds/';
var doc = UrlFetchApp.fetch(url+'download/spreadsheets/Export?key='+id+'&exportFormat=xls',
googleOAuth_('docs',url)).getBlob()
}
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"};
}