如何在Python中对整个Google表格进行批处理请求?

发布于 2025-01-20 19:29:08 字数 627 浏览 1 评论 0 原文

使用 Python,我尝试检查 Google 表格中的每个单元格是否有删除线,并返回没有删除线的行。我遇到的问题是达到当前分钟的配额限制,因为我正在检查 150 多行。另外,我需要检查 11 张纸,每张纸有 50 到 200 行。

有没有办法“batchGet()”格式化,而不仅仅是值,这样我就不会达到配额限制?

我不确定真正需要什么示例代码,所以这是我的工作“这个单元格是否用删除线格式化”定义:

def row_has_strikethrough(sheet, i):
    return 'strikethrough=True' in str(get_user_entered_format(sheet, 'A' + str(i)))

这在我的 while 循环中有效,但我再次达到了配额:

last_row = int(sheet.acell('C1').value)  # 166
i = 3
while i <= last_row:
    if not row_has_strikethrough(sheet, i):
        records += get_record(sheet, MAPPING, i)
    i += 1

任何帮助将不胜感激!

Using Python, I'm trying to check each cell in a Google Sheet for a strikethrough and return the rows without a strikethrough. The problem I run into is reaching my quota limit for the current minute since I'm checking 150+ rows. Plus I need to check 11 sheets with anywhere from 50-200 rows each.

Is there a way to "batchGet()" formatting, not just values, so I don't hit my quota limit?

I'm not sure what example code is really needed so here's my working "is this cell formatted with a strikethrough" definition:

def row_has_strikethrough(sheet, i):
    return 'strikethrough=True' in str(get_user_entered_format(sheet, 'A' + str(i)))

This works within my while loop but again, I hit the quota:

last_row = int(sheet.acell('C1').value)  # 166
i = 3
while i <= last_row:
    if not row_has_strikethrough(sheet, i):
        records += get_record(sheet, MAPPING, i)
    i += 1

Any help would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2025-01-27 19:29:08

我相信您的目标如下。

  • 关于,我正在尝试检查Google表中的每个单元格中的罢工,并在没有罢工的情况下返回行。,从您的返回'strikethrough = true'str(get_user_entered_format(get_user_entered_format)(表格,'a' + str(i))),我认为您可能想检查列“ A”的单元格值是否具有罢工。
  • 而且,从您的显示脚本来看,我认为您可能正在为Python使用Gspread。
  • 您想降低脚本的过程成本。

如果我的理解是正确的,那么以下示例脚本怎么样?

示例脚本:

spreadsheetId = '###' # Please set your Spreadsheet ID.
client = gspread.authorize(credentials) # Here, please use your credentials and client
sheetNames = ['Sheet1', 'Sheet2'] # Please set the sheet names you want to retrieve the values.

# 1. Retrieve textFormat from each sheet using one API call.
access_token = credentials.access_token
ranges = '&'.join(['ranges=' + e for e in sheetNames])
url = 'https://sheets.googleapis.com/v4/spreadsheets/' + spreadsheetId + '?fields=sheets(data(rowData(values(userEnteredFormat(textFormat(strikethrough))))))&' + ranges
res1 = requests.get(url, headers={'Authorization': 'Bearer ' + access_token})
obj = res1.json()
rowNumbers = {}
for i, d in enumerate(obj['sheets']):
    sheet = []
    for j, r in enumerate(d['data'][0].get('rowData')):
        if 'values' not in r:
            sheet.append(j)
    rowNumbers[sheetNames[i]] = sheet

# 2. Retrieve values from each sheet using one API call.
spreadsheet = client.open_by_key(spreadsheetId)
res2 = spreadsheet.values_batch_get(sheetNames)['valueRanges']

# 3. Retrieve the rows from each sheet by checking the rows without the strikethrough at the column "A".
values = {}
for i, s in enumerate(res2):
    temp = []
    for j, r in enumerate(s['values']):
        sheet = sheetNames[i]
        if j in rowNumbers[sheet]:
            temp.append(r)
    values[sheet] = temp

print(values) # Here, you can see the result value.

在此脚本中,使用了2个API调用API。

测试:

在样本电子表格中使用此脚本时,将获得以下结果。

{
  'Sheet1': [['a2', 'b2', 'c2'], ['a5', 'b5', 'c5'], ['a6', 'b6', 'c6']],
  'Sheet2': [['a3', 'b3', 'c3'], ['a5', 'b5', 'c5'], ['a6', 'b6', 'c6']]
}

该输出值具有每个表的行。这些行是“ a”列的没有罢工的行。

参考:

  • https://developers.google.com/sheets/api/reference/Rest/Rest/v4/spreadsheets/get”

I believe your goal is as follows.

  • About I'm trying to check each cell in a Google Sheet for a strikethrough and return the rows without a strikethrough., from your script of return 'strikethrough=True' in str(get_user_entered_format(sheet, 'A' + str(i))), I thought that you might have wanted to check whether the cell value of column "A" has the strikethrough.
  • And, from your showing script, I thought that you might be using gspread for python.
  • You want to reduce the process cost of the script.

If my understanding is correct, how about the following sample script?

Sample script:

spreadsheetId = '###' # Please set your Spreadsheet ID.
client = gspread.authorize(credentials) # Here, please use your credentials and client
sheetNames = ['Sheet1', 'Sheet2'] # Please set the sheet names you want to retrieve the values.

# 1. Retrieve textFormat from each sheet using one API call.
access_token = credentials.access_token
ranges = '&'.join(['ranges=' + e for e in sheetNames])
url = 'https://sheets.googleapis.com/v4/spreadsheets/' + spreadsheetId + '?fields=sheets(data(rowData(values(userEnteredFormat(textFormat(strikethrough))))))&' + ranges
res1 = requests.get(url, headers={'Authorization': 'Bearer ' + access_token})
obj = res1.json()
rowNumbers = {}
for i, d in enumerate(obj['sheets']):
    sheet = []
    for j, r in enumerate(d['data'][0].get('rowData')):
        if 'values' not in r:
            sheet.append(j)
    rowNumbers[sheetNames[i]] = sheet

# 2. Retrieve values from each sheet using one API call.
spreadsheet = client.open_by_key(spreadsheetId)
res2 = spreadsheet.values_batch_get(sheetNames)['valueRanges']

# 3. Retrieve the rows from each sheet by checking the rows without the strikethrough at the column "A".
values = {}
for i, s in enumerate(res2):
    temp = []
    for j, r in enumerate(s['values']):
        sheet = sheetNames[i]
        if j in rowNumbers[sheet]:
            temp.append(r)
    values[sheet] = temp

print(values) # Here, you can see the result value.

In this script, 2 API calls of Sheets API are used.

Testing:

When this script is used in a sample Spreadsheet, the following result is obtained.

{
  'Sheet1': [['a2', 'b2', 'c2'], ['a5', 'b5', 'c5'], ['a6', 'b6', 'c6']],
  'Sheet2': [['a3', 'b3', 'c3'], ['a5', 'b5', 'c5'], ['a6', 'b6', 'c6']]
}

This output value has the rows of each sheet. Those rows are the rows without the strikethrough at column "A".

References:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文