Skip to content

Instantly share code, notes, and snippets.

@ikemo3

ikemo3/sheets.py Secret

Created August 27, 2019 12:56
Show Gist options
  • Save ikemo3/a52e85070e0c0536db512793ddeabc68 to your computer and use it in GitHub Desktop.
Save ikemo3/a52e85070e0c0536db512793ddeabc68 to your computer and use it in GitHub Desktop.
Google Sheets APIの例
#!/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