title: Google Sheets Script – Function that outputs the background color of a cell
body: <p>I would like to write my own function in Google Script that I can use in Google Sheets to get the background color of a cell. Let's call the function GETBACKGROUNDCOLOR.</p>
  
  <p>I want to be able to pass a cell reference (in A1 notation) as parameter, e.g. if I call =GETBACKGROUNDCOLOR(B2), it should return the background color of cell B2. If I call the function without parameter, I want it to return the background color of the same cell in which it is called, e.g. calling =GETBACKGROUNDCOLOR() in C3 should return the background color of C3.</p>
  
  <p>I have tried this:</p>
  
  <pre><code>function GETBACKGROUNDCOLOR(cell){
    return SpreadsheetApp.getActiveSheet().getRange(cell).getBackground();
  }
  </code></pre>
  
  <p>But when I call GETBACKGROUNDCOLOR(A1), the parameter is not the cell reference of A1, but the cell content of A1. </p>
  
  <p>How can I solve this? </p>