Using Google Spreadsheet API without having values in header row in lower case with spaces removed

一曲冷凌霜 提交于 2020-01-03 02:05:14

问题


I was wondering if there is any way around (read: hacks) using the Google Spreadsheet API that doesn't impose the restriction of having the values in the header row in lowercase with no spaces. I'm aware that I can just make use of the cell based feed but that would have an overhead of my application having to track what column number corresponds to a particular column name.

Does anyone have an alternative means?

(Also I noticed that the Google Spreadsheet API Docs doesn't make any mention of the header row name restriction, I had to search around to find out why my code wasn't working initially)


回答1:


You don't have to actually change any headers in your spreadsheet and bent to some restrictions. Basically the API transforms your headers into the "lowercase no-space" format and you can access them as such. So trying to access a column with a header "My Header" would work by querying the column "myheader". The headers in the original spreadsheet remain unchanged and in the desired format.




回答2:


#header_names = list of header names
spreadclient = gdata.spreadsheets.client.SpreadsheetsClient(source='Your App Name')

#Add a line to authenticate the spreadsheet client

spreadsheet_key = spreadsheet.GetId().split("%3A")[1]
worksheets = spreadclient.GetWorksheets(spreadsheet_key)
#get worksheet id
wsid =  worksheets.entry[0].GetWorksheetId()
#header row can only be accessed via cellfeed
for i,name in enumerate(header_names):
    cellentry= spreadclient.GetCell(spreadsheet_key, worksheet_id=wsid, row_num=1, col_num=1+i)
    #update value
    cellentry.cell.input_value = name #TODO: do batch update instead
    spreadclient.update(cellentry)


来源:https://stackoverflow.com/questions/11592785/using-google-spreadsheet-api-without-having-values-in-header-row-in-lower-case-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!