首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不允许我将数据写入我的Google工作表

问不允许我将数据写入我的Google工作表
EN

Stack Overflow用户
提问于 2022-11-20 19:59:25
回答 1查看 37关注 0票数 1

密码在下面。这不是完整的代码,而是它的基础。我试图从Twitter的API中获取数据,并将其写入我的。以下是守则。

代码语言:javascript
复制
from googleapiclient import discovery
from google.oauth2 import service_account
from google.oauth2.credentials import Credentials 
from googleapiclient.discovery import build


SERVICE_ACCOUNT_FILE = 'twitter.json' #json File should be in the same folder as this Python Script.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']

creds = None
creds = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = '1f4iqVHljytmRSC2EZcApEiA2lE1cHa1o6Fr4s3t0AKg' 
#SAMPLE_RANGE_NAME = 'Class Data!A2:A60'

service = build('sheets', 'v4', credentials=creds)
sheet = service.spreadsheets()

# Call the Sheets API
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
                                   range='twitterData!A1:A180').execute()
#ERROR HERE BELOW
request = sheet.values().update(spreadsheetId = SAMPLE_SPREADSHEET_ID,
                                  range = 'twitterData!B2:B180', valueInputOption='USER_ENTERED', body={"values": 8}).execute()

得到错误的一行im在下面的错误上面有注释。这就是错误信息:

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:\Users\John Doe\Desktop\Code\Python\Twitter\test.py", line 27, in <module>
    request = sheet.values().update(spreadsheetId = SAMPLE_SPREADSHEET_ID,
  File "C:\Users\John Doe\AppData\Local\Programs\Python\Python38-32\lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "C:\Users\John Doe\AppData\Local\Programs\Python\Python38-32\lib\site-packages\googleapiclient\http.py", line 938, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/1f4iqVHljytmRSC2EZcApEiA2lE1cHa1o6Fr4s3t0AKg/values/twitterData%21B2%3AB180?valueInputOption=USER_ENTERED&alt=json returned "Invalid value at 'data.values' (type.googleapis.com/google.protobuf.ListValue), 8". Details: "[{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'data.values', 'description': "Invalid value at 'data.values' (type.googleapis.com/google.protobuf.ListValue), 8"}]}]">

我以前使用过。我不知道为什么这个代码不起作用。只有一句台词。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-21 01:29:10

我认为你目前发行"Invalid value at 'data.values' (type.googleapis.com/google.protobuf.ListValue), 8"的原因是body={"values": 8}。在这种情况下,需要使用二维数组.因此,请按以下方式修改。

发自:

代码语言:javascript
复制
request = sheet.values().update(spreadsheetId = SAMPLE_SPREADSHEET_ID,
                                  range = 'twitterData!B2:B180', valueInputOption='USER_ENTERED', body={"values": 8}).execute()

至:

代码语言:javascript
复制
request = sheet.values().update(spreadsheetId = SAMPLE_SPREADSHEET_ID, range = 'twitterData!B2:B180', valueInputOption='USER_ENTERED', body={"values": [[8]]}).execute()
  • 在这个修改中,body={"values": 8}被修改为body={"values": [[8]]}。
  • 通过这种修改,8被放入"twitterData“工作表的单元格"B2”中。

参考资料:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74511522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档