我使用此请求代码将这些复选框插入到列中,但我还需要将它们默认全部设置为 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());
最佳答案
不必同时使用 repeatCell
和 updateCells
请求,而是可以使用单个 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/