-
-
Save ikemo3/a52e85070e0c0536db512793ddeabc68 to your computer and use it in GitHub Desktop.
Google Sheets APIの例
This file contains 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
#!/usr/local/bin/python3 | |
import httplib2 | |
from googleapiclient import discovery | |
from oauth2client.service_account import ServiceAccountCredentials | |
SCOPES = "https://www.googleapis.com/auth/spreadsheets" | |
CREDENTIALS_FILE = "credentials.json" | |
SPREADSHEET_ID = "..." | |
def get_credentials(): | |
"""サービスアカウントで認証する""" | |
return ServiceAccountCredentials.from_json_keyfile_name(CREDENTIALS_FILE, SCOPES) | |
def main(): | |
credentials = get_credentials() | |
http = credentials.authorize(httplib2.Http()) | |
discovery_url = 'https://sheets.googleapis.com/$discovery/rest?version=v4' | |
service = discovery.build('sheets', 'v4', http=http, discoveryServiceUrl=discovery_url) | |
values = [ | |
[ | |
"8", "2", "3" | |
] | |
] | |
body = { | |
"values": values | |
} | |
range_name = "A:C" | |
value_input_option = "USER_ENTERED" | |
sheet = service.spreadsheets() | |
sheet.values().append(spreadsheetId=SPREADSHEET_ID, range=range_name, valueInputOption=value_input_option, | |
insertDataOption="INSERT_ROWS", body=body).execute() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment