Google Apps脚本 - 单独导出所有表格
我已经按照我的要求贴上了以下脚本,但是由于某种原因,它总是在我的电子表格的第5或第6页之后终止,无论工作表的内容实际上是什么...任何人都可以发现错误或改进脚本通常是为了使其正常工作?
这是我收到的错误msg:
fehler
异常:
function savePDFs( optSSId, optSheetId ) {
var ss = (optSSId) ? SpreadsheetApp.openById(optSSId) : SpreadsheetApp.getActiveSpreadsheet();
var url = ss.getUrl().replace(/edit$/,'');
var parents = DriveApp.getFileById(ss.getId()).getParents();
if (parents.hasNext()) {
var folder = parents.next();
}
else {
folder = DriveApp.getRootFolder();
}
// Get array of all sheets in spreadsheet
var sheets = ss.getSheets();
// Loop through all sheets, generating PDF files.
for (var i=0; i<sheets.length; i++) {
var sheet = sheets[i];
// If provided a optSheetId, only save it.
if (optSheetId && optSheetId !== sheet.getSheetId()) continue;
//export parameters
var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf
+ '&gid=' + sheet.getSheetId() //the sheet's Id
// following parameters are optional...
+ '&size=A4' // paper size
+ '&portrait=true' // orientation, false for landscape
+ '&scale=4' // 1= Normal 100% / 2= Fit to width / 3= Fit to height / 4= Fit to Page
+ '&sheetnames=false&printtitle=false&pagenumbers=false' //hide optional headers and footers
+ '&gridlines=false' // hide gridlines
+ '&fzr=false'; // do not repeat row headers (frozen rows) on each page
var options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
}
}
var response = UrlFetchApp.fetch(url + url_ext, options);
var blob = response.getBlob().setName(ss.getName() + ' - ' + sheet.getName() + '.pdf');
//the pdf is named according to the sheet - this is important for me!
}
}
I've adatped the following script to my demands, but for some reason it always terminates after the 5th or 6th sheet of my spreadsheet, no matter what the content of the sheet actually is...can anyone spot an error or improve the script generally to get it working stable?
This is the error msg I'm receiving:
Fehler
Exception: Request failed for https://docs.google.com returned code 429. Truncated server response: <meta name="viewport" ... (use muteHttpExceptions option to examine full response)
savePDFs @ savePDFs.gs:59
function savePDFs( optSSId, optSheetId ) {
var ss = (optSSId) ? SpreadsheetApp.openById(optSSId) : SpreadsheetApp.getActiveSpreadsheet();
var url = ss.getUrl().replace(/edit$/,'');
var parents = DriveApp.getFileById(ss.getId()).getParents();
if (parents.hasNext()) {
var folder = parents.next();
}
else {
folder = DriveApp.getRootFolder();
}
// Get array of all sheets in spreadsheet
var sheets = ss.getSheets();
// Loop through all sheets, generating PDF files.
for (var i=0; i<sheets.length; i++) {
var sheet = sheets[i];
// If provided a optSheetId, only save it.
if (optSheetId && optSheetId !== sheet.getSheetId()) continue;
//export parameters
var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf
+ '&gid=' + sheet.getSheetId() //the sheet's Id
// following parameters are optional...
+ '&size=A4' // paper size
+ '&portrait=true' // orientation, false for landscape
+ '&scale=4' // 1= Normal 100% / 2= Fit to width / 3= Fit to height / 4= Fit to Page
+ '&sheetnames=false&printtitle=false&pagenumbers=false' //hide optional headers and footers
+ '&gridlines=false' // hide gridlines
+ '&fzr=false'; // do not repeat row headers (frozen rows) on each page
var options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
}
}
var response = UrlFetchApp.fetch(url + url_ext, options);
var blob = response.getBlob().setName(ss.getName() + ' - ' + sheet.getName() + '.pdf');
//the pdf is named according to the sheet - this is important for me!
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我看到您的脚本时,我担心以下情况。
urlfetchapp.fetch
的连续请求。当这些要点反映在您的脚本中时,如下所示。
修改后的脚本:
5000
utilities.sleep.sleep(5000)
通过测试此脚本,您的实际情况。参考:
When I saw your script, I'm worried the following situations.
UrlFetchApp.fetch
.When these points are reflected in your script, it becomes as follows.
Modified script:
5000
ofUtilities.sleep(5000)
for your actual situation by testing this script.References: