Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if clean:
wks = clean_worksheet(wks, gfile_id, wks_name, credentials)
start_col = re.split('(\d+)',start_cell)[0].upper()
start_row = re.split('(\d+)',start_cell)[1]
start_row_int, start_col_int = gspread.utils.a1_to_rowcol(start_cell)
# find last index and column name (A B ... Z AA AB ... AZ BA)
num_rows = len(df.index) + 1 if col_names else len(df.index)
last_idx_adjust = start_row_int - 1
last_idx = num_rows + last_idx_adjust
num_cols = len(df.columns) + 1 if row_names else len(df.columns)
last_col_adjust = start_col_int - 1
last_col_int = num_cols + last_col_adjust
last_col = re.split('(\d+)',(gspread.utils.rowcol_to_a1(1, last_col_int)))[0].upper()
# If user requested to resize sheet to fit dataframe, go ahead and
# resize larger or smaller to better match new size of pandas dataframe.
# Otherwise, leave it the same size unless the sheet needs to be expanded
# to accomodate a larger dataframe.
if df_size:
wks.resize(rows=len(df.index) + col_names, cols=len(df.columns) + row_names)
if len(df.index) + col_names + last_idx_adjust > wks.row_count:
wks.add_rows(len(df.index) - wks.row_count + col_names + last_idx_adjust)
if len(df.columns) + row_names + last_col_adjust > wks.col_count:
wks.add_cols(len(df.columns) - wks.col_count + row_names + last_col_adjust)
# Define first cell for rows and columns
first_col = re.split('(\d+)',(gspread.utils.rowcol_to_a1(1, start_col_int + 1)))[0].upper() if row_names else start_col
first_row = str(start_row_int + 1) if col_names else start_row
# Checking existing orders to give idea of current order state
# Creating range of last 30 and next two rows to check recent orders (the next two being a failsafe in case someone ordered at the same time)
print (app_code,location_code,sublocation," started order number check")
# Current row number plus two
row_plus_2=int(row)+2
print (app_code,location_code,sublocation,"row_plus_2 is: ", row_plus_2)
# Current row number minus 30
row_minus_30=int(row)-30
print (app_code,location_code,sublocation,"row_minus_30 is: ", row_minus_30)
# Getting the first cell in the row-30
past_30=gspread.utils.rowcol_to_a1(row_minus_30, 1)
print (app_code,location_code,sublocation,"past_30 is: ", past_30)
# Getting the first cell in the row+2
next_2=gspread.utils.rowcol_to_a1(row_plus_2, 1)
print (app_code,location_code,sublocation,"next_2 is: ", next_2)
# Creating our range within the first column from row-30 to row+2
dates=past_30+":"+next_2
# Returns a list of everything in the date column in that range
dates_range=orders1.range(dates)
print (app_code,location_code,sublocation,"dates_range is: ", dates_range)
#Getting today's date
today=str(date.today())
print (app_code,location_code,sublocation,"today is: ", today)
# Updating name on sheet using name value passed in function
# Getting the second cell in our row
moniker_cell=gspread.utils.rowcol_to_a1(row_val, 2)
print (app_code,location_code,sublocation,"moniker_cell is", moniker_cell)
# Using the value we got from our database as the name to put here
worksheet.update_acell(moniker_cell, user_name)
print (app_code,location_code,sublocation," added user_name: ", user_name)
# Updating food cell in sheet using food value passed in function
# Getting the third cell
food_cell=gspread.utils.rowcol_to_a1(row_val, 3)
# Putting in the food value we got from API.AI in this session
worksheet.update_acell(food_cell, food)
print (app_code,location_code,sublocation," added food: ", food)
# Updating time cell in sheet using current time
# Getting the fifth cell as where we'll put the time
time_cell=gspread.utils.rowcol_to_a1(row_val, 5)
# Getting the current time with datetime.now().time() and putting that value in the cell we selected
worksheet.update_acell(time_cell, datetime.now().time())
print (app_code,location_code,sublocation," added time: ", datetime.now().time())
# We give back the row value because that's later used to calculate things like
def flush(self):
ws = self.parent.get_spreadsheet()
num_columns = len(self.buffer[0])
num_lines = len(self.buffer)
ws.resize(rows=num_lines, cols=num_columns)
cell_list = ws.range( 'A1:%s' % rowcol_to_a1(num_lines, num_columns) )
for cell in cell_list:
val = self.buffer[cell.row-1][cell.col-1]
# if type(val) is str:
# val = val.decode('utf-8')
cell.value = val
ws.update_cells(cell_list)
self.buffer = []
def update_cell(self, row, col, value):
"""Sets the new value to a cell.
:param row: Row number.
:param col: Column number.
:param value: New value.
Example::
worksheet.update_cell(1, 1, '42')
"""
range_label = '%s!%s' % (self.title, rowcol_to_a1(row, col))
data = self.spreadsheet.values_update(
range_label,
params={
'valueInputOption': 'USER_ENTERED'
},
body={
'values': [[value]]
}
)
return data
#//////////////////////////////////////////////////////#
#------------------------------------------------------#
sublocation= "B (update_sheet) - "
#Proess for updating the first empty row in the sheet thanks to (https://gspread.readthedocs.io/en/latest/)
print (app_code,location_code,sublocation," started update_sheet action ")
#finding row of first empty cell
print (app_code,location_code,sublocation," calling available_row function ")
row_val=next_available_row(app_code,location_code,worksheet)
print (app_code,location_code,sublocation," row val is: ", row_val)
# Selecting the first cell in our row by using row_val and column 1 (this is the cell we want to update with the date)
date_cell=gspread.utils.rowcol_to_a1(row_val, 1)
print (app_code,location_code,sublocation,"date cell is ", date_cell)
# This gets today's date
today=str(date.today())
print (app_code,location_code,sublocation,"today is ", today)
# This formats today's date to be easily readable by humans
today_formatted=datetime.strptime(today, '%Y-%m-%d').strftime('%d/%m/%Y')
print (app_code,location_code,sublocation,"today formatted is ", today_formatted)
# This updates the cell we selected
worksheet.update_acell(date_cell, today_formatted)
print (app_code,location_code,sublocation," added today's date: ", today)
# Updating name on sheet using name value passed in function
def update_cell(self, row, col, value):
"""Updates the value of a cell.
:param row: Row number.
:type row: int
:param col: Column number.
:type col: int
:param value: New value.
Example::
worksheet.update_cell(1, 1, '42')
"""
range_label = '%s!%s' % (self.title, rowcol_to_a1(row, col))
data = self.spreadsheet.values_update(
range_label,
params={
'valueInputOption': 'USER_ENTERED'
},
body={
'values': [[value]]
}
)
return data