How to use the gspread.utils function in gspread

To help you get started, we’ve selected a few gspread examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github maybelinot / df2gspread / df2gspread / df2gspread.py View on Github external
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

    # Addition of col names
    if col_names:
        cell_list = wks.range('%s%s:%s%s' % (first_col, start_row, last_col, start_row))
        for idx, cell in enumerate(cell_list):
            cell.value = df.columns.astype(str)[idx]
        wks.update_cells(cell_list)

    # Addition of row names
    if row_names:
        cell_list = wks.range('%s%s:%s%d' % (
            start_col, first_row, start_col, last_idx))
        for idx, cell in enumerate(cell_list):
            cell.value = df.index.astype(str)[idx]
        wks.update_cells(cell_list)
github maybelinot / df2gspread / df2gspread / df2gspread.py View on Github external
except:
        gfile_id = get_file_id(credentials, gfile, write_access=True)

    # Tuple of rows, cols in the dataframe.
    # If user did not explicitly specify to resize sheet to dataframe size
    # then for new sheets set it to new_sheet_dimensions, which is by default 1000x100
    if df_size:
        new_sheet_dimensions = (len(df), len(df.columns))
    wks = get_worksheet(gc, gfile_id, wks_name, write_access=True,
        new_sheet_dimensions=new_sheet_dimensions)
    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:
github Robin-Lord / vietnambot-the-third / tasks.py View on Github external
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)

    # Formatting today's date to match the dates in the sheet
    today_formatted=datetime.strptime(today, '%Y-%m-%d').strftime('%d/%m/%Y')
    print (app_code,location_code,sublocation,"today_formatted is: ", today_formatted)
    #-----#
github maybelinot / df2gspread / df2gspread / gspread2df.py View on Github external
"Trying to open non-existent or inaccessible spreadsheet")

    wks = get_worksheet(gc, gfile_id, wks_name)

    if wks is None:
        raise RuntimeError(
            "Trying to open non-existent or inaccessible worksheet")

    raw_data = wks.get_all_values()

    if not raw_data:
        raise ValueError(
            'Worksheet is empty or invalid.'
            )

    start_row_int, start_col_int = gspread.utils.a1_to_rowcol(start_cell)

    rows, cols  = np.shape(raw_data)
    if start_col_int > cols or (row_names and start_col_int + 1 > cols):
        raise RuntimeError(
            "Start col (%s) out of the table columns(%s)" % (start_col_int +
                                                            row_names, cols))

    if start_row_int > rows or (col_names and start_row_int + 1 > rows):
        raise RuntimeError(
            "Start row (%s) out of the table rows(%s)" % (start_row_int +
                                                            col_names, rows))

    raw_data = [row[start_col_int-1:] for row in raw_data[start_row_int-1:]]

    if row_names and col_names:
        row_names = [row[0] for row in raw_data[1:]]
github Robin-Lord / vietnambot-the-third / tasks.py View on Github external
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
    # the most prolific orderer of late
    return row_val