在母表Python/googledriveapi中仅使用高亮显示的单元格创建新表

2024-06-17 15:48:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个母工作簿,view here(https://docs.google.com/spreadsheets/d/13UnBY3umkojHMd9K9PYv8zITu253vO_ipHNxLvE5u9Q/edit#gid=521061986),其中我只保留了“星期六”和“星期天”的工作表。我已将此电子表格复制到驱动器中,并突出显示我感兴趣的事件(将填充颜色更改为黄色)。我想创建一个代码,根据我突出显示的事件创建一个新的“日程表”。。。保留黄色事件,但将其他事件设置为null并删除空行

到目前为止,我已经成功地连接到我的数据库,但我不知道如何识别他们的填充颜色的细胞

import os
import gspread
from oauth2client.service_account import ServiceAccountCredentials


DATA_DIR = '/path/here/'
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive',
         'https://www.googleapis.com/auth/spreadsheets']
path = os.path.join(DATA_DIR, 'client_secret.json')
creds = ServiceAccountCredentials.from_json_keyfile_name(path, scope)
client = gspread.authorize(creds)


spreadsheet = client.open('dcon').sheet1

sheets = ['Saturday', 'Sunday']
# deleted every other sheet except Saturday & Sunday since that's when I'll be there


def get_sheet_colors(spreadsheet, ranges: list):
    params = {'ranges': ranges,
              'fields': 'sheets(data(rowData(values(effectiveFormat/backgroundColor,formattedValue)),'
                        'startColumn,startRow),properties(sheetId,title))'}
    return spreadsheet.get(**params).execute()

desiredA1NotationRanges = ['Sunday''!A1:K2', 'Saturday''!B2:D4']
# these are example ranges
get_sheet_colors(spreadsheet, desiredA1NotationRanges)

我尝试了从另一个StackOverflow问题中提取的函数,但无法对“worksheet”对象运行“get”。有没有人对此有什么想法,或者有没有更简单的方法来确定我感兴趣的事件,这样代码就更容易编写了


Tags: pathhttpsimportcomclientgetheregoogle