import gspread
import json
import time


#ServiceAccountCredentials：Googleの各サービスへアクセスできるservice変数を生成します。
from oauth2client.service_account import ServiceAccountCredentials 

#2つのAPIを記述しないとリフレッシュトークンを3600秒毎に発行し続けなければならない
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']

#認証情報設定
#ダウンロードしたjsonファイル名をクレデンシャル変数に設定（秘密鍵、Pythonファイルから読み込みしやすい位置に置く）
credentials = ServiceAccountCredentials.from_json_keyfile_name('japanreturn2021-dfb378370788.json', scope)

#OAuth2の資格情報を使用してGoogle APIにログインします。
gc = gspread.authorize(credentials)


#共有設定したスプレッドシートキーを変数[SPREADSHEET_KEY]に格納する。
SPREADSHEET_KEY = '1XY91NRniC9CjKKMddogol4bLsr16i1_w7vz1i50zs88'


#### 前提条件 #######################
workbook = gc.open_by_key(SPREADSHEET_KEY)
####################################

#初期設定
worksheet = workbook.worksheet('Pension')
settingsheet = workbook.worksheet('setting')

num = int(settingsheet.acell('B1').value)
font_type= "GenShinGothic"

#タイムスタンプ判断
judge = str(worksheet.acell('D'+ str(num)).value)
s = str(worksheet.acell('A'+ str(num)).value)
if len(judge) == 0:
	print('NULL')
else:
	num = int(s.removeprefix('DA211'))
	import sppdf01
	time.sleep(5)
	import sppdf02
	import sppdf03
	# メール送信処理
	import mailpy
	num = num +1
	settingsheet.update_acell('B1', num)

#import sppdf01
#time.sleep(5)
#import sppdf02
#import sppdf03
# メール送信処理
#import mailpy


