Google Sheets / 如何更改值与数组中值相同的单元格的格式?

Google Sheets / How to change the format of cells whose values are the same as the values from the array?

请告诉如何更改单元格中的视觉内容(背景、字体粗细和颜色),前提是存储在该单元格中的值与正在检查的数组的值匹配? 我在 ConditionalFormatRule 上观看并尝试编写脚本来解决 nodejs

上的这个问题
const client = new google.auth.JWT(
    keys.client_email,
    null,
    keys.private_key,
    ["https://www.googleapis.com/auth/spreadsheets"]
)

const gsapi = google.sheets({
    version: 'v4',
    auth: client
})

const spreadsheetIdCurr = '1pircQiSbWuiaMVOVFi9ZfVizB5KEx4YSh7FqdWEp4_f';

const checkData = {values: ["aaa", "b"]}; // Arrey for checking

async function colorizeResult() {

    res = await gsapi.spreadsheets.batchUpdate({
        spreadsheetId: spreadsheetIdCurr,
        requestBody: {
            requests: [
                {
                    updateCells: {
                        range: {
                            sheetId: 0,
                            startColumnIndex: 0,
                            endColumnIndex: 5,
                            startRowIndex: 0,
                            endRowIndex: 2,
                        },
                    },
                    addConditionalFormatRule: {
                        rule: {
                            booleanRule: {
                                condition: {
                                    type: "ONE_OF_LIST",
                                    values: [{userEnteredFormat: {userEnteredValue: checkData[0]}},
                                        {userEnteredFormat: {userEnteredValue: checkData[1]}},],
                                },
                                format: {
                                    textFormat: {
                                        bold: true,
                                        foregroundColor: {red: 1, green: 1, blue: 1}
                                    },
                                    backgroundColor: {red: 0.1, green: 0.3, blue: 1}
                                }
                            }
                        }
                    }

                }
            ]
        },
    })

}
colorizeResult();

...,但它不起作用。我收到一条消息 (node:10700) UnhandledPromiseRejectionWarning: Error: Invalid value at 'requests[0]' (oneof), oneof field 'kind' is already set. Cannot set 'addConditionalFormatRule'

我相信你的目标如下。

  • 您想使用“aaa”或“b”的值将条件格式规则设置为“A1:E2”的范围。
  • 您想使用 Node.js 的 googleapis 实现此目的。
  • 您已经能够使用表格 API.
  • 为 Google 电子表格获取和放置值

修改点:

  • 在您的请求正文中,未使用 updateCells
  • 在你的情况下,我认为 CUSTOM_FORMULA 而不是 ONE_OF_LIST 可能更合适。

当这些要点反映到你的request body中,就变成了下面这样。

修改后的脚本:

async function colorizeResult() {
  res = await gsapi.spreadsheets.batchUpdate({
    spreadsheetId: spreadsheetIdCurr,
    requestBody: {
      requests: [
        {
          addConditionalFormatRule: {
            rule: {
              booleanRule: {
                condition: {
                  type: "CUSTOM_FORMULA",
                  values: [
                    {
                      userEnteredValue: `=REGEXMATCH(A1,"${checkData.values.map(e => `^${e}$`).join("|")}")`, // Modified
                    },
                  ],
                },
                format: {
                  textFormat: {
                    bold: true,
                    foregroundColor: {
                      red: 1,
                      green: 1,
                      blue: 1,
                    },
                  },
                  backgroundColor: {
                    red: 0.1,
                    green: 0.3,
                    blue: 1,
                  },
                },
              },
              ranges: [
                {
                  sheetId: 0,
                  startColumnIndex: 0,
                  endColumnIndex: 5,
                  startRowIndex: 0,
                  endRowIndex: 2,
                },
              ],
            },
          },
        },
      ],
    },
  });
}

注:

  • 当这个脚本多次运行时,添加了条件格式规则。在这个答案中,我修改了您的请求正文以删除您当前的问题。例如,我想如果你想更新现有的条件格式规则,你也可以使用 UpdateConditionalFormatRuleRequest.

  • 此修改后的脚本支持您已经能够使用表格 API 为 Google 电子表格获取和放置值。请注意这一点。

  • 如果userEnteredValue: `=REGEXMATCH(A1,"${checkData.values.map(e => `^${e}$`).join("|")}")`,对您的实际情况没有用,请修改为userEnteredValue: `=OR(${checkData.values.map(e => `A1="${e}"`).join(",")})`,再测试。我认为这两个脚本将是相同的结果。

参考文献: