Beispiel #1
0
def get_sheets(workbook: Spreadsheet) -> dict:
    """Googleスプレッドシート内にあるシートのリストを取得する

    Args:
        workbook (Spreadsheet): `gspread`で定義されている`Spreadsheet`モデル

    Returns:
        dict: 取得結果 `data`は`gspread`で定義されている`Worksheet`モデルのリスト
        e.g. {'action:': create_sheet, 'result': 'Success', 'message': 'Created a sheet: {sheet title}', 'data': list}
    """
    action = 'get_sheet_titles'
    response = init_response(action)
    sheets = workbook.worksheets()
    if sheets:
        response = {
            'action:': action,
            'result': 'Success',
            'message': 'Got the sheets.',
            'data': sheets
        }
    else:
        response = {
            'action:': action,
            'result': 'Failure',
            'message': 'Any sheet does not exist.',
            'data': None
        }
    return response
def build_sheet_dict(wb_name: str, wb: Spreadsheet) -> Tuple[str, SheetDict]:
    global COUNTER
    print(f"Building sheet_dict for \'{wb.title}\'...")
    sheet_dict: SheetDict = {}
    worksheets: List[Worksheet] = wb.worksheets()
    for i, ws in enumerate(worksheets):
        entries_dict = {}
        entries_dict['sheet'] = ws
        entries_dict['sheet_idx'] = i

        sheet_dict[ws.title] = entries_dict

    with open(SHEET_DICT_TEMPLATE, 'r') as f:
        contents = f.read()
        template_dict: SheetDict = json.loads(contents)

    for sheet in sheet_dict:
        sheet_dict[sheet]['header_info_dict'] = template_dict[sheet][
            'header_info_dict']

    COUNTER += 1
    print(f"{COUNTER}. \'{wb.title}\' template built!")
    return wb_name, sheet_dict