自动将 Google 电子表格导出到 Excel
Automatically export Google Spreadsheet to Excel
我想要一个 Google Apps 脚本,它可以通过使用触发器是
时间驱动(比如每 30 分钟)或事件驱动(对文件进行的每次编辑)。我是基于 Stack Overflow 上的一个老问题 here 但由于函数 oAuthConfig
是
停产了,我想我会根据 OAuth1
库打开一个新线程。
以下是我遵循的所有步骤:
当文件打开时,我转到 Tools -> Script Editor...
然后我按照这些 instructions 安装最新的 OAuth1
库(v.12 和库
我使用的项目密钥是 Mb2Vpd5nfD3Pz-_a-39Q4VfxhMjh3Sh48
)。我也尝试过启用或禁用 [=20=]。
然后我尝试根据 Google API(参见 here, here, here, and here)的所有更改来更改上面的脚本。
我 运行 除了 googleOAuth_(...)
和 g运行 之外的所有函数,他们要求的任何权限。
最后,我从 Resources -> Current project's triggers
创建了一个触发器,它在每次编辑电子表格时调用函数 myOnEdit
并在失败时通过电子邮件通知我。
这是脚本:
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"};
}
所以,代码似乎没有产生任何错误,但有两个问题:
即使创建了文件newfile.xls
,也不是我想要的。基本上,当我在 Excel 上打开它时,我收到消息 "The file format and extension of "newfile.xls" 不匹配。文件可能已损坏或不安全。除非您信任其来源,否则不要打开it.Do you want to open it anyway?",然后等待一段时间,当文件打开时,它看起来像 Excel 格式的 Google 登录页面。
每次我对一行进行编辑时,对应于该行第一列的单元格都会更改为当前时间戳,例如“07/03/16,03:58:30”在
原始电子表格。
部分简答
Google Sheets/Google 驱动器不支持 XLS。
说明
AFAIK .XLS
当前版本的 Google 表格不支持。无论如何,我只是直接在浏览器的地址栏上尝试了具有以下结构的 URL:
https://docs.google.com/spreadsheets/d/spreadsheet-key/export?exportFormat=xls
得到了
要定期在您的 Google 驱动器上生成 XLSX 文件,您可以设置一个定时触发器,从 How to convert a Google Docs-File to an Excel-File (XLSX).[=12= 中引用的要点调用 test_downloadXLS()
函数]
我想要一个 Google Apps 脚本,它可以通过使用触发器是
时间驱动(比如每 30 分钟)或事件驱动(对文件进行的每次编辑)。我是基于 Stack Overflow 上的一个老问题 here 但由于函数 oAuthConfig
是
停产了,我想我会根据 OAuth1
库打开一个新线程。
以下是我遵循的所有步骤:
当文件打开时,我转到
Tools -> Script Editor...
然后我按照这些 instructions 安装最新的OAuth1
库(v.12 和库 我使用的项目密钥是Mb2Vpd5nfD3Pz-_a-39Q4VfxhMjh3Sh48
)。我也尝试过启用或禁用 [=20=]。然后我尝试根据 Google API(参见 here, here, here, and here)的所有更改来更改上面的脚本。
我 运行 除了
googleOAuth_(...)
和 g运行 之外的所有函数,他们要求的任何权限。最后,我从
Resources -> Current project's triggers
创建了一个触发器,它在每次编辑电子表格时调用函数myOnEdit
并在失败时通过电子邮件通知我。
这是脚本:
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"};
}
所以,代码似乎没有产生任何错误,但有两个问题:
即使创建了文件
newfile.xls
,也不是我想要的。基本上,当我在 Excel 上打开它时,我收到消息 "The file format and extension of "newfile.xls" 不匹配。文件可能已损坏或不安全。除非您信任其来源,否则不要打开it.Do you want to open it anyway?",然后等待一段时间,当文件打开时,它看起来像 Excel 格式的 Google 登录页面。每次我对一行进行编辑时,对应于该行第一列的单元格都会更改为当前时间戳,例如“07/03/16,03:58:30”在 原始电子表格。
部分简答
Google Sheets/Google 驱动器不支持 XLS。
说明
AFAIK .XLS
当前版本的 Google 表格不支持。无论如何,我只是直接在浏览器的地址栏上尝试了具有以下结构的 URL:
https://docs.google.com/spreadsheets/d/spreadsheet-key/export?exportFormat=xls
得到了
要定期在您的 Google 驱动器上生成 XLSX 文件,您可以设置一个定时触发器,从 How to convert a Google Docs-File to an Excel-File (XLSX).[=12= 中引用的要点调用 test_downloadXLS()
函数]