确实不是!可能是因为oAuth功能没有得到正确的授权,也可能是因为简单的onEdit不允许做这种操作。
您必须创建一个可安装的触发器(menu>ressource>current trigger>create)。
试试下面的这个脚本并运行这个authorize
函数。
我还更改了一些细节:timeZone
直接取自电子表格,id
也取自活动电子表格。
另请注意,新创建的 XLSX 不会覆盖任何以前的文件,您将获得许多具有相同名称的文件!如果您只想保留最新版本,那么您应该自己处理,获取所有文档名称“新文件”并file.setTrashed(true)
在创建新文件之前删除它们。
这就像这两行代码一样简单:
var oldVersions = DocsList.find('newfile.xls');
for(var d in oldVersions){oldVersions[d].setTrashed(true)};
编码 :
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"};
}
编辑:根据您的评论,这是一个仅每 30 秒保存一次的版本(如果未进行编辑,则保存更多)。如有必要,您可以轻松地将时间值更改为另一个间隔。
重新运行授权函数以初始化 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"};
}