【问题标题】:Change table background color using google docs API (node.js)使用 google docs API (node.js) 更改表格背景颜色
【发布时间】:2021-07-20 11:01:56
【问题描述】:

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

看看这个例子:

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

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

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

【问题讨论】:

    标签: javascript google-drive-api google-docs google-docs-api


    【解决方案1】:

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

    在我的情况下,文档中表格位置的索引是 2 - 相应地适应您的位置。

    【讨论】:

    • 感谢您的回答 :) 这看起来很有希望,我会试试这个并回复您。
    • 好的,所以我得到了这个error: Invalid requests[18].updateTableCellStyle: The provided table start location is invalid.,这意味着我不理解这个tableStartLocation 属性。我会假设我的 tableStartLocation 将是index: 0,因为我的文档正文中只有 1 个表。我在这里阅读:developers.google.com/docs/api/reference/rest/v1/documents/… 我确实有一个标题,并且在我的表格开始之前有一些文本,这意味着什么吗?如何识别这个索引?
    • 您可以使用documents.get方法,通过将fields设置为body/content/table/tableRows/startIndex,获取表格第一行的startIndextableStartLocation index 将比 tableRows/startIndex 小一。
    • 通过使用 document.get 端点并检查内容,我找到了这个对象,我假设它必须是表:{ startIndex: 88, endIndex: 1358, table: { rows: 15, columns: 4, tableRows: [....], tableStyle: { tableColumnProperties: [Array] } } } 但是当我提供8887 作为@ 987654336@ 我仍然遇到同样的错误:The provided table start location is invalid. 有什么想法吗?
    • 最终解决了,但是,tableStartLocation 索引必须与检查文档中的startIndex 相同(不是负1)。我重写了我的代码,最终它成功了。我认为竞争条件发生了一些奇怪的事情,我在更新表格单元格的同时替换了文本,我认为索引可能同时发生了变化。
    猜你喜欢
    • 1970-01-01
    • 2017-11-04
    • 2018-10-14
    • 2011-05-23
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    相关资源
    最近更新 更多