I want a Google Apps Script that automatically exports a Google Spreadsheet that I have on Google Drive to .xls
or .xlsx
by using a trigger that is either
time-driven (say every 30 minutes) or event-driven (on every edit made on file). I was based on an old question here on Stack Overflow but since the function oAuthConfig
is
discontinued, I think I would open a new thread based on OAuth1
library.
Here are all the steps I followed:
While the file was open I went to
Tools -> Script Editor...
and then I followed these instructions to install the latestOAuth1
library (v.12 and the library project key I used isMb2Vpd5nfD3Pz-_a-39Q4VfxhMjh3Sh48
). I have also tried with theDevelopment mode
enabled or disabled.Then I tried to change the above script based on all the changes of the Google API (see here, here, here, and here).
I ran every function except
googleOAuth_(...)
and granted whatever permissions they asked for.Finally, I created a trigger from
Resources -> Current project's triggers
which calls the functionmyOnEdit
on every edit to the spreadsheet and notifies me on e-mail if it fails.
Here is the script:
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()
DriveApp.createFile(doc).setName('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 service = OAuth1.createService(name);
service.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
service.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
service.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
service.setConsumerKey('anonymous');
service.setConsumerSecret('anonymous');
return {oAuthServiceName:name, oAuthUseToken:"always"};
}
So, it seems that the code doesn't produce any errors but there are 2 problems:
Even if the file
newfile.xls
is created, it is not what I want. Basically, when I open it on Excel I get the message "The file format and extension of "newfile.xls" don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?", then it waits for a while and when the file opens it seems like the Google Login page in Excel format.Each time I make an edit on a row, the cell that corresponds to the first column of that row changes to the current timestamp like "07/03/16, 03:58:30" on the original spreadsheet.