【问题标题】:Google Sheets Api, python. How to access specific cell data谷歌表格 API,python。如何访问特定的单元格数据
【发布时间】:2021-09-22 12:09:46
【问题描述】:

我目前正在为大学做项目,我需要使用谷歌表格和 python 创建一个基本的银行系统。我已成功添加客户数据并检查数据是否正确。但是,我希望了解如何根据客户的电子邮件地址检查特定客户的余额。

Email Address Full Name Password Balance
brian_smith@outlook.com brian Smith Password1234 500

这是我的代码。

def emailcheck(emailinput):
    """
    Email validator function
    """
    try:
        valid = validate_email(emailinput)
        emailinput = valid.email
        return emailinput
    except EmailNotValidError as e:
        print("The email you provided is not valid please try again\n")



def registerEmail():
    """
    This fucntion checks the email address is correct 
    while loop will continue until a valid emai is input
    """

    print("Welcome to retro bank As a new customer\n" +
        "Please complete the following fileds to sign up for an account\n")

    while True:
        try:
            global email
            email = input("Please enter your email address: ")
            einput = emailcheck(email)
            if einput != email:
                raise ValueError(
                    "Please enter a valid email" +
                    f" you entered: {email}"
                )
            else:
                regDetails()
                return email
        except ValueError as e:
            print(f"Invalid data: {e}, please try again.\n")



def regDetails():

    sheet = SHEET.worksheet("customer-data")
    new_cust = []
    bonus = 500

    
    while True:
        name = input("Please enter your full name: ")
        password = input("Please enter your password: ")
        if name == "":
            print("Name is required")
        elif password == "":
            print("Password Required")
        else:
            new_cust.append(email)
            new_cust.append(name)
            new_cust.append(password)
            new_cust.append(bonus)
            sheet.append_row(new_cust)
            print("WELCOME TO RETRO BANK !" +
            " As a new customer you will receive £500 joining bonus")
            login()
            break



def login():
    """
    This fucntion will allow the exisiting user 
    log into their bank account
    """
    global email_ver
    global details
    global password
    global balances
    email_ver = SHEET.worksheet("customer-data")
    details = email_ver.col_values(1)
    password = email_ver.col_values(3)
    balances = email_ver.col_values(4)
    user = []

    while True:
        ename = input("Please enter your email address: ")
        epass = input("Please enter your password: ")
        if ename  == "":
            print("Name is required")
        elif epass == "":
            print("Password Required")
        else:
            break

    user.append(ename)
    user.append(epass)

    found = 0

    for i in zip(details, password):
        if i == tuple(user):
            found = 1
            print("Successfully Verified")
            mainDash(user)
            return found
    if found == 0:
        print("The username or the password you provided might be wrong.\n")
        login()
        return found


def mainDash(user):
    print(user)
    print("WELCOME")

    for i in (details):
        if i == user:
            print()

【问题讨论】:

  • 考虑阅读 API 文档。

标签: python google-sheets gspread


【解决方案1】:

如 Ethan J 所述,阅读 documentation 会有所帮助。

请参阅下面的简单函数,该函数将包含用户的行作为列表返回。如果未找到用户,则返回 None。请注意,此函数在输入中区分大小写。

注意,您可能需要将返回的行偏移 1。试试看它是否返回正确的行。

def find_user(sheet, email):
    emails = sheet.col_values(1)
    if email in emails:
        # https://docs.gspread.org/en/latest/user-guide.html#getting-all-values-from-a-row-or-a-column
        return sheet.row_values(emails.index(email))
    else:
        return None

【讨论】:

  • 谢谢!!是的,我已经设法解决了这个问题。我通读了文档,经过一番挖掘后找到了答案。
猜你喜欢
  • 2021-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多