I'm trying to write an Apps Script that change the font color of a cell when it value changes.

I found a way to to change de fontColor of a range, and add to trigger 'onEdit', but the all range is changing and not the single cells that I've been modify.

Here is my Script :

function changeFontColor() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetA1 = ss.getSheetByName('Voltaire');
  var lastRow = sheetA1.getLastRow();
  var lastColumn = sheetA1.getLastColumn();
  var dataRange = sheetA1.getRange(3, 3, lastRow -6, lastColumn);



1 Answer 1


Using the edit event object.

function changeFontColor(e) {


  • On the first line e is used to "catch" the edit event object.
  • On the "last statement" e.range is used instead of dataRange.
  • All other statements aren't required.

Using getActiveRange (no edit event object)

function changeFontColor() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  • On the "last statement" ss.getActiveRange() is used instead of dataRange.
  • Only the first and the last statements are required.


  • Thank you @Rubén, I can't manage to make it work tho... I get an error message : "TypeError: Cannot read property 'range' of undefined (ligne 6, fichier "Code")"
    – Hugo Ganet
    Dec 2, 2020 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.