Python Google Sheets API未设置值(无错误)

bqujaahr  于 8个月前  发布在  Python
关注(0)|答案(1)|浏览(77)

我有这段代码(下面)来检查一个谷歌工作表的随机值,返回它,并设置另一个值。检查工作表的所有繁重工作,唯一的问题是update方法。使用调试,我看到它确实使它成为更新方法,它只是不工作。没有错误消息。此外,范围设置为https://www.googleapis.com/auth/spreadsheets。提前感谢!

def getitem(ctxuid):
    creds = None
    if os.path.exists("token.json"):
        creds = Credentials.from_authorized_user_file("token.json", SCOPES)
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
          creds.refresh(Request())
        else:
          flow = InstalledAppFlow.from_client_secrets_file(
              "credentials.json", SCOPES
          )
          creds = flow.run_local_server(port=0)
    with open("token.json", "w") as token:
        token.write(creds.to_json())

    try:
        service = build("sheets", "v4", credentials=creds)

        # Call the Sheets API
        sheet = service.spreadsheets()
        result = (
            sheet.values()
            .get(spreadsheetId=SPREADSHEET_ID, range="Sheet1!A2:C101")
            .execute()
        )
        values = result.get("values", [])
        available = []
        for i in range(len(values)):
            if values[i][2] == "Available":
                available.append(values[i])
        choice = random.choice(range(len(available)))
        if ctxuid in calamity_allowSheetUpdate:
            cal_vals = (
                #(available[choice][0],available[choice][1], 'test')
                "ASHOFHSAOFOHISA"
                )
            cal_vals_rangeBody = {
            'majorDimension': "ROWS",
            'values': cal_vals
            }
            sheet.values().update(
                spreadsheetId=SPREADSHEET_ID,range=f"Sheet1!A{values.index(available[choice])+2}",valueInputOption="USER_ENTERED",body=cal_vals_rangeBody) #CHECK FOR AVAILABLE VALUE IN REGULAR VALUES, THEN UPDATE TO "Unavailable" IN RED
            # print(f"Sheet1!A{values.index(available[choice])+2}",cal_vals_rangeBody)
        return f"### The shaboingery gods have chosen...\n# {available[choice][0]}\n## {available[choice][1]}"

字符串
我该如何解决此问题?
提前感谢您的帮助!
编辑:所需的文件,credentials.jsontoken.json存在且正确。

2wnc66cl

2wnc66cl1#

我忘了把.execute()后我的更新方法。

相关问题