Script to Change Row Color when a cell changes text

前端 未结 5 439
傲寒
傲寒 2020-11-27 11:05

I have a Google spreadsheet where I keep a list of bugs and whenever I fix a bug I change the status from \"Not Started\" to \"Complete\". I want to write a script for the

5条回答
  •  独厮守ぢ
    2020-11-27 11:30

    I used GENEGC's script, but I found it quite slow.

    It is slow because it scans whole sheet on every edit.

    So I wrote way faster and cleaner method for myself and I wanted to share it.

    function onEdit(e) {
        if (e) { 
            var ss = e.source.getActiveSheet();
            var r = e.source.getActiveRange(); 
    
            // If you want to be specific
            // do not work in first row
            // do not work in other sheets except "MySheet"
            if (r.getRow() != 1 && ss.getName() == "MySheet") {
    
                // E.g. status column is 2nd (B)
                status = ss.getRange(r.getRow(), 2).getValue();
    
                // Specify the range with which You want to highlight
                // with some reading of API you can easily modify the range selection properties
                // (e.g. to automatically select all columns)
                rowRange = ss.getRange(r.getRow(),1,1,19);
    
                // This changes font color
                if (status == 'YES') {
                    rowRange.setFontColor("#999999");
                } else if (status == 'N/A') {
                    rowRange.setFontColor("#999999");
                // DEFAULT
                } else if (status == '') { 
                    rowRange.setFontColor("#000000");
                }   
            }
        }
    }
    

提交回复
热议问题