gspread icon indicating copy to clipboard operation
gspread copied to clipboard

I am trying to push my list in a range of cells but not able to get the range right.

Open isrj5 opened this issue 8 years ago • 6 comments

I have a list which i want to update in my range of cells along the column. Iterating live data over rows. but i am not able to get the range right.

import gspread
import os
import datetime
import time

from gspread_dataframe import get_as_dataframe


from oauth2client.service_account import ServiceAccountCredentials
 
#to find next available row 
def next_available_row(sheet):
    str_list = list(filter(None, sheet.col_values(1)))  # fastest
    return str(len(str_list)+1)

# use creds to create a client to interact with the Google Drive API
def update_spreadsheet(data_list):
    
    scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']
    basedir = os.path.abspath(os.path.dirname(__file__))
    DATA_JSON = basedir+'/'+'client_secret.json'
    creds = ServiceAccountCredentials.from_json_keyfile_name(DATA_JSON, scope)
    
    client = gspread.authorize(creds)
    
    # Find a workbook by name and open the first sheet
    # Make sure you use the right name here.
    sheet = client.open("mqttsheet").sheet1
   
    next_row = next_available_row(sheet)
#    
#    
#    
#    
    count=int(next_row)
    count-=1
    
    
    #insert on the next available row
    date_today=str(datetime.date.today())
    
    
            
    time_today=time.strftime("%H:%M:%S")
    datalist=[count,date_today,time_today] + data_list
    range_list= sheet.range('A:L')
    
    for data,ran in datalist and enumerate(range_list):
        sheet.update_acell(ran.format(next_row),data)

isrj5 avatar May 17 '18 16:05 isrj5

range_list= sheet.range('A:L')

you have to give it both a row and a column: e.g. 'A1:L5'

ADraginda avatar May 17 '18 17:05 ADraginda

@ADraginda I figured, but that will be for only first row. My list data is a variable so i want to record all the values iterate them over the rows. So i want to provide a general range of columns.

isrj5 avatar May 17 '18 17:05 isrj5

UP I'm trying to use sheet.range("A2:E") which is a perfectly valid range and the application crashes

maximevast avatar Jun 13 '19 09:06 maximevast

Hi @isrj5 If you want to get all values from a sheet you can use the method range in the object WorkSheet which will return a range of cells defined by either an A1 notation or some indices (first row, first col, last row, last col) the latest might be appropriate to you by setting last col and last row with the property row_count or col_count from the same WorkSheet and will start from 1 for both.

If you are only interested in the values you can use the get_all_values method that takes a range too, but you would need to convert you column/row indices to an A1 notation, you can do so by using cast_to_a1_notation function from the utilspackage.

Hope that helps you resolve your issue.

lavigne958 avatar May 07 '21 21:05 lavigne958

@maximevast I am not too sure this range works, I need to double check.

If the application crashes could you please open a new issue with a code snippet that you use and produce the crash and we'll look at it from there please ?

lavigne958 avatar May 07 '21 21:05 lavigne958

No response from the issue owner, provided a proposal with the right methods to use.

Closing this issue.

lavigne958 avatar May 24 '21 18:05 lavigne958