首页 > 解决方案 > 错误:setBackgroundColorTransparent() 不是 Google Apps 脚本中的函数

问题描述

我正在使用 Google Apps 脚本更改 Google 表格中某些单元格行的背景颜色。出于某种原因,当我将它们作为一系列单元格的函数运行时(不是我尝试过的其他方法),setBackgroundColor('white') 有效,而 setBackgroundColorTransparent() 调用“TypeError:不是函数”消息。我是否遗漏了 setBackgroundColorTransparent() 的用法或语法?

我的代码:

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.
    }
  }
}

错误信息:

[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)

标签: google-apps-scriptgoogle-sheets

解决方案


我相信你的目标如下。

  • 为了将背景颜色设置为默认值,您尝试使用setBackgroundColorTransparent().
  • 您要设置单元格的背景颜色。

为此,这个答案怎么样?

修改点:

  • 不幸的是,setBackgroundColorTransparent()电子表格服务中不包含 的方法。我认为你的问题的原因是这个。我认为在您的情况下,setBackgroundColorTransparent()可能会使用 Slides Service 中 Class TextStyle 的方法。参考
  • 的方法setBackgroundColor不包含在类范围中。在这种情况下,请使用setBackground.

那么当你想将背景颜色设置为默认时,下面的修改呢?

从:

currentRow.setBackgroundColor('#F87CF8');

至:

currentRow.setBackground('#F87CF8');

从:

currentRow.setBackgroundColorTransparent();

至:

currentRow.setBackground(null);

笔记:

  • 的方法setBackgroundColor不包含在类范围中。但是从OP的回复中发现可以使用这种方法。

参考:


推荐阅读