谷歌文档中的条件格式化表格单元格的Google应用程序脚本

时间:2018-05-16 21:16:18

标签: google-apps-script google-docs

我通过Twitter联系了@mhawksey,他给了我这个...... Remove row of table in a Google Document with Google Apps Script

这让我看一下DOC而不是SHEET(我之前只编写过SHEETS脚本)的脚本,但它并没有给我我想要的东西。

我希望能够根据通过邮件合并输入的值有条件地格式化表格单元格。我确定这是一个可以处理这个问题的简单脚本。如果有人可以用我需要阅读的内容和第一个' if'声明我应该能继续下去。

谢谢!

1 个答案:

答案 0 :(得分:0)

假设你有Document(DocumentApp.openById或DocumentApp.create或DocumentApp.openByUrl),你将不得不获取Body,搜索Tables,然后处理他们的TableCell。

类似的东西:

var doc = DocumentApp.openByUrl([URL]);  
var body = doc.getBody();  
var tables = body.getTables();

for each (var table in tables) {
    var i;
    for (i = 0; i < table.getNumRows(); i++) {
        var row = table.getRow(i);

        var j;
        for (j = 0; j < row.getNumCells(); j++) {
            var cell = row.getCell(j);

            // formatting goes here
            // example based on a numeric cell and a threshold of 50
            if (cell.getValue() > 50) {
                cell.setBackgroundColor("#ff0000");
            } else {
                cell.setBackgroundColor("#00ff00");
            }

            // Define a custom paragraph style.
            // can be used for more complicated formatting
            var style = {};
            style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =
                 DocumentApp.HorizontalAlignment.RIGHT;
            style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
            style[DocumentApp.Attribute.FONT_SIZE] = 18;
            style[DocumentApp.Attribute.BOLD] = true;
            cell.setAttributes(style);
        }    
    }             
}