creating worskeet added
diff --git a/config.py b/config.py
index f6867ac..1fbb576 100644
--- a/config.py
+++ b/config.py
@@ -1,7 +1,8 @@
-EMAIL = "xxx2@gmail.com"
-PASSWORD = "yyyy"
+EMAIL = "glebstepanov1992@gmail.com"
+PASSWORD = "stgleb12021992"
DOCUMENT_ID = "1Xvd0aHA7mr-_b5C3b-sQ66BQsJiOGIT2UesP7kG26oU"
-SHEET_NAME = ""
+SHEET_NAME = "aaa"
WORK_SHEET = "Worksheet"
COL_COUNT = 2
+ROW_COUNT = 10
DEFAULT_FILE_PATH="test.json"
\ No newline at end of file
diff --git a/make_report.py b/make_report.py
index 8a8a0d0..96bb622 100644
--- a/make_report.py
+++ b/make_report.py
@@ -2,7 +2,9 @@
import argparse
import json
-from config import DEFAULT_FILE_PATH, PASSWORD, EMAIL, COL_COUNT, WORK_SHEET, DOCUMENT_ID
+from config import DEFAULT_FILE_PATH, PASSWORD, EMAIL, \
+ WORK_SHEET, DOCUMENT_ID, ROW_COUNT
+from gspread.exceptions import WorksheetNotFound
def load_data(file_name):
@@ -11,21 +13,53 @@
return json.loads(data)
+#getting worksheet from sheet or create it with specified column names.
+def get_work_sheet(sheet, name, column_names):
+ try:
+ work_sheet = sheet.worksheet(name)
+ except WorksheetNotFound:
+ work_sheet = sheet.add_worksheet(title=name, rows=ROW_COUNT,
+ cols=len(column_names))
+
+ for i in range(1, len(column_names) + 1):
+ work_sheet.update_cell(1, i, column_names[i - 1])
+
+ return work_sheet
+
+
+def get_row_number(work_sheet):
+ num = 2
+
+ while num < work_sheet.row_count and work_sheet.cell(num, 1).value != "":
+ num += 1
+
+ if num == work_sheet.row_count:
+ work_sheet.append_row(["" for x in range(work_sheet.col_count)])
+
+ return num
+
+
+def append_row(work_sheet, row):
+ row_number = get_row_number(work_sheet)
+
+ i = 1
+ for k in row.keys():
+ work_sheet.update_cell(row_number, i, row[k])
+ i += 1
+
+
def make_report(data):
gc = gspread.login(EMAIL, PASSWORD)
sh = gc.open_by_key(DOCUMENT_ID)
- worksheet = sh.add_worksheet(title=WORK_SHEET, rows=len(data.keys()), cols=COL_COUNT)
- i = 1
- for k in data.keys():
- worksheet.update_cell(i, 1, k)
- worksheet.update_cell(i, 2, data[k])
- i += 1
+ work_sheet = get_work_sheet(sh, WORK_SHEET, data.keys())
+ append_row(work_sheet, data)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
- parser.add_argument('-n', '--name', help='data file path', default=DEFAULT_FILE_PATH)
+ parser.add_argument('-n', '--name', help='data file path',
+ default=DEFAULT_FILE_PATH)
results = parser.parse_args()
data = load_data(results.name)
make_report(data)