如何使用 Google Docs 中的 google apps 脚本设置 table 行的背景色?
How can I set the background color of a row in a table using google apps script in Google Docs?
我有一个 Google 文档和一个 table。我有一个脚本可以复制 table 并将副本插入第一段和 table 之间。我希望 table 相同,但我不知道如何使 table 的 header 行像原始 table 中那样变成灰色。
我已经想出如何突出显示 header 行中的文本,但我希望将整行变成灰色。
//添加菜单到文档
function onOpen() {
DocumentApp.getUi()
.createMenu('New Table')
.addItem('Add Table', 'addTable')
.addToUi();
}
function addTable() {
var body = DocumentApp.getActiveDocument().getBody();
//在现有 table 之上添加水平线和换行符
body.insertHorizontalRule(3);
body.insertParagraph(4, ' ');
// 创建一个包含单元格内容的 two-dimensional 数组。
var cells = [
['Week of: Week _'],
['Agenda/ Focus for the Week:'],
['Notes / Questions:'],
['Next Steps / Who’s Responsible?:'],
['Progress Toward SMART Goal(s):'],
];
// 从数组
构建一个table
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');
//设置table
中的字体样式
var style = {};
style[DocumentApp.Attribute.FONT_SIZE] = '10';
style[DocumentApp.Attribute.BOLD] = false;
table1.setAttributes(style);
//设置header行样式
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);
我希望新 table 的第一行颜色为灰色,但它只会突出显示灰色文本。
您可能想试试:
tableHeader.getCell(0).setBackgroundColor('#BBB9B9');
如果该行有多列,您将需要遍历它们。 Ref docs here.
我有一个 Google 文档和一个 table。我有一个脚本可以复制 table 并将副本插入第一段和 table 之间。我希望 table 相同,但我不知道如何使 table 的 header 行像原始 table 中那样变成灰色。
我已经想出如何突出显示 header 行中的文本,但我希望将整行变成灰色。
//添加菜单到文档
function onOpen() {
DocumentApp.getUi()
.createMenu('New Table')
.addItem('Add Table', 'addTable')
.addToUi();
}
function addTable() {
var body = DocumentApp.getActiveDocument().getBody();
//在现有 table 之上添加水平线和换行符
body.insertHorizontalRule(3);
body.insertParagraph(4, ' ');
// 创建一个包含单元格内容的 two-dimensional 数组。
var cells = [
['Week of: Week _'],
['Agenda/ Focus for the Week:'],
['Notes / Questions:'],
['Next Steps / Who’s Responsible?:'],
['Progress Toward SMART Goal(s):'],
];
// 从数组
构建一个tablevar 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');
//设置table
中的字体样式var style = {};
style[DocumentApp.Attribute.FONT_SIZE] = '10';
style[DocumentApp.Attribute.BOLD] = false;
table1.setAttributes(style);
//设置header行样式 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);
我希望新 table 的第一行颜色为灰色,但它只会突出显示灰色文本。
您可能想试试:
tableHeader.getCell(0).setBackgroundColor('#BBB9B9');
如果该行有多列,您将需要遍历它们。 Ref docs here.