-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathgoogle_sheets_tool.py
120 lines (101 loc) · 4.27 KB
/
google_sheets_tool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from __future__ import print_function
import pickle
import os.path
import argparse
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from po2csv import po2dict, dict2PO
# NOTE
# author: [email protected]
# delete the token.pickle under this directory
# then, run the script to download sheet from google spreadsheet
#
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = '1iZa4wCIyTDlIRYoW7229YoZWKZ0lmIiOFsCJG3ZVw-s'
def google_get_creds():
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w', encoding='utf8') as token:
token.write(creds.to_json())
return creds
def google_get_po_items(locale_code):
SAMPLE_RANGE_NAME = locale_code + '!A:C'
creds = google_get_creds()
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range=SAMPLE_RANGE_NAME).execute()
values = result.get('values', [])
po_items = {}
if not values:
print('No data found.')
else:
i = 2
for row in values[1:]:
# Print columns A and E, which correspond to indices 0 and 4.
#print('%s, %s, %s' % (row[0], row[1], row[2]))
#print(row)
msgid = row[0][1:-1]
msgstr = ''
contrib = ''
if (len(row)>1):
msgstr = row[1][1:-1]
if (len(row)>2):
contrib = row[2]
po_items[msgid] = [msgstr, contrib]
i = i + 1
return po_items
def google_remove_po_items(locale_code, po_items):
creds = google_get_creds()
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
remove_rows = []
for msgid, val in po_items.items():
remove_rows.append(locale_code + '!A' + str(val[0]))
remove_rows.append(locale_code + '!B' + str(val[0]))
remove_rows.append(locale_code + '!C' + str(val[0]))
if (len(remove_rows) > 0):
batch_clear_values_request_body = { 'ranges': remove_rows }
result = sheet.values().batchClear(spreadsheetId=SAMPLE_SPREADSHEET_ID,
body=batch_clear_values_request_body).execute()
def google_append_po_items(locale_code, po_items):
creds = google_get_creds()
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
value_input_option = 'RAW'
insert_data_option = 'INSERT_ROWS'
value_range_body = {}
for msgid, val in po_items.items():
result = sheet.values().append(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range=range_, valueInputOption=value_input_option,
insertDataOption=insert_data_option,
body=value_range_body).execute()
parser = argparse.ArgumentParser(description='Google Sheets Utils')
parser.add_argument('--output',required=True, dest='output_po_file', help='path of an output po_file that is created from Google Sheet')
parser.add_argument('locale_code', type=str, help='local code: e.g. es, zh_CN, de')
args = parser.parse_args()
if __name__ == "__main__":
locale_code = args.locale_code
output_po = args.output_po_file
google_items = google_get_po_items(locale_code)
dict2PO(google_items, output_po)