python gspread 库只写入标有“sheet1"的工作表 [英] python gspread library only writes to worksheet labeled 'sheet1'

查看:51
本文介绍了python gspread 库只写入标有“sheet1"的工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作表名为doc_name",它有两个工作表,sheet1"和sheet2".但是,我只能将数据写入标有sheet1"的工作表?

My sheet is named 'doc_name', and it has two worksheets, 'sheet1' and 'sheet2'. but, i can only write data to the worksheet labeled 'sheet1'?

这是限制还是我做错了什么?

is this a limitation or am i doing something wrong?

这行得通,

wks = gc.open("doc_name").sheet1

但这失败了,

wks = gc.open("doc_name").sheet2

出现这个错误,

AttributeError: 'Spreadsheet' 对象没有属性 'sheet2'

我也注意到这失败了,

wks = gc.open("doc_name").Sheet1

wks = gc.open("doc_name").Sheet1

...其中我使用大写的S".. 并且只有在我指定小写时才会写入.sheet1

...where i use a capital 'S'.. and it will only write if i specify lowercase .sheet1

如何在无需编码的情况下写入工作表... wks = gc.open("doc_name").sheet1?

how do i write to a worksheet without having to code... wks = gc.open("doc_name").sheet1?

推荐答案

这是因为 gspread 仅实现了 sheet1 来让您检索电子表格中的第一张工作表 作为快捷方式.

This is because gspread only implemented sheet1 to let you retrieve the first sheet in your spreadsheet as a shortcut.

源代码,您可以看到 sheet1 的实现是使用 get_worksheet(0)

From the source code you can see the implementation of sheet1 is using get_worksheet(0)

@property
def sheet1(self):
    """Shortcut property for getting the first worksheet."""
    return self.get_worksheet(0)

因此,如果您想检索其他工作表,则需要使用其他方法,例如:

So if you want to retrieve other sheets, you need to use other methods like:

1.将index指定为一个整数,指示从0开始打开的工作表的位置:

1.specify index as a integer indicating the position of the sheet to open starting from 0:

wks = gc.open("doc_name").get_worksheet(index)

2.指定要作为字符串打开的工作表的title:

2.specify title of the sheet to open as a string:

wks = gc.open("doc_name").worksheet(title)

也就是说,在你的情况下,你可能可以使用sheet2

That is to say, in you case, to get sheet2 you can probably use

wks = gc.open("doc_name").get_worksheet(1)

这篇关于python gspread 库只写入标有“sheet1"的工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆