3
votes

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

1
What do you expect to see under transparent background? May be there is a workaround for your issue. - megabyte1024
Well, I don't expect to see anything special. I have a white page and a table with black cells. I want to remove cell background color to see white table on white page. Technically transparent on white and white on white looks the same. But white background is not the same as 'None' background. The same issue with text background. - user1596203

1 Answers

3
votes

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);
}