Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Scripts API. Make cell background transparent

I have a Google document with a table. Table cells are, say, black and white. Goal: remove cell background (make all cells background transparent) by using Google Scripts API.

Is there a way to do so? I tried Cell.setBackgroundColor(color) in various ways (by using "none", "null", "transparent", "", etc. as input) but this doesn't work.

Making all cells white is not an acceptable workaround.

Thanks, Alex

like image 696
user1596203 Avatar asked Feb 28 '26 20:02

user1596203


1 Answers

Hm. Just tried the following code. It works as expected - changes the 'D10:E20' range colored background to white and the cells A1 and A2 contain the text

  • #980000
  • none

i.e. the range background is 'none' and not white. The function is

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var range = sheet.getRange('D10:E20');
  sheet.getRange('A1').setValue(range.getBackgroundColor());
  range.setBackground('none');
  sheet.getRange('A2').setValue(range.getBackgroundColor());
}

Edit 00: I am sorry, I misunderstood you. Fixing my fault, I created a text document and a small script (see bellow). In the document, I created a new table and, without any further modifications, I started the script. The results are

  • The cell[0, 0] contains the Background is null text and the code throws the We're sorry, a server error occurred. Please wait a bit and try again. (line 14) exception in the cell00.setBackgroundColor(bg00); line. Wrong.

In the document editor I changed the cell[0, 0] background, by the Table->Table properties dialog, to the yellow. The results are

  • The cell[0, 0] text is #ffff00. No errors. Correct.

I changed the cell background to None. The results are

  • The cell[0, 0] contains the Background is empty text and the code throws the Invalid color value. (line 14) exception in the cell00.setBackgroundColor(bg00); line. Wrong.

My opinion is that the points marked as Wrong is a bug. You have to file an issue to the issue tracker. For sure, you can use my code and this text to describe the problem.

function testDoc() {
  var doc = DocumentApp.openById('the-text-document-id');
  var cell00 = doc.getTables()[0].getCell(0, 0);
  var bg00 = cell00.getBackgroundColor();
  if (bg00 == null) {
    cell00.setText('Background is null');
  }
  else if (bg00 == '') {
    cell00.setText('Background is empty');
  }
  else {
    cell00.setText(bg00);
  }
  cell00.setBackgroundColor(bg00);
}
like image 61
megabyte1024 Avatar answered Mar 02 '26 10:03

megabyte1024



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!