如何请求多行的updateCells具有相同的值

问题描述 投票:0回答:1

我正在使用此请求代码在列中插入这些复选框,但我还需要将它们都默认设置为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());
google-apps-script google-sheets-api
1个回答
1
投票

可以使用单个repeatCell请求来改变所需范围的“数据验证”和“用户输入值”属性,而不是同时使用updateCellsrepeatCell请求。关键是必须包含(或有意省略,删除)的“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());

请注意,如果省略endColumnIndexGridRange将被解释为无界:

所有索引都是从零开始的。索引是半开放的,例如起始索引是包含性的,结束索引是独占的 - [startIndexendIndex)。缺少索引表明该范围在该方面是无限制的。

参考文献:

© www.soinside.com 2019 - 2024. All rights reserved.