使用Drive搜索结果会导致迭代器错误

时间:2018-05-07 09:06:56

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

我的Apps脚本代码一直出现此错误:

  

无法检索下一个对象:迭代器已到达终点。

行上出现此错误:

var priceChangeImage = DriveApp.getFilesByName("Price Change_" + tickerSymbol + ".png").next();

var earningsRevisionsImage = DriveApp.getFilesByName("Earnings Revisions_" + tickerSymbol + ".png").next();

var FgpeImage = DriveApp.getFilesByName("F-GPE_" + tickerSymbol + ".png").next();

执行上下文:

  //Slide4
  var shapeSlide4 = slides[3].getShapes();
  var priceChangeImage = DriveApp.getFilesByName("Price Change_" + tickerSymbol + ".png").next();
  var newImage4 = shapeSlide4[0].replaceWithImage(priceChangeImage);
  newImage4.setWidth(650);
  newImage4.alignOnPage(SlidesApp.AlignmentPosition.HORIZONTAL_CENTER);

  //Slide5
  var shapeSlide5 = slides[4].getShapes();
  var earningsRevisionsImage = DriveApp.getFilesByName("Earnings Revisions_" + tickerSymbol + ".png").next();
  var newImage5 = shapeSlide5[0].replaceWithImage(earningsRevisionsImage);
  newImage5.setWidth(650);
  newImage5.alignOnPage(SlidesApp.AlignmentPosition.HORIZONTAL_CENTER);

  //Slide6
  var shapeSlide6 = slides[5].getShapes();
  var FgpeImage = DriveApp.getFilesByName("F-GPE_" + tickerSymbol + ".png").next();
  var newImage6 = shapeSlide6[0].replaceWithImage(FgpeImage);
  newImage6.setWidth(650);
  newImage6.alignOnPage(SlidesApp.AlignmentPosition.HORIZONTAL_CENTER);


  newSlideNotify(slideUrl);

}

为什么我收到此错误?我该如何解决?

2 个答案:

答案 0 :(得分:2)

您的代码失败了,因为您在next()上盲目地呼叫FileIterator,而没有先检查是否有搜索结果。

使用文件和文件夹迭代器的正确方法是先调用hasNext()

此示例获取搜索给定文件名的第一个结果,如果您的搜索未返回任何结果,则会抛出错误(因此您知道是否需要修复搜索或文件名)。

function getFirstFileWithName_(searchName) {
  var allFiles = getAllFilesWithName_(searchName);
  // Were there any results from the search?
  if (allFiles.length === 0)
    throw new Error("No files with name '" + searchName + "'");

  return allFiles[0];
}
function getAllFilesWithName_(name) {
  var results = DriveApp.getFilesByName(name);
  var files = [];
  while (results.hasNext()) {
    files.push(results.next());
  }
  return files;
}

答案 1 :(得分:1)

尝试使用以下给定的代码:

    var shapeSlide4 = slides[3].getShapes();
        var filesPriceChange = DriveApp.getFilesByName("Price Change_"+tickerSymbol+".png");
        if(filesPriceChange.hasNext())
        {
              var priceChangeImage = filesPriceChange .next();
        }
          var newImage4 = shapeSlide4[0].replaceWithImage(priceChangeImage);
          newImage4.setWidth(650);
          newImage4.alignOnPage(SlidesApp.AlignmentPosition.HORIZONTAL_CENTER);

          //Slide5
          var shapeSlide5 = slides[4].getShapes();
    var filesEarningsRevisions = DriveApp.getFilesByName("Earnings Revisions_"+tickerSymbol+".png");
        if(filesEarningsRevisions .hasNext())
        {
              var earningsRevisionsImage = filesEarningsRevisions .next();
        }
      var newImage5 = shapeSlide5[0].replaceWithImage(earningsRevisionsImage);
      newImage5.setWidth(650);
      newImage5.alignOnPage(SlidesApp.AlignmentPosition.HORIZONTAL_CENTER);

      //Slide6
      var shapeSlide6 = slides[5].getShapes();
var filesFgpe = DriveApp.getFilesByName("F-GPE_"+tickerSymbol+".png");
if(filesFgpe.hasNext())
{
      var FgpeImage = filesFgpe.next();
}
      var newImage6 = shapeSlide6[0].replaceWithImage(FgpeImage);
      newImage6.setWidth(650);
      newImage6.alignOnPage(SlidesApp.AlignmentPosition.HORIZONTAL_CENTER);


      newSlideNotify(slideUrl);