简体   繁体   English

如何使用 gspread 在工作表中搜索符合多个条件的项目?

[英]How do you search a sheet using gspread for an item matching multiple criteria?

I'm trying to develop a Python script to search a sheet for a particular row, matching multiple criteria.我正在尝试开发一个 Python 脚本来搜索特定行的工作表,匹配多个条件。 I'll start with a sample scenario, a fruits menu in Google Sheets.我将从示例场景开始,即 Google 表格中的水果菜单。 See screenshot below.请参见下面的屏幕截图。 Note, this example might or might not reflect actual prices.请注意,此示例可能反映也可能不反映实际价格。

In gspread , I would normally use the find function to search for a particular item (eg cell = worksheet.find("Apple") ) and from that I can find out which row the item is located in. However, this might only find the row for the first "Apple" entry that appears.gspread中,我通常会使用find function 来搜索特定项目(例如cell = worksheet.find("Apple") ),然后我可以找出该项目位于哪一行。但是,这可能只会找到出现的第一个“Apple”条目所在的行。 In the sample below, it'll find the "green apple" and its corresponding row - Row 2.在下面的示例中,它会找到“青苹果”及其对应的行 - 第 2 行。

What if I'm interested in the row number of a fruit matching a certain criteria?如果我对符合特定条件的水果的行号感兴趣怎么办? For example, Apple, Red , which is located in Row 6. How do I search a sheet using gspread ( find function) to find a particular row of something matching 2 or more criteria?例如,位于第 6 行的Apple, Red 。如何使用gspreadfind函数)搜索工作表以查找符合 2 个或更多条件的特定行? In the example, I want to return the row number containing the red apple (Row 6), which seems to appear after the first apple entry (green apple).在示例中,我想返回包含红苹果的行号(第 6 行),它似乎出现在第一个苹果条目(绿苹果)之后。

水果菜单

EDIT:编辑:

  • Solved / Answered (Code Snippet Below)已解决/已回答(下面的代码片段)
  • Thanks @Tanaike谢谢@Tanaike
# OUTPUT: Code below updates Cell D6 with the letter "X"
def itemRow(sheet):
    searchColA = "Apple"
    searchColB = "Red"
    row = [i+1 for i, r in enumerate(sheet.get_all_values()) if 
           r[0] == searchColA and r[1] == searchColB][0]
    sheet.update("D" + str(row), "X")

When I saw the document of find of gspread, it says in_column (int) – (optional) One-based column number to scope the search.当我看到gspread的find文档时,它说in_column (int) – (optional) One-based column number to scope the search. . . In this case, unfortunately, it seems that only one column is used for searching values.不幸的是,在这种情况下,似乎只有一列用于搜索值。 And, when I saw the script of gspread, in the case of find method, it seems that a value is searched by retrieving all values from the sheet.而且,当我看到 gspread 的脚本时,在find方法的情况下,似乎是通过从工作表中检索所有值来搜索一个值。 Ref When find is used 2 times, your goal can be achieved. Reffind被使用 2 次时,您的目标就可以实现。 But, in this case, 2 API calls are required.但是,在这种情况下,需要调用 2 个 API。 So, I thought that in your situation, when the above flow is reflected for achieving your goal, your goal can be achieved by one API call.所以,我想在你的情况下,当上面的流程体现为实现你的目标时,一个API电话就可以实现你的目标。 In this case, how about the following sample script?在这种情况下,下面的示例脚本怎么样?

Sample script:示例脚本:

client = ### # Please use your client.

searchColA = "Apple" # This is used for searching column "A".
searchColB = "Red" # This is used for searching column "B".
spreadsheetId = "###" # Please set your spreadsheet ID.
sheetName = "Sheet1" # Please set your sheet name.

spreadsheet = client.open_by_key(spreadsheetId)
sheet = spreadsheet.worksheet(sheetName)
res = [i + 1 for i, r in enumerate(sheet.get_all_values()) if r[0] == searchColA and r[1] == searchColB]

print(res) # Found row numbers. In this case, 1st row number is 1.

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM