1
votes

I'm using Google Apps Script to change the background colors of certain rows of cells in a Google Sheet. For some reason, when I run them as a function of a range of cells (not that I have tried otherwise), setBackgroundColor('white') works, while setBackgroundColorTransparent() invokes a "TypeError: not a function" message. Am I missing some bit with the usage or syntax of setBackgroundColorTransparent()?

My code:

function colorSundays() {
  var maxColumns = sheet.getMaxColumns();
  for (i = 1; i <= 31; i++) {
    var currentCell = sheet.getRange(i, 1);
    var value = currentCell.getValues();
    if (value == 'Sunday') {
      var currentRow = sheet.getRange(i, 1, 1, maxColumns);
      currentRow.setBackgroundColor('#F87CF8');
    } else {
      var currentRow = sheet.getRange(i, 1, 1, maxColumns);
//      currentRow.setBackgroundColor('white');
      currentRow.setBackgroundColorTransparent(); // Preferred, but now working right now.
    }
  }
}

The error message:

[20-06-08 19:09:04:246 CDT] TypeError: currentRow.setBackgroundColorTransparent is not a function
    at colorSundays(Code:52:18)
    at setThisMonth(Code:61:3)
1

1 Answers

3
votes

I believe your goal as follows.

  • In order to set the backgound color to the default, you tried to use setBackgroundColorTransparent().
  • You want to set the background color of cell.

For this, how about this answer?

Modification points:

  • Unfortunately, the method of setBackgroundColorTransparent() is not included in the Spreadsheet service. I think that the reason of your issue is this. I thought that in your case, the method of setBackgroundColorTransparent() of Class TextStyle in Slides Service might be used. Ref
  • the method of setBackgroundColor is not included in Class Range. In this case, please use setBackground.

So when you want to set the background color to the default, how about the following modification?

From:

currentRow.setBackgroundColor('#F87CF8');

To:

currentRow.setBackground('#F87CF8');

and

From:

currentRow.setBackgroundColorTransparent();

To:

currentRow.setBackground(null);

Note:

  • The method of setBackgroundColor is not included in Class Range. But from OP's replying, it was found that this method can be used.

Reference: