DocumentApp 的 TableCell.appendImage 在图像前创建了一个不必要的段落。我怎样才能避免这种情况?

DocumentApp's TableCell.appendImage creates an unnecessary paragraph before the image. How can I avoid this?

我正在尝试使用 DocumentApp API 创建 Google 文档文件。

我想制作一个 table 并在其单元格中插入一个内联图像。 像这样:

        table = body.appendTable([[''],[contact]]);
        let img = table.getCell(0,0).appendImage(DriveApp.getFileById(imgFileId).getBlob());

可以,但是在插入的图片之前有一个段落(或'\n'或'\r'?我不确定。)。

我希望单元格只包含图像。

有什么办法吗?

谢谢。

当创建 table 时,单元格默认有一个子节点,如 所述。那么,根据你的情况,为了达到你的目的,做以下修改怎么样?

修改后的脚本:

发件人:

let img = table.getCell(0,0).appendImage(DriveApp.getFileById(imgFileId).getBlob());

收件人:

在此修改中,图像附加到单元格并在第 1 段中删除。我想这样修改可能有助于理解上面的情况。

var cell = table.getCell(0, 0);
cell.appendImage(DriveApp.getFileById(imgFileId).getBlob());
cell.getChild(0).removeFromParent();

或者,你也可以使用下面的修改。在此修改中,图像被插入到第 1 段中。

table.getCell(0, 0).getChild(0).asParagraph().insertInlineImage(0, DriveApp.getFileById(imgFileId).getBlob());

参考: