尝试在 google 应用程序脚本中检索段落的 child 元素时出现异常
Exception when trying to retrieve child elements of paragraphs in google apps script
我一直在尝试使用 google 应用程序脚本处理文档中的图像集。我设法做我想做的事,即从我的文档中获取图像并将它们放在一个 table 中,两行一列,其中第一行包含一个 space 用于图像描述和第二个包含图像本身。但是,我在浏览文档的段落时遇到困难,我的脚本找到了没有 children 的段落。这已生成运行时异常,如下所示:异常:child 索引 (0) 必须小于 child 元素 (0) 的数量。我试图用 if () 来处理这个问题,但是当我到达空段落时,同样的异常被抛出。
我的脚本代码如下:
function manipuleImage(){
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraphs = body.getParagraphs();
var imagens = body.getImages();
var image = imagens[0];
var typeImage = image.getType();
for(var i = 0; i < paragraphs.length; i++){
var paragraph = paragraphs[i];
if(paragraph.getChild(0) != 0){
var child = paragraph.getChild(0);
var typeChild = child.getType();
if(typeChild == "INLINE_IMAGE"){
var cells = [
[''],['']
];
var styleCell = {};
styleCell[DocumentApp.Attribute.FONT_SIZE] = 11;
styleCell[DocumentApp.Attribute.BOLD] = true;
styleCell[DocumentApp.Attribute.FOREGROUND_COLOR] = '#ffffff';
styleCell[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
styleCell[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
styleCell[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
var tableImage = body.insertTable(i, cells)
tableImage.getRow(0).getCell(0).setBackgroundColor("#ef5350");
tableImage.setBorderColor("#ef5350");
tableImage.setBorderWidth(1);
tableImage.getRow(0).getCell(0).setAttributes(styleCell);
tableImage.getRow(1).getCell(0).setAttributes(styleCell);
tableImage.getRow(0).getCell(0).setTextAlignment(DocumentApp.TextAlignment.NORMAL);
tableImage.getRow(1).getCell(0).setTextAlignment(DocumentApp.TextAlignment.NORMAL);
var appendImage = tableImage.getRow(1).getCell(0).clear().appendImage(child.getBlob());
var index = i + 1;
body.removeChild(body.getChild(index));
var imageH = appendImage.getHeight() / 2;
var imageW = appendImage.getWidth() / 2;
appendImage.setHeight(imageH).setWidth(imageW);
}
}
}
}
这个修改怎么样?
修改点:
getChild(childIndex)
returns 元素对象。请注意这一点。
- 在这种情况下,我认为当段落只是换行时,就会出现错误。我想你的问题可能是这个原因。
如果想跳过只有换行的段落,下面的修改如何?
修改后的脚本:
当您的脚本修改时,请修改如下。
从:
if(paragraph.getChild(0) != 0){
到:
if(paragraph.getNumChildren() != 0){
参考文献:
我一直在尝试使用 google 应用程序脚本处理文档中的图像集。我设法做我想做的事,即从我的文档中获取图像并将它们放在一个 table 中,两行一列,其中第一行包含一个 space 用于图像描述和第二个包含图像本身。但是,我在浏览文档的段落时遇到困难,我的脚本找到了没有 children 的段落。这已生成运行时异常,如下所示:异常:child 索引 (0) 必须小于 child 元素 (0) 的数量。我试图用 if () 来处理这个问题,但是当我到达空段落时,同样的异常被抛出。 我的脚本代码如下:
function manipuleImage(){
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraphs = body.getParagraphs();
var imagens = body.getImages();
var image = imagens[0];
var typeImage = image.getType();
for(var i = 0; i < paragraphs.length; i++){
var paragraph = paragraphs[i];
if(paragraph.getChild(0) != 0){
var child = paragraph.getChild(0);
var typeChild = child.getType();
if(typeChild == "INLINE_IMAGE"){
var cells = [
[''],['']
];
var styleCell = {};
styleCell[DocumentApp.Attribute.FONT_SIZE] = 11;
styleCell[DocumentApp.Attribute.BOLD] = true;
styleCell[DocumentApp.Attribute.FOREGROUND_COLOR] = '#ffffff';
styleCell[DocumentApp.Attribute.FONT_FAMILY]='Roboto';
styleCell[DocumentApp.Attribute.VERTICAL_ALIGNMENT] = DocumentApp.VerticalAlignment.CENTER;
styleCell[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
var tableImage = body.insertTable(i, cells)
tableImage.getRow(0).getCell(0).setBackgroundColor("#ef5350");
tableImage.setBorderColor("#ef5350");
tableImage.setBorderWidth(1);
tableImage.getRow(0).getCell(0).setAttributes(styleCell);
tableImage.getRow(1).getCell(0).setAttributes(styleCell);
tableImage.getRow(0).getCell(0).setTextAlignment(DocumentApp.TextAlignment.NORMAL);
tableImage.getRow(1).getCell(0).setTextAlignment(DocumentApp.TextAlignment.NORMAL);
var appendImage = tableImage.getRow(1).getCell(0).clear().appendImage(child.getBlob());
var index = i + 1;
body.removeChild(body.getChild(index));
var imageH = appendImage.getHeight() / 2;
var imageW = appendImage.getWidth() / 2;
appendImage.setHeight(imageH).setWidth(imageW);
}
}
}
}
这个修改怎么样?
修改点:
getChild(childIndex)
returns 元素对象。请注意这一点。- 在这种情况下,我认为当段落只是换行时,就会出现错误。我想你的问题可能是这个原因。
如果想跳过只有换行的段落,下面的修改如何?
修改后的脚本:
当您的脚本修改时,请修改如下。
从:if(paragraph.getChild(0) != 0){
到:
if(paragraph.getNumChildren() != 0){