google-apps-script - 如何请求 updateCells 使多行具有相同的值

标签 google-apps-script google-sheets-api

我使用此请求代码将这些复选框插入到列中,但我还需要将它们默认全部设置为 true。到目前为止,我已经看到了具有多个“值”的示例,每一行一个,但我想知道是否有一种方法可以仅声明一次并且已经为某个范围内的所有其他值设置了

var resource = {"requests": [
    {"repeatCell": {
      "cell": {"dataValidation": {"condition":{"type": "BOOLEAN"}}},
      "range": {"sheetId": sheetId, "startRowIndex": 1, "endRowIndex": 300, "startColumnIndex": 18},
      "fields": "dataValidation"
      }
    },
    {"updateCells": {
      "rows": {"values": {"userEnteredValue": {"boolValue": true}}},
      "range": {"sheetId": sheetId, "startRowIndex": 1, "endRowIndex": 300, "startColumnIndex": 18},
      "fields": "userEnteredValue"
      }
    }
  ]};
  Sheets.Spreadsheets.batchUpdate(resource, ss.getId());

最佳答案

不必同时使用 repeatCellupdateCells 请求,而是可以使用单个 repeatCell 请求来更改“数据验证”和所需范围的“用户输入值”属性。关键是“fields”参数(表示要修改的属性)和实际属性都必须包含(或故意省略,以便删除)。

指定范围内的所有单元格(R2C19:R301C19,因为“_____Index”表示 -> 0-base)将被修改为使用您请求中找到的指定属性:

var resource = {"requests": [
  {"repeatCell": {
    "cell": {
      "dataValidation": {"condition":{"type": "BOOLEAN"}},
      "userEnteredValue": {"boolValue": true}
    },
    "range": {
      "sheetId": sheetId,
      "startRowIndex": 1,
      "endRowIndex": 300,
      "startColumnIndex": 18,
      "endColumnIndex": 19 // Specify the end to insert only one column of checkboxes
    },
    "fields": "dataValidation,userEnteredValue"
  }
}]};
Sheets.Spreadsheets.batchUpdate(resource, ss.getId());

请注意,如果省略 endColumnIndex,则 GridRange被解释为无界:

All indexes are zero-based. Indexes are half open, e.g the start index is inclusive and the end index is exclusive -- [startIndex, endIndex). Missing indexes indicate the range is unbounded on that side.

引用文献:

关于google-apps-script - 如何请求 updateCells 使多行具有相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51389147/

相关文章:

java - 使用 SpreadSheet API 时无法完成 HTTP 请求

google-apps-script - 获取所有 Google Script 触发器并删除它们

javascript - 谷歌应用脚​​本侧边栏表单发送按钮不起作用

google-apps-script - 当 Google 任务标记为 "completed"时如何触发 Google Script

google-apps-script - 使用 Google App 脚本删除一定数量的过滤器 View

javascript - 如何通过 Chrome Identity 使用登录帐户

google-apps-script - onOpen 触发器未在 Google 表单上激活

google-sheets-api - 是否可以访问谷歌电子表格的行ID?

flutter - 如何在 Google 表格中接受可变数量的参数

java - 如何从 url 中提取 id?谷歌表格