2016-12-15 8 views
3

あなたの助けが必要です。私はこれを理解しようとしており、存在しないシートを読み込むときに与えられたHttpErrorをキャプチャしようとするurllib2などを試しています。だからここHandling SheetsAPIエラー - Python 2.7

は今、最初の呼び出し元のコード

discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?''version=v4') 
service = discovery.build('sheets', 'v4', http=http, discoveryServiceUrl=discoveryUrl) 
result = service.spreadsheets().values().get(spreadsheetId=spreadsheetId, range=rangeName).execute() 
values = result.get('values', []) 

if not values: 
    print('No data found.') 
    tkMessageBox.showwarning("ERROR", "There is nothing on this page!") 
    LoadCSV() 
else: 

大丈夫です。私は存在していないシートを呼び出すと、私はエラーを処理し、ここで

「を試してみません複数のシートを」と言って警告を表示するエラーです:

私はこのエラーを処理するにはどうすればよい
HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/(ID)/values/130%21A2%3AI?alt=json returned "Unable to parse range: 130!A2:I"> 

そのページが存在しないことの警告とプログラムの終了を指示します。

答えて

1

sheets-api-quickstartを使用すると、googleapiclient.errors.HttpErrorが表示されます。これは私のために働きます:

import googleapiclient 

discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?''version=v4') 
service = discovery.build('sheets', 'v4', http=http, discoveryServiceUrl=discoveryUrl) 
try: 
    # The `execute()` call triggers the exception. 
    result = service.spreadsheets().values().get(spreadsheetId=spreadsheetId, range=rangeName).execute() 
    # deceptively named, custom HttpError 
except googleapiclient.errors.HttpError: 
    print "page does not exist" 
else: 
    values = result.get('values', []) 

    if not values: 
     print('No data found.') 
     tkMessageBox.showwarning("ERROR", "There is nothing on this page!") 
     LoadCSV() 
    else: 
+0

こんにちはエリオット、私はこれを実装しており、素晴らしい作品です。私はクイックスタートとそれが提供するリファレンスを見ました。私はTry/Exceptを見た、私もHttpErrorを試してみた。ちょうどAPIのエラーコードを知らなかった。私はエラーコード400を処理しようとしましたが、運がありませんでした。多くの感謝。 – Joel