如何在Google脚本中诊断“ 429错误”

时间:2019-06-21 12:44:03

标签: google-apps-script google-drive-api

我的脚本遍历一个列表,在每个索引处它都将工作表中的工作表另存为PDF,然后将其保存到我的google驱动器文件夹中。列表中有7个人。问题是我的脚本在导出第6个PDF之后引发429错误。我正在尝试找出解决该错误的方法,但是我对Google脚本编写还很陌生,因此我需要一些正确方向的帮助。

Error Image

function ToPortrait() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var validation_sheet = ss.getSheetByName('ValidDSM');
  var lastRow = validation_sheet.getLastRow();
  var inputs = ss.getSheetByName('ValidDSM').getRange('A2:A'+lastRow).getValues();
  var sheet2 = ss.getSheetByName('DSM');
  var tab = ss.getSheetByName('DSMD');

  var url = ss.getUrl().replace(/edit$/,'');
  var sheets = ss.getSheets();
  var optSheetId = 1123194386;

  var options = {
      headers: {
        'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken()
      }
    }


  // Loop through List of 7
  for (var i = 0; i < inputs.length; i++){
    sheet2.getRange('B3').setValue(inputs[i][0]);
    SpreadsheetApp.flush();
    Utilities.sleep(500);

    // 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; 

    //additional parameters for exporting the sheet as a pdf
    var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf
        + '&gid=' + tab.getSheetId()   //the sheet's Id
        // following parameters are optional...
        + '&size=letter'      // paper size
        + '&portrait=false'    // orientation, false for landscape
        + '&fitw=true'        // fit to width, false for actual size
        + '&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


     // Get folder containing spreadsheet, for later export
    var name = sheet2.getRange('B3').getValue();    
    var folderID = "1IsffOYYQOHZJKggUzRTntfjmJKN0F6ga"; // Folder id to save in a folder.
    var folder = DriveApp.getFolderById(folderID);
    var response = UrlFetchApp.fetch(url + url_ext, options)
     var blob = response.getBlob().setName(name);

    //save the pdf to drive
    folder.createFile(blob);


}
}

我希望可以导出所有7个PDF,但是只有6个可以导出。

0 个答案:

没有答案