子索引(1)必须小于子元素的数量

时间:2016-10-29 08:44:14

标签: javascript google-apps-script

请原谅这个基本问题。我已经在谷歌应用脚​​本 -

上发送了这个错误代码

子索引(1)必须小于子元素的数量(0)。 (第86行,文件"代码")自从我改变了一些代码。

代码中没有第86行,我运行的一个测试表明下面的代码行是问题所在。

提前致谢!

function getImageLinks(){
  //create array of images to update (paragraph, index, URL, width, height)
  var imgLocations = [
    [78,1,'https://docs.google.com/spreadsheets/EDITED OUTimage',680,393],

//  var doc = DocumentApp.openById('1NENe-GSyUDNLHwp84cKwamDlQbbIipaZ2i2GGFUOFr0');  //test 
  var doc = DocumentApp.openById('1mwwJBPdGPAdILZ9pfARqTAG5Gt9svPthqZ8RcCRS6xI');  //prod
  var body = doc.getBody();
    for(var intX=0;intX<imgLocations.length;intX++){
      var newImage = UrlFetchApp.fetch(imgLocations[intX][2]);      
      var img = body.getChild(imgLocations[intX][0]).asParagraph().getChild(imgLocations[intX][1]);
      var parent = img.getParent();
      var paragraph = parent.asParagraph();
      img.removeFromParent();
      paragraph.insertInlineImage(imgLocations[intX][1],newImage);
      **var updatedImage = body.getChild(imgLocations[intX][**0]).asParagraph().getChild(imgLocations[intX][1]);
      updatedImage.asInlineImage().setWidth(imgLocations[intX][3]).setHeight(imgLocations[intX][4]);
    }
}

1 个答案:

答案 0 :(得分:0)

首先,粗体在代码格式化中不起作用。

首先,如评论中所述,您在这里打开括号

    String excelFile="";

    excelFile = getFullFilePath(fileName);
    File file = new File(excelFile + fileName,"UTF-8");
    File output = new File(excelFile,"UTF-8");

    FileUtils.writeStringToFile(file, content, "UTF-8");

应该在哪里

var imgLocations = [
    [78,1,'https://docs.google.com/spreadsheets/EDITED OUTimage',680,393],

请注意,您不仅错过了var imgLocations = [ [78,1,'https://docs.google.com/spreadsheets/EDITED OUTimage',680,393]] ,而且在结束前还有],这表示您的imgLocations应该有多个条目。由于,等于for

,您的imgLocations.lenght循环只会运行一次

有了这个问题,你得到这个问题以及缺少括号的原因在于:

1

因为那将有0个孩子,并且你应该至少有1个孩子来获得父母。