Google 文档脚本:边框属性不起作用

Google script for Document: border attribute not working

我正在尝试在 google 文档图像中添加边框,但我无法使用 InlineImage 或 PositionedImage 获得结果。

PositionedImage 不支持 setAttributes(),所以我将使用 InlineImage 作为示例:

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

bounded 脚本添加图像并尝试为其添加边框,但它不起作用:边框仍然不存在

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);
  
}

谢谢

目前还没有使用 Apps 脚本的此类功能

根据 documentation 属性 BORDER_COLORBORDER_WIDTH 用于 tables.

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

一位社区成员打开了 feature request in the Google Issue tracker

解决方法是使用 table 和内部图像

这不是理想的解决方案,但您仍然可以根据需要调整图像大小和table。

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);
}

参考

Google Apps Script > Body.appendTable

Google Apps Script > Table.setAttributes

Google Apps Script > Document.Attribute

Google Apps Script > InlineImage.setWidth