【问题标题】:Google Scripts API. Make cell background transparent谷歌脚本 API。使单元格背景透明
【发布时间】:2012-08-10 00:57:14
【问题描述】:

我有一个带有表格的 Google 文档。比如说,表格单元格是黑白的。目标:使用 Google Scripts API 去除单元格背景(使所有单元格背景透明)。

有没有办法做到这一点?我以各种方式尝试了 Cell.setBackgroundColor(color)(通过使用“none”、“null”、“transparent”、“”等作为输入),但这不起作用。

将所有单元格设为白色不是可接受的解决方法。

谢谢, 亚历克斯

【问题讨论】:

  • 您希望在透明背景下看到什么?可能有解决您的问题的方法。
  • 好吧,我不希望看到什么特别的东西。我有一个白页和一个带有黑色单元格的表格。我想删除单元格背景颜色以查看白页上的白表。白底白字和白底白字在技术上是透明的,看起来是一样的。但是白色背景与“无”背景不同。文本背景也有同样的问题。

标签: google-apps-script


【解决方案1】:

嗯。刚刚尝试了以下代码。它按预期工作 - 将“D10:E20”范围的彩色背景更改为白色,并且单元格 A1 和 A2 包含文本

  • #980000
  • none

即范围背景是“无”而不是白色。功能是

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:对不起,我误会了你。为了解决我的错误,我创建了一个文本文档和一个小脚本(见下文)。在文档中,我创建了一个新表,并且在没有任何进一步修改的情况下启动了脚本。结果是

  • 单元格[0, 0] 包含Background is null 文本,代码在cell00.setBackgroundColor(bg00); 行中引发We're sorry, a server error occurred. Please wait a bit and try again. (line 14) 异常。 错误

在文档编辑器中,我通过Table->Table properties 对话框将单元格[0, 0] 背景更改为黄色。结果是

  • 单元格[0, 0] 文本为#ffff00。没有错误。 正确

我将单元格背景更改为None。结果是

  • 单元格[0, 0] 包含Background is empty 文本,代码在cell00.setBackgroundColor(bg00); 行中引发Invalid color value. (line 14) 异常。 错误

我认为标记为错误的点是一个错误。您必须向issue tracker 提出问题。当然,您可以使用我的代码和这段文字来描述问题。

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

【讨论】:

  • 这真的很有趣。 1. 如果我在电子表格中执行您的代码,它可以正常工作。但结果仍然是白色的(两个单元格)。更重要的是,如果我手动将单元格背景更改为无,那么应用程序会自动将背景颜色更改为白色。 2.说清楚:我有一个带表格的文档(文本文档)。如果我在以下代码中使用“无”颜色代码,则会引发错误(无效颜色代码):DocumentApp.getActiveDocument().getActiveSection().getTables()[0].getCell(0, 0).setBackgroundColor("none"); 我为文档中第一个表格的第一个单元格设置了 bg 颜色
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-15
  • 2020-08-10
  • 2012-11-24
相关资源
最近更新 更多