Created
November 6, 2020 11:55
-
-
Save HidemotoKisuwa/3c967b6d434532f914ca688378278318 to your computer and use it in GitHub Desktop.
Insert Data Through API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import httplib2 | |
import os | |
from apiclient import discovery | |
from google.oauth2 import service_account | |
try: | |
scopes = ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/spreadsheets"] | |
secret_file = os.path.join(os.getcwd(), 'client_secret.json') | |
spreadsheet_id = '1EC_FIA2ZpydWxBsuUaQIUUpcYk3mm6VYWMs4Q4cn26c' | |
range_name = 'Sheet1!A1:D2' | |
credentials = service_account.Credentials.from_service_account_file(secret_file, scopes=scopes) | |
service = discovery.build('sheets', 'v4', credentials=credentials) | |
values = [ | |
['a1', 'b1', 'c1', 123], | |
['a2', 'b2', 'c2', 456], | |
] | |
data = { | |
'values' : values | |
} | |
service.spreadsheets().values().update(spreadsheetId=spreadsheet_id, body=data, range=range_name, valueInputOption='USER_ENTERED').execute() | |
except OSError as e: | |
print e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment