无法检索下一个对象:迭代器已到达末尾

Cannot retrieve the next object: iterator has reached the end

我正在尝试从共享 google 磁盘中的电子表格创建 pdf,但我正在尝试找出阻止生成 pdf 的原因。 问题出在这一行:

var driveFolder = DriveApp.getFoldersByName(folderName).next(); 

完整代码如下:

function createPDF() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Article 19");
  var folderName=ss.getRangeByName("NumAff").getValue();
  var fileName='Tableau 19.09_' + ss.getRangeByName("NumAff").getValue();

  sheet.showSheet();
  var gid = sheet.getSheetId();
  const pdfOpts = '&top_margin=0.30&bottom_margin=0.30&left_margin=0.25&right_margin=0.25'
    +'&size=LETTER' // paper size letter / You can use A4 or legal
    +'&portrait=true' // orientation portal, use false for landscape
    +'&fitw=true' // fit to page width false, to get the actual size
    +'&sheetnames=false' // hide optional headers
    +'&printtitle=false' //and footers
    +'&pagenumbers=false' // hide page numbers 
    +'&gridlines=false' //and gridlines
    +'&horizontal_alignment=CENTER'
    +'&vertical_alignment=CENTER'
    +'&fzr=true' // do not repeat row headers (frozen rows) on each page
    +'&attachment=false'
    +'&gid='+gid;

  var url = ss.getUrl().replace(/edit$/, '') + 'export?format=pdf' + pdfOpts // export as pdf / csv / xls / xlsx
  var options = {headers: {'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken()}}
  var blobresponse = UrlFetchApp.fetch(url, options);
  var blob=blobresponse.getBlob().setName(ss.getName() +".pdf" );
  var driveFolder = DriveApp.getFoldersByName(folderName).next(); 
  var file = driveFolder.createFile(blob);
  file.setName(fileName);
}

希望有人能向我解释问题的根源。

处理folderfile时应检查迭代器。

我认为 folderName 没有命中结果。

var folders = DriveApp.getFoldersByName(folderName); 
if (folders.hasNext()) {
  var driveFolder = folders.next();
}

参考: FolderIterator