Python 从指向另一个单元格的单元格中获取实际值
我试图创建一个脚本来处理google电子表格,但有时,当我使用命令Python 从指向另一个单元格的单元格中获取实际值,python,python-3.x,google-sheets,google-sheets-api,gspread,Python,Python 3.x,Google Sheets,Google Sheets Api,Gspread,我试图创建一个脚本来处理google电子表格,但有时,当我使用命令val=worksheet.cell(1,2).value,实际值类似于=M7,因此,它没有脚本的实际值。是否有任何可能的解决方法从“M7”单元格中获取内容并将其复制到我正在阅读的单元格中 提供了一份样本表 示例代码: import gspread from oauth2client.service_account import ServiceAccountCredentials scope = ['https://spread
val=worksheet.cell(1,2).value
,实际值类似于=M7
,因此,它没有脚本的实际值。是否有任何可能的解决方法从“M7”单元格中获取内容并将其复制到我正在阅读的单元格中
提供了一份样本表
示例代码:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds']
spreadsheet_id = '1sfltKJ1-EBlXJReDSmb5KiqeNSXbHuuLH2d2O1_Qfyc'
credentials = ServiceAccountCredentials.from_json_keyfile_name('C:\credentials.json', scope)
client = gspread.authorize(credentials)
wb = client.open_by_key(spreadsheet_id)
ws = None
sheets = [s for s in wb.worksheets() if s.id == 0]
if sheets:
ws = sheets[0]
values = ws.get_all_values(value_render_option='FORMULA')
print(values)
# if you run this code, you will see that for gspread, the stirng 'original_value' is only in the B2 cell.
# When the gspread module reads the D2 cell, it shows actually the formula, that is =B2
# Also, I need to used the param 'value_render_option='FORMULA'', because sometimes there is a hyperlink formula in the cell,
# so sometimes I need to read the real content of the cell.
# I hope you understand the formula
正在恢复,如果运行上述代码,您将获得以下列表:
[['', '', '', ''], ['', 'original_value', '', '=B2']]
但预期产出应为:
[['', '', '', ''], ['', 'original_value', '', 'original_value']]
更新:
我已经为这个问题创建了一个解决方案,因此如果这对任何人都有用,代码如下:
def column2number(col):
num = 0
for c in col:
if c in string.ascii_letters:
num = num * 26 + (ord(c.upper()) - ord('A')) + 1
return num
values = ws.get_all_values(value_render_option='FORMULA')
for i in range(len(values)):
for j in range(len(values[i])):
# this will check a combination of any number of uppercase letters followed by any number of numbers, representing the column and the line, respectively.
p = re.compile('^=[A-Z]+[0-9]+')
try:
# if the variable p is not null, we will retrieve the matched regex from the string, split it, and get only the letters.
r = p.match(str(values[i][j])).group()
output = re.split('(\d+)',r[1:])
# this function 'column2number' will convert the column letters to its position numerically
output[0] = column2number(output[0]) - 1
output[1] = int(output[1]) - 1
output[2] = i
output.append(j)
# the output is filled, respectivelly by: referenced column, referenced line, line where the content will be placed, column where the content will be placed
values[i][j] = values[output[1]][output[0]]
except AttributeError:
pass
print(values)
此解决方法将用M7单元格中的现有内容替换引用公式,如
=M7
。我相信您的目标如下
- 您希望使用gspread检索单元格值
- 您不想检索公式
- 当我看到
的文档时,当使用get\u all\u values
时,当单元格具有公式时,将检索公式。因此,当您想要检索公式中的单元格值时,请使用value\u render\u option='FORMULA'
和value\u render\u option='FORMATTED\u value'
。value\u render\u option='UNFORMATTED\u value'
- 当我看到gspread的脚本时,似乎默认值是
李>FORMATTED\u VALUE
- 当我看到gspread的脚本时,似乎默认值是
- 在此示例脚本中,将检索值和公式,当公式中包含
时,将检索公式。我认为这种方法可能是一种简单的方法=hyperlink
value\u render\u option='FORMULA'
来读取超链接公式。谢谢感谢您的回复和添加更多信息。根据您更新的问题,我提出了一个修改点作为答案。你能确认一下吗?如果我误解了你的问题,而这不是你期望的结果,我道歉。你好@Tanaike,你好吗?我已经用一个可能的解决方案更新了我的帖子,你觉得这个解决方案怎么样?谢谢。我想更改渲染选项,但正如我所说的,如果我更改该选项,我将无法获得超链接公式。。例如:使用value\u render\u option='FORMULA'
我得到=hyperlink(“https://google.com“;“google”)
,如果没有该选项,我只会将google
作为输出。。。看见使用此选项会打断脚本的一部分,而不使用此选项会打断另一部分。@João Casarin感谢您的回复。我为我糟糕的英语水平和糟糕的技能深表歉意。我没有注意到你脚本中的注释行。从您的回复中,我又添加了一个示例脚本作为答案。你能确认一下吗?如果这不是你期望的结果,我再次道歉。谢谢你的回答。。。我感谢您努力帮助我,但运行您的代码时,我在c:中的“=hyperlink”一行中遇到了一个错误:TypeError:int类型的参数不适用
。。。但没关系!但是我能够使用regex和模块字符串为这个创建一个解决方案,我现在将更新我的帖子,添加我认为的算法!谢谢你的帮助@若昂·卡萨林感谢您的回复。不幸的是,我不能理解你的剧本。因为我建议的脚本和更新的脚本的结果值都相同。而且,如果c::TypeError中的“=hyperlink”:类型为“int”的参数不可iterable,则我无法复制,我会在行中得到一个错误。我再次为我的拙劣技能道歉。我可以问一下复制问题的详细信息吗?通过这一点,我想确认一下。
values = ws.get_all_values(value_render_option='FORMULA')
values = ws.get_all_values()
values = ws.get_all_values(value_render_option='UNFORMATTED_VALUE')
values = ws.get_all_values()
formulas = ws.get_all_values(value_render_option='FORMULA')
for i, r in enumerate(formulas):
for j, c in enumerate(r):
if '=hyperlink' in c:
values[i][j] = c
print(values)