import gspread
import datetime

def update_sheet(setting, mse, mae):
    # print(mse.keys())
    gc = gspread.service_account()
    sh = gc.open("test")

    # worksheet = sh.get_worksheet(6)

    worksheet = sh.add_worksheet(title=str(datetime.datetime.now()), rows=100, cols=100)

    channel = len(mse)
    # i = 1
    # while True:
        
    #     if worksheet.get(f'A{i}') == [[]]:
    #         break
    #     else:
    #         i += 1

    # for row in range(1, channel+1):
    #     if row == 1:
    #         worksheet.update_cell(i, 1, setting)
    #     else:
    #         worksheet.update_cell(i+row-1, 1, "Channel "+str(row-1))
    
    # worksheet.update_cell(i+channel, 1, "Average")

    # for j in range(1, channel):
    #     worksheet.update_cell(i+j, 2, mse[j])
    #     worksheet.update_cell(i+j, 3, mae[j])
    # worksheet.update_cell(i+channel, 2, mse['avg'])
    # worksheet.update_cell(i+channel, 3, mae['avg'])

    worksheet.update(f"A1:C{1+channel}", [[setting, '', ''], *[["Channel "+str(i), mse[i], mae[i]] for i in range(1, channel)], ["Average", mse['avg'], mae['avg']]])