使用 google 文档更改 table 背景颜色 API (node.js)

Change table background color using google docs API (node.js)

我正在使用 google 文档和驱动器 API 生成一些报告。我想根据数字的高低动态设置 table 单元格中的背景颜色,数字越低越红,数字越高越绿。

看看这个例子:

我可以像这样从我的节点服务更新数字:

const auth = new google.auth.GoogleAuth({
  keyFile: 'credentials.json',
  scopes: ['https://www.googleapis.com/auth/documents', 'https://www.googleapis.com/auth/drive']
});

// Create client instance for auth
const client = await auth.getClient();
const docs = google.docs({ version: 'v1', auth: client });
const drive = google.drive({ version: 'v3', auth: client });

const copyMetaData = {
  name: `Some report`,
  parents: ['1b78gdfgdf8gdiokkg4XG2-9WFWGGla'] // some folder id
};

const copiedFile = await drive.files.copy({
  auth,
  fileId: '1VMKs89fg9AGP1xs-1F545gKgkvf2Daza7UZMRsdf789', // some file id
  requestBody: copyMetaData
});

const requests = [
  ['{{data-1}}', '447'],
  ['{{data-2}}', '378'],
  ['{{data-3}}', '102'],
  ['{{data-1-p}}', '91'],
  ['{{data-2-p}}', '78'],
  ['{{data-3-p}}', '23']
].map(update => {
  const [handleBar, replaceText] = update;
  return {
    replaceAllText: {
      containsText: {
        text: handleBar,
        matchCase: true
      },
      replaceText
    }
  };

我只是不知道如何设置背景颜色,甚至无法识别我的 table 单元格。或者这是否可能,但对我来说这似乎是一个非常微不足道的用例,因为在所有 google 的示例中,它们都在生成发票,您很容易认为有人会想要产生负余额粗体和红色,但如果它是正余额,例如黑色。

update table cell colors for Google Docs 的示例请求:

{
  "requests": [
    {
      "updateTableCellStyle": {
        "tableCellStyle": {
          "backgroundColor": {
            "color": {
              "rgbColor": {
                "green": 1
              }
            }
          }
        },
        "fields": "backgroundColor",
        "tableRange": {
          "columnSpan": 1,
          "rowSpan": 1,
          "tableCellLocation": {
            "columnIndex": 0,
            "rowIndex": 0,
            "tableStartLocation": {
              "index": 2
            }
          }
        }
      }
    }
  ]
}

在我的例子中,文档中 table 位置的索引是 2 - 根据您的位置进行相应调整。