1

我已經試過這個:Google Spreadsheet: Script to Change Row Color when a cell changes text;如何更改google電子表格行顏色,當行中的單元格被編輯?

但它不能得到它的工作。該行的顏色不會更改爲#000000

這是我到目前爲止有:

function onEdit(event) 
{ 
    var ss = event.source.getActiveSheet(); 
    var r = event.source.getActiveRange(); 

    var currentValue = r.getValue(); 

    if(currentValue == "dags dato") 
    { 
    var dd = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"); 
    r.setValue(dd); 
    } 
    else if(currentValue == "dialog") 
    { 
    setRowColor("yellow"); 
    } 
    else if(currentValue == "besvaret") 
    { 
    setRowColor("yellow"); 
    } 
    else if(currentValue == "afvist") 
    { 
    setRowColor("red"); 
    } 
} 

function setRowColor(color) 
{ 
    var range = SpreadsheetApp.getActiveSheet().getDataRange(); 
    var statusColumnOffset = getStatusColumnOffset(); 

    for (var i = range.getRow(); i < range.getLastRow(); i++) { 
    rowRange = range.offset(i, 0, 1); 
    status = rowRange.offset(0, statusColumnOffset).getValue(); 

    rowRange.setBackgroundColor("#000000"); 

} 


//Returns the offset value of the column titled "Status" 
//(eg, if the 7th column is labeled "Status", this function returns 6) 
function getStatusColumnOffset() { 

    lastColumn = SpreadsheetApp.getActiveSheet().getLastColumn(); 
    var range = SpreadsheetApp.getActiveSheet().getRange(1,1,1,lastColumn); 

    for (var i = 0; i < range.getLastColumn(); i++) { 
    if (range.offset(0, i, 1, 1).getValue() == "Status") { 
     return i; 
    } 
    } 
} 
+0

嘗試手動運行onEdit(和模擬事件參數)。實際上,在調試中嘗試一下,看看它在哪裏。 – alex 2012-04-16 09:35:27

+0

我會盡力的。我可以得到行號:r.getRow() - 是否可以使用getRange()選擇enitre行? – Kenci 2012-04-16 09:43:55

回答

2

我爲自己寫了更快更清潔的方法,我想分享它。

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"); 
      } 
     } 
    } 
} 
0

你可以嘗試使用Logger類,像這樣來檢查你的任何錯誤或問題的代碼:

try { 
    //your code 
} 
catch(e) { 
    Logger.log(e); 
} 

然後你可以去查看 - >看到從腳本編輯器日誌如果每一行代碼的預期相符。此外,執行抄本可能會有助於查看代碼是否在特定的代碼行處中斷。你可以view more details about how each troubleshooting method works

相關問題