Home > Blockchain >  DocumentApp's TableCell.appendImage creates an unnecessary paragraph before the image. How can
DocumentApp's TableCell.appendImage creates an unnecessary paragraph before the image. How can

Time:12-31

I'm trying to create a Google Document file using DocumentApp API.

I want to make a table and insert an inline-image into its cell. Like this:

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

It works, but there is a paragraph ( or '\n' or '\r'? I'm not sure.) before the inserted image.

I want that the cell to contain only the image.

Is there any way to do that?

Thanks.

CodePudding user response:

When a table is created, the cell has one child as the default as mentioned by Cooper's comment. So, in your situation, in order to achieve your goal, how about the following modification?

Modified script:

From:

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

To:

In this modification, the image is appended to the cell and removed in the 1st paragraph. I thought that this modification might help to understand the above situation.

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

Or, you can also use the following modification. In this modification, the image is inserted into the 1st paragraph.

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

Reference:

  • Related