【问题标题】:Google apps script for conditional formatting table cells in google Doc谷歌文档中条件格式表格单元格的谷歌应用脚​​本
【发布时间】:2018-10-27 01:06:53
【问题描述】:

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

这让我了解了为 DOC 而不是 SHEET 编写脚本(我以前只编写过 SHEETS 脚本),但它并没有给我我想要的东西。

我希望能够根据通过邮件合并输入的值有条件地格式化表格单元格。我相信会有一个简单的脚本可以处理这个问题。如果有人可以让我从我需要阅读的值和第一个'if'语句开始我应该能够继续。

谢谢!

【问题讨论】:

    标签: google-apps-script google-docs


    【解决方案1】:

    假设您有文档(DocumentApp.openById 或 DocumentApp.create 或 DocumentApp.openByUrl),您将必须获取 Body,搜索表格,然后处理它们的 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);
            }    
        }             
    }
    

    【讨论】:

    • 非常感谢理查德的回复!我已将 Doc URL 弹出并运行调试。但是 setColour() 属性无法识别。我找不到如何在此评论上附加屏幕截图。
    • 对不起 - 我想我的意思是 setBackgroundColor() 那里。 developers.google.com/apps-script/reference/document/…
    • 太棒了!好的,所以我已经给出了预期输出所需的格式,但是我需要一些 IF 语句来根据它找到的内容查看单元格内容和格式。我看过这个stackoverflow.com/questions/20404002/…,但我不确定你提供的代码中的哪一点我需要比较。谢谢!
    • 这听起来像是一个未公开的家庭作业问题,但您需要一个 if 语句来根据您的标准测试单元格的值。假设单元格是一个数字,高于 50 与低于 50 不同: if (cell.getValue() > 50) { cell.setBackgroundColor("#ff0000"); } else { cell.setBackgroundColor("#00ff00"); }
    • 我希望我能为家庭作业设置这样的东西!我是一名教师!我的编码知识非常幼稚,但我已经到了那里。一般来说,我需要一些入门指导,我可以从那里找到自己的路。太好了,谢谢!
    猜你喜欢
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多