07-电子表格的属性
14.2.2 电子表格的属性
虽然实际数据存在于电子表格的各个表中,但 Spreadsheet
对象具有属性: title
、 spreadsheetId
、 url
、 sheetTitles
和 sheets
,用于操作电子表格本身。在交互式环境中输入以下内容:
>>> import ezsheets
>>> ss = ezsheets.Spreadsheet('1J-Jx6Ne2K_vqI9J2SO-TAXOFbxx_9tUjwnkPC22LjeU')
>>> ss.title # The title of the spreadsheet.
'Education Data'
>>> ss.title = 'Class Data' # Change the title.
>>> ss.spreadsheetId # The unique ID (this is a read-only attribute).
'1J-Jx6Ne2K_vqI9J2SO-TAXOFbxx_9tUjwnkPC22LjeU'
>>> ss.url # The original URL (this is a read-only attribute).
'https://docs.google.com/spreadsheets/d/1J-Jx6Ne2K_vqI9J2SO- TAXOFbxx_9tUjwnkPC22LjeU/'
>>> ss.sheetTitles # The titles of all the Sheet objects
('Students', 'Classes', 'Resources')
>>> ss.sheets # The Sheet objects in this Spreadsheet, in order.
(<Sheet sheetId=0, title='Students', rowCount=1000, columnCount=26>, <Sheet
sheetId=1669384683, title='Classes', rowCount=1000, columnCount=26>, <Sheet
sheetId=151537240, title='Resources', rowCount=1000, columnCount=26>)
>>> ss[0] # The first Sheet object in this Spreadsheet.
<Sheet sheetId=0, title='Students', rowCount=1000, columnCount=26>
>>> ss['Students'] # Sheets can also be accessed by title.
<Sheet sheetId=0, title='Students', rowCount=1000, columnCount=26>
>>> del ss[0] # Delete the first Sheet object in this Spreadsheet.
>>> ss.sheetTitles # The "Students" Sheet object has been deleted:
('Classes', 'Resources')
如果有人通过Google Sheets网站修改电子表格,那么你的脚本可以调用 refresh()
方法更新 Spreadsheet
对象,使它与在线数据相匹配:
>>> ss.refresh()
这不仅会刷新 Spreadsheet
对象的属性,还会刷新其包含的 Sheet
对象中的数据。你对电子表格对象所做的更改将实时反映到在线电子表格中。