Home > Blockchain >  Cannot retrieve the next object: iterator has reached the end
Cannot retrieve the next object: iterator has reached the end

Time:12-15

I'm trying to create a pdf from a spreadsheet in a shared google disk, but i'm trying to figure out what is blocking to generate the pdf. The problem is situated in this line:

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

Here is the whole code:

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);
}

Hope if someone can explain me the issue's source.

CodePudding user response:

You should check the iterator when you deal with folder or file.

I think there is not hitting result with the folderName.

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

Reference: FolderIterator

  • Related