title: Google Sheets: Change background color of cell using content of another cell
body: <p><a href="https://i.stack.imgur.com/R9e9T.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/R9e9T.png" alt="enter image description here"></a></p>
  
  <p>I have a google sheet as above where I am typing in colour codes in a hex format and then naming them. I would like to automatically update the Colour Block column's background colour with the hex code in the code column.</p>
  
  <p>Script I have tried, but setBackground function doesn't work.</p>
  
  <pre><code>function myFunction() {
      var sheet = SpreadsheetApp.getActiveSheet();
      var cells = sheet.getDataRange().getValues();
      for(n = 1; n &lt; cells.length; n++) {
          var cell = cells[n];
          cell[n][2].setBackground(cell[n][1]);
      }
  }
  </code></pre>