【发布时间】:2020-05-05 11:43:47
【问题描述】:
我想通过 Google Drive API 使用 Google Cloud Function 读取/写入 Google Sheet。我尝试将以下代码部署为 Cloud Function(由 Pub/Sub 触发):
def hello_pubsub(event, context):
from googleapiclient.discovery import build
import google.auth
credentials, project_id = google.auth.default(scopes=['https://www.googleapis.com/auth/spreadsheets'])
service = build('sheets', 'v4', credentials=credentials)
spreadsheet_id = 'my_spreadsheet_id_goes_here'
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=spreadsheet_id,range="A1:B4").execute()
values = result.get('values', [])
for value in values:
print(value)
range_ = 'A7'
request = service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=range_, valueInputOption='RAW', insertDataOption='OVERWRITE', body={"values":[['test cell input']]})
response = request.execute()
print(response)
if __name__ == '__main__':
hello_pubsub('a', 'b')
我在 Google Cloud Platform > 日志记录中遇到了几个错误: 使用 oauth2client >= 4.0.0 时,file_cache 不可用 自动检测中的文件“/env/local/lib/python3.7/site-packages/google_api_python_client-1.8.0-py3.7.egg/googleapiclient/discovery_cache/init.py”,第 36 行 from google.appengine.api import memcache ModuleNotFoundError: No module named 'google.appengine'
我可以从终端本地运行相同的脚本,并且运行良好。
感谢任何帮助或想法。同样,我想从 Cloud Function 读取/写入 Google Sheet。这是我自己的 Google 表格,通过同一个 Google Cloud 帐户运行。
【问题讨论】:
-
你的
requirements.txt文件是什么? -
@guillaumeblaquiere 为空,我不确定它应该包含什么,因为我在 pip freeze 中看不到“googleapiclient”或“google”
-
我的回答是用良好的文本格式更清晰。让我知道它是否有效。
标签: python google-sheets google-cloud-platform google-cloud-functions