jeudi 16 juillet 2020

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. The checkbox is been create successfully but unfortunately without any value (When unchecked we expect 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.




Aucun commentaire:

Enregistrer un commentaire