same_row = self.row == other.row 属性错误:'NoneType'对象没有属性'row'与 gspread



我正在尝试编写一个discord bot程序,该程序链接到谷歌表单,但我的代码中遇到了无数错误。我得到的错误如下:

Traceback (most recent call last):
File "/home/runner/Bundeswehr-Bot/venv/lib/python3.8/site-packages/discord/ui/modal.py", line 186, in _scheduled_task
await self.on_submit(interaction)
File "/home/runner/Bundeswehr-Bot/modals.py", line 45, in on_submit
if sheet.find(self.host.value) == None:
File "/home/runner/Bundeswehr-Bot/venv/lib/python3.8/site-packages/gspread/cell.py", line 44, in __eq__
same_row = self.row == other.row
AttributeError: 'NoneType' object has no attribute 'row'

我的代码是:

class bct_modal(discord.ui.Modal, title="BCT Logging"):
host = discord.ui.TextInput(
label='Host ID',
placeholder='The hosts discord ID here...',
required=True
)
async def on_submit(self, interaction: discord.Interaction):
ids = []
ids.append(self.host.value)
print(ids)
if sheet.find(self.host.value) == None:
sheet.append_row([self.host.value, 0, 0, 0, 0, 0, 0, 0, 0])
for id in ids:
bct_host_col = sheet.find("Hosted BCT").col
cell_row = sheet.find(id).row
sheet.update_cell(cell_row, bct_host_col, int(sheet.cell(cell_row, bct_host_col).value) + 1)
elif sheet.find(self.host.value) != None:
print("This ID is already in the sheet.")

如有任何帮助,我们将不胜感激。

注意:当我输入一个尚未在谷歌电子表格中的ID时,它会按照我想要的方式完美地附加它,但如果我尝试输入一个已经在电子表格中了的ID,它会给我带来这个错误。

在您的脚本中,我认为可能需要修改if sheet.find(self.host.value) == None:。因此,==就是is。我想这可能是你发布'NoneType' object has no attribute 'row'的原因。

我认为在这种情况下,这个线程可能很有用。

那么,下面的修改怎么样?

发件人:

if sheet.find(self.host.value) == None:

收件人:

if sheet.find(self.host.value) is None:
  • 当我测试您的脚本时,我可以与您复制相同的问题。而且,当这个修改反映在脚本中时,我确认错误已经删除

参考:

  • 相关线程:Python无比较:我应该使用"是";或者==

在这种情况下,这一点很重要的原因是,如果x的类存在__eq__方法,并且gspreadCell类实现了一个自定义__eq__方法,该方法尝试对other对象(other.row(进行无保护属性访问,则Python将x == y转换为x.__eq__(y),如果other不是Cell(在这种情况下,它会抛出错误,但传入另一个具有row属性的类可能会有不同的行为(,则可能会产生意外的结果。

虽然这对gspread开发人员来说是非常糟糕的做法,但如果使用is作为与None进行比较的规则,则可以避免这种做法。

相关内容

最新更新