Google Apps脚本 - 单独导出所有表格

发布于 2025-01-22 05:39:44 字数 1912 浏览 0 评论 0原文

我已经按照我的要求贴上了以下脚本,但是由于某种原因,它总是在我的电子表格的第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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云巢 2025-01-29 05:39:45

当我看到您的脚本时,我担心以下情况。

  1. 使用urlfetchapp.fetch的连续请求。
  2. 隐藏的床单可能包含在电子表格中。

当这些要点反映在您的脚本中时,如下所示。

修改后的脚本:

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();
  }
  var sheets = ss.getSheets();
  for (var i = 0; i < sheets.length; i++) {
    var sheet = sheets[i];

    var isSheetHidden = sheet.isSheetHidden(); // <--- Added
    if (isSheetHidden) continue; // <--- Added

    if (optSheetId && optSheetId !== sheet.getSheetId()) continue;
    var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf
      + '&gid=' + sheet.getSheetId()   //the sheet's Id
      + '&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 = { muteHttpExceptions: true, headers: { 'Authorization': 'Bearer ' + ScriptApp.getOAuthToken() } }
    var response = UrlFetchApp.fetch(url + url_ext, options);
    var blob = response.getBlob().setName(ss.getName() + ' - ' + sheet.getName() + '.pdf');

    Utilities.sleep(5000); // <--- Added

  }
}
  • 请调整5000 utilities.sleep.sleep(5000)通过测试此脚本,您的实际情况。

参考:

When I saw your script, I'm worried the following situations.

  1. Continuous request using UrlFetchApp.fetch.
  2. The hidden sheets might be included in the Spreadsheet.

When these points are reflected in your script, it becomes as follows.

Modified script:

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();
  }
  var sheets = ss.getSheets();
  for (var i = 0; i < sheets.length; i++) {
    var sheet = sheets[i];

    var isSheetHidden = sheet.isSheetHidden(); // <--- Added
    if (isSheetHidden) continue; // <--- Added

    if (optSheetId && optSheetId !== sheet.getSheetId()) continue;
    var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf
      + '&gid=' + sheet.getSheetId()   //the sheet's Id
      + '&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 = { muteHttpExceptions: true, headers: { 'Authorization': 'Bearer ' + ScriptApp.getOAuthToken() } }
    var response = UrlFetchApp.fetch(url + url_ext, options);
    var blob = response.getBlob().setName(ss.getName() + ' - ' + sheet.getName() + '.pdf');

    Utilities.sleep(5000); // <--- Added

  }
}
  • Please adjust 5000 of Utilities.sleep(5000) for your actual situation by testing this script.

References:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文