[英]How to create a checkbox with value with Python using gspread and gspread_formatting
I'm using google spreadsheets threw python and I want to create a checkbox.我正在使用谷歌电子表格抛出 python 并且我想创建一个复选框。 The checkbox is been create successfully but unfortunately without any value (When unchecked we expect FALSE).
该复选框已成功创建,但不幸的是没有任何值(未选中时,我们期望 FALSE)。 In the meanwhile, I have the following code
同时,我有以下代码
from time import localtime, time, sleep
from typing import Iterable, Dict, List, Set, Tuple
from gspread_formatting import DataValidationRule, BooleanCondition, set_data_validation_for_cell_range
import gspread
from oauth2client.service_account import ServiceAccountCredentials
validation_rule = DataValidationRule(
BooleanCondition('BOOLEAN', ['TRUE', 'FALSE']), # condition'type' and 'values', defaulting to TRUE/FALSE
showCustomUi=True)
def update_sheet(sheet: 'Google_WorkSheet', org_name: str, error_type: str, num_of_rows: int) -> None:
"""
This function updates a worksheet by inserting the parameters
in the last row of the sheet
:param sheet: the worksheet we modify
:param org_name: the organization name to put in the row
:param error_type: the FinOps error type to put in the row
:param num_of_rows: the number of rows in the sheet
:return: None
"""
current_time = localtime(time()) # current_time : time.struct
current_time = '{0}/{1}/{2}'.format(current_time[2], current_time[1], current_time[0]) # current_time : str
note = check_error_type(error_type)
new_row = [current_time, org_name, error_type, note]
sheet.append_row(new_row)
set_data_validation_for_cell_range(sheet, f'G{num_of_rows + 1}', validation_rule) # inserting checkbox
This code creates the checkbox successfully but with no value in it.此代码成功创建了复选框,但其中没有任何值。 I know that I can use the
gspread.initialize_spreadsheet().worksheet().update_cell()
But I have a limited number of API calls so I don't want to use it.我知道我可以使用
gspread.initialize_spreadsheet().worksheet().update_cell()
但我的 API 调用数量有限,所以我不想使用它。 For example:例如:
This checkbox has been created by the script above but we can see that there is no value in it.这个复选框是由上面的脚本创建的,但我们可以看到其中没有任何值。
In this case, the method of batchUpdate in Sheets API is used for putting the checkbox.在这种情况下,Sheets API 中的 batchUpdate 方法用于放置复选框。 But the method of
set_data_validation_for_cell_ranges
uses the batch update request for only creating checkbox.但是
set_data_validation_for_cell_ranges
的方法使用批量更新请求只创建复选框。 Ref It seems that in this method, several batch requests cannot be included. Ref似乎在这种方法中,无法包含几个批处理请求。 By this, the initial value cannot be seen.
由此,无法看到初始值。 So in order to put the checkbox and give the initial value using one API call, in this answer, I would like to propose to use the method of
batch_update
in gspread.因此,为了使用一个 API 调用来放置复选框并给出初始值,在这个答案中,我想建议使用
batch_update
中的 batch_update 方法。 The sample script is as follows.示例脚本如下。
spreadsheetId = "###" # Please set the Spreadsheet ID.
sheetName = "Sheet1" # Please set the sheet name.
client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(spreadsheetId)
sheet = spreadsheet.worksheet(sheetName)
sheetId = sheet._properties['sheetId']
requests = {"requests": [
{
"repeatCell": {
"cell": {"dataValidation": {"condition": {"type": "BOOLEAN"}}},
"range": {"sheetId": sheetId, "startRowIndex": 0, "endRowIndex": 3, "startColumnIndex": 0, "endColumnIndex": 3},
"fields": "dataValidation"
}
},
{
"updateCells": {
"rows": [
{"values": [{"userEnteredValue": {"boolValue": True}}, {"userEnteredValue": {
"boolValue": False}}, {"userEnteredValue": {"boolValue": False}}]},
{"values": [{"userEnteredValue": {"boolValue": True}}, {"userEnteredValue": {
"boolValue": True}}, {"userEnteredValue": {"boolValue": False}}]},
{"values": [{"userEnteredValue": {"boolValue": True}}, {"userEnteredValue": {
"boolValue": True}}, {"userEnteredValue": {"boolValue": True}}]}
],
"start": {"rowIndex": 0, "columnIndex": 0, "sheetId": sheetId},
"fields": "userEnteredValue"
}
}
]}
res = spreadsheet.batch_update(requests)
print(res)
When above sample script is run, the checkboxes are put to the cells "A1:C3" on "Sheet1" with the initial values like below.运行上述示例脚本时,复选框将放置在“Sheet1”上的单元格“A1:C3”中,初始值如下所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.