如何使用Google文档中的Google Apps脚本设置表格中某行的背景颜色? [英] How can I set the background color of a row in a table using google apps script in Google Docs?

查看:41
本文介绍了如何使用Google文档中的Google Apps脚本设置表格中某行的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带表的Google文档.我已经获得了一个脚本,该脚本可以复制表格并将副本插入第一段和表格之间.我希望表是相同的,但是我不知道如何像原来的表一样使表的标题行变成灰色.

I have a Google Doc with a table. I've gotten a script that copies the table and inserts the copy between the first paragraph and the table. I would like the tables to be identical but I can't figure out how to make the header row of the table gray like it is in the original table.

我已经弄清楚了如何突出显示标题行中的文本,但是我希望将整个行变成灰色.

I've figured out how to highlight the text in the header row, but I'm looking to turn the entire row gray.

//向文档添加菜单

function onOpen() {
  DocumentApp.getUi()
  .createMenu('New Table')
  .addItem('Add Table', 'addTable')
  .addToUi();

}

function addTable() {

var body = DocumentApp.getActiveDocument().getBody();

//添加水平线&现有表格上方的换行符

//Add a horizontal rule & line break above existing tables

body.insertHorizontalRule(3);
body.insertParagraph(4, ' ');

//创建一个包含单元格内容的二维数组.

// Create a two-dimensional array containing the cell contents.

var cells = [
  ['Week of: Week _'],
  ['Agenda/ Focus for the Week:'],
  ['Notes / Questions:'],
  ['Next Steps / Who’s Responsible?:'],
  ['Progress Toward SMART Goal(s):'],

];

//从数组构建表

var table1 = body.insertTable(2,cells);

//设置第一列的宽度

table1.setColumnWidth(0, 467);

//设置第一列的高度

table1.getRow(1).setMinimumHeight(35);
table1.getRow(2).setMinimumHeight(35);
table1.getRow(3).setMinimumHeight(35);
table1.getRow(4).setMinimumHeight(35);

//设置边框颜色

table1.setBorderColor('#000000');

//在表格中设置字体样式

//Set style of font in table

var style = {};
style[DocumentApp.Attribute.FONT_SIZE] = '10';
style[DocumentApp.Attribute.BOLD] = false;

table1.setAttributes(style);

//设置标题行样式var tableHeader = table1.getRow(0);

//Set header row style var tableHeader = table1.getRow(0);

var headerStyle = {};  
headerStyle[DocumentApp.Attribute.BOLD] = true;
headerStyle[DocumentApp.Attribute.FONT_SIZE] = '10';
headerStyle[DocumentApp.Attribute.BACKGROUND_COLOR] = '#BBB9B9';

tableHeader.setAttributes(headerStyle);

我希望新表的第一行显示为灰色,但只能将文本突出显示为灰色.

I want the top row of the new table to be colored gray, but it only highlights the text gray.

推荐答案

您可能要尝试:

  tableHeader.getCell(0).setBackgroundColor('#BBB9B9');

如果该行有多个列,则需要遍历它们.在此处引用文档.

If the row has multiple columns, you'll need to iterate through them. Ref docs here.

这篇关于如何使用Google文档中的Google Apps脚本设置表格中某行的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆