Ruby 条件格式 get badRequest: 无效请求[0]: 未设置请求。 (谷歌::Apis::客户端错误)

标签 ruby google-sheets gs-conditional-formatting

我正在尝试格式化 Google 表格。这是基于 Ruby 的 Google API 文档、条件格式和 Ruby 文档(非常差)的代码。

到目前为止,我还没有找到任何关于如何使代码适应 Ruby 的完整文档。

终端返回:

`check_status': badRequest: Invalid requests[0]: No request set. (Google::Apis::ClientError)

您知道如何使此代码工作吗?

requests.push({
      add_conditional_formats: {
        rule: {
          ranges: [
            {
              sheet_id: 0, start_column_index: 9, end_column_index: 100,
            }
          ],
          gradient_rule: {
            minpoint: {
              color: {
                green: 0.2,
                red: 0.8
              },
              type: "MIN"
            },
            maspoint: {
              color: {
                green: 0.9
              },
              type: "MAX"
            },
          }
        },
        index: 0
      },
      add_conditional_formats: {
        rule: {
          ranges: [
            {
              sheet_id: 0, start_column_index: 9, end_column_index: 100,
            }
          ],
          gradient_rule: {
            minpoint: {
              color: {
                green: 0.2,
                red: 0.8
              },
              type: "NUMBER",
              value: 0
            },
            maspoint: {
              color: {
                green: 0.9
              },
              type: "NUMBER",
              value: 256
            },
          }
        },
        index: 1
      }
    })

    body = {requests: requests}
    result = service.batch_update_spreadsheet(spreadsheet_id, body, {})

最佳答案

这个修改怎么样?此修改后的脚本假设您的访问 token 可用于更新电子表格。

修改点:

  • add_conditional_format_rule 是一个请求。因此,在您的情况下,requests 数组需要为 2 个元素,因为您在请求中使用了 2 个 add_conditional_format_rule
  • maspointmaxpoint
  • add_conditional_formatsadd_conditional_format_rule
  • sheet_id 的值为字符串。
  • gradient_rulevalue 值为字符串。

修改后的脚本:

requests = []
requests.push({
      add_conditional_format_rule: {
        rule: {
          ranges: [
            {
              sheet_id: "0", start_column_index: 9, end_column_index: 100,
            }
          ],
          gradient_rule: {
            minpoint: {
              color: {
                green: 0.2,
                red: 0.8
              },
              type: "MIN"
            },
            maxpoint: {
              color: {
                green: 0.9
              },
              type: "MAX"
            },
          }
        },
        index: 0
      }
    },{
      add_conditional_format_rule: {
        rule: {
          ranges: [
            {
              sheet_id: "0", start_column_index: 9, end_column_index: 100,
            }
          ],
          gradient_rule: {
            minpoint: {
              color: {
                green: 0.2,
                red: 0.8
              },
              type: "NUMBER",
              value: "0"
            },
            maxpoint: {
              color: {
                green: 0.9
              },
              type: "NUMBER",
              value: "256"
            },
          }
        },
        index: 1
      }
    })

body = {requests: requests}
result = service.batch_update_spreadsheet(spreadsheet_id, body, {})

引用:

如果这不是您想要的,我很抱歉。

关于Ruby 条件格式 get badRequest: 无效请求[0]: 未设置请求。 (谷歌::Apis::客户端错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50667671/

相关文章:

ruby-on-rails - 通过关联在 has_many 中的连接表中添加一个额外的字段

sorting - Google Script 排序故障排除(作为 2 个函数使用但不合并)

google-apps-script - 谷歌脚本: How to implement conditional formatting into spreadsheet

javascript - 使用google脚本操作数据条件格式和数据验证

google-sheets - 如果另一列的任何单元格中都存在值,则突出显示单元格

ruby-on-rails - 在应用程序 html.erb 中使用 Controller 变量

ruby-on-rails - 不同 Controller 中的 link_to 'new' Action ?

javascript - Stripe 支付金额

javascript - 如何从谷歌电子表格中获取谷歌文档中的段落并创建文档的链接?

javascript - 代码运行速度太慢