python - Add Google sheet with data using Google API v4 -


i using python. need add sheet in spreadsheets using google api v4. can create sheet using batchupdate spreadsheet id , addsheet request (it returns shetid , creates empty sheet). how can add data in it?

data = {'requests': [     {         'addsheet':{             'properties':{'title': 'new sheet'}         }     } ]}  res = service.spreadsheets().batchupdate(spreadsheetid=s_id, body=data).execute() sheet_id = res['replies'][0]['addsheet']['properties']['sheetid'] 

you can add code write data in google sheet. in document - reading & writing cell values

spreadsheets can have multiple sheets, each sheet having number of rows or columns. cell location @ intersection of particular row , column, , may contain data value. google sheets api provides spreadsheets.values collection enable simple reading , writing of values.

writing single range

to write data single range, use spreadsheets.values.update request:

values = [     [         # cell values ...     ],     # additional rows ... ] body = {   'values': values } result = service.spreadsheets().values().update(     spreadsheetid=spreadsheet_id, range=range_name,     valueinputoption=value_input_option, body=body).execute() 

the body of update request must valuerange object, though required field values. if range specified, must match range in url. in valuerange, can optionally specify majordimension. default, rows used. if columns specified, each inner array written column instead of row.

writing multiple ranges

if want write multiple discontinuous ranges, can use spreadsheets.values.batchupdate request:

values = [     [         # cell values     ],     # additional rows ] data = [     {         'range': range_name,         'values': values     },     # additional ranges update ... ] body = {   'valueinputoption': value_input_option,   'data': data } result = service.spreadsheets().values().batchupdate(     spreadsheetid=spreadsheet_id, body=body).execute() 

the body of batchupdate request must batchupdatevaluesrequest object, contains valueinputoption , list of valuerange objects (one each written range). each valuerange object specifies own range, majordimension, , data input.

hope helps.


Comments

Popular posts from this blog

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

laravel - Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: F:\project\resources\views\admin\carousels\index.blade.php) -