【发布时间】:2020-11-18 21:03:21
【问题描述】:
我正在编写一个新的 python 脚本,该脚本需要从谷歌表格中提取数据,但是有许多单元格被合并/组合,并且只有来自此合并的左上角单元格具有值。在所有合并的单元格上都有该值很重要。
我该怎么做?
Python 3.8.5 + gspread 3.6.0
注意:每条评论“试图获取...”,其下方的代码应返回与之前代码相同的值。
电子表格测试: https://docs.google.com/spreadsheets/d/17Dyxufu1y1ouBCPkf5Y7Vt1UW70WroK0Moy_DD7bZKc/edit?usp=sharing
重现问题的代码:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import os
import pprint
here = os.path.dirname(os.path.abspath(__file__))
secret = os.path.join(here, 'credentials.json')
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name(secret, scope)
client = gspread.authorize(creds)
sheet = client.open_by_key('17Dyxufu1y1ouBCPkf5Y7Vt1UW70WroK0Moy_DD7bZKc')
ws = sheet.sheet1
pp = pprint.PrettyPrinter()
#getting the FIRST text
result = ws.acell('A1')
pp.pprint('A1: '+result.value)
#trying to get the SAME text on the cell col+1
result = ws.acell('A2')
pp.pprint('A2: '+result.value)
#getting the 'simple_cell'
result = ws.acell('C2')
pp.pprint('C2: '+result.value)
#getting the 'row_merged'
result = ws.acell('D2')
pp.pprint('D2: '+result.value)
#trying to get 'row_merged' on row+1
result = ws.acell('E2')
pp.pprint('E2: '+result.value)
#getting the 'col_merged'
result = ws.acell('D6')
pp.pprint('D6: '+result.value)
#trying to get 'col_merged' on col+1
result = ws.acell('D7')
pp.pprint('D7: '+result.value)
输出是这样的:
('A1: just to confirm, the value "row_merged" has been put to cell D2 originally. Value "col_merged" is in D6 initially. Since it's merged, the expected result should be found on all merged cells')
'A2: '
'C2: simple cell'
'D2: row_merged'
'E2: '
'D6: col_merged'
'D7: '
PS C:\Users\joaov\Desktop>
重点是:A2 必须等于 A1...E2=D2,D7==D6...但是似乎没有办法用 gspread 处理合并的单元格。
【问题讨论】:
-
你有机会发minimal reproducible example吗?
-
@RandomDavis 确定测试电子表格是:docs.google.com/spreadsheets/d/… 代码是:hastebin.com/umuvecajir.apache 输出:hastebin.com/wuliqihasa.rust 注意:每条评论“试图获得...”,它下面的代码应该返回与前面代码相同的值。
-
我应该澄清一下,您的示例应该是独立的,即不像您那样托管在外部站点上。输出也是如此,它必须在您的问题本身中。它应该是一些 Python 代码,我们可以粘贴到 IDE 中,运行它,然后查看问题。如果您必须伪造或“模拟”任何传入数据,那很好,只要它向我们展示了同样的问题。我建议使用tour 以更好地了解该网站希望问题的格式。
-
我很抱歉,@RandomDavis,我没想过用代码等来编辑我的主要问题。我现在就去编辑它,希望你能帮忙我的问题:)
-
那很好,因为我或其他人可能会在这种情况下提供帮助。
标签: python google-sheets google-sheets-api gspread