-1

I'm trying to add a border in a google Document image, but I cannot get the result with InlineImage or PositionedImage.

PositionedImage doesn't support setAttributes(), so I will use InlineImage for the example:

Example: https://docs.google.com/document/d/19f_vFb6dFov7X8rBZSeThG3xW8ODZaIJuchRl-BPIXk/edit?usp=sharing

The bounded script add the image and try to make it bordered, but it's not working: the border is still absent

function test() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  body.clear();
  
  var par1 = body.appendParagraph("Test");
  
  var bordered = {};
  bordered[DocumentApp.Attribute.BORDER_COLOR] = "#000000";
  bordered[DocumentApp.Attribute.BORDER_WIDTH] = 1;
  
  var image = DriveApp.getFileById("1Z_Tc6LPFTrfl5-BOCkJ6u-Us6jaTAG_-").getBlob();
  par1.appendInlineImage(image);
  par1.setAttributes(bordered);
  
}

Thanks

brazoayeye
  • 329
  • 1
  • 2
  • 14

1 Answers1

1

There's no such feature using Apps Script by now

As per the documentation the attribute BORDER_COLOR and BORDER_WIDTH are for tables.

var bordered = {};
bordered[DocumentApp.Attribute.BORDER_COLOR] = "#000000";
bordered[DocumentApp.Attribute.BORDER_WIDTH] = 1;

A community member has opened a feature request in the Google Issue tracker.

As a workaround use a table and an image inside

This is not the ideal solution but you still can resize the image and table as needed.

function test() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  body.clear();
  
  var par1 = body.appendParagraph("Test");

  var tableBordered = {};
  tableBordered[DocumentApp.Attribute.BORDER_COLOR] = "#000000";
  tableBordered[DocumentApp.Attribute.BORDER_WIDTH] = 3;

  var cellBordered = {};
  cellBordered[DocumentApp.Attribute.PADDING_BOTTOM] = 0;
  cellBordered[DocumentApp.Attribute.PADDING_LEFT] = 0;
  cellBordered[DocumentApp.Attribute.PADDING_RIGHT] = 0;
  cellBordered[DocumentApp.Attribute.PADDING_TOP] = 3;
  cellBordered[DocumentApp.Attribute.BACKGROUND_COLOR] = "#000000";
  
  var image = DriveApp.getFileById("1Z_Tc6LPFTrfl5-BOCkJ6u-Us6jaTAG_-").getBlob();
  var table = body.appendTable([['']]);
  table.setAttributes(tableBordered);

  var cell = table.getRow(0).getCell(0);
  cell.setAttributes(cellBordered);
  
  var img = cell.appendImage(image);
  img.setWidth(body.getPageWidth() - 3);
}

Reference

Google Apps Script > Body.appendTable

Google Apps Script > Table.setAttributes

Google Apps Script > Document.Attribute

Google Apps Script > InlineImage.setWidth

Jose Vasquez
  • 1,678
  • 1
  • 6
  • 14