Sheetfu:一個快捷方便的python package,輕鬆操作 Google Sheets


Sheetfu:一個快捷方便的python package,輕鬆操作 Google Sheets

介紹

Sheetfu旨在通過簡單,直觀,快速的API與Google表格進行交互。該庫的主要目標是通過Python操作 Google App Script API。使用Sheetfu,您可以輕鬆獲取或設置單元格值,背景色,字體顏色或任何其他單元格屬性。

安裝

使用pip安裝和更新 :

<code>pip install -U Sheetfu/<code>

一個簡單的例子

<code>from sheetfu import SpreadsheetApp

spreadsheet = SpreadsheetApp('path/to/secret.json').open_by_id('<insert>')
# if ENV vars are defined, you can do initialize it with:
# spreadsheet = SpreadsheetApp(from_env=True).open_by_id('<insert>')

sheet = spreadsheet.get_sheet_by_name('Sheet1')
data_range = sheet.get_data_range() # returns the sheet range that contains data values.

# this is how you get things
values = data_range.get_values() # returns a 2D matrix of values.
backgrounds = data_range.get_backgrounds() # returns a 2D matrix of background colors in hex format.

# this is how you set things
data_range.set_background('#000000') # set every cell backgrounds to black
data_range.set_font_color('#ffffff') # set every cell font colors to white

data_range.commit() # to commit your changes otherwise nothing gets changed./<insert>/<insert>/<code>

要通過認證連接到你自己的Google Sheets ,請參閱 身份驗證教程

您可以參考 sheetfu API文檔 以獲得更詳細的描述。

表格模塊

Sheetfu還包含一個表格模塊,該表格模塊針對類似於ORM的語法完全抽象了座標系統。以下示例適用於具有“名稱”,“姓氏”和“年齡”三列的工作表。

<code>from sheetfu import Table

spreadsheet = SpreadsheetApp('path/to/secret.json').open_by_id('<insert>')
# if ENV vars are defined, you can do initialize it with:
# spreadsheet = SpreadsheetApp(from_env=True).open_by_id('<insert>')

data_range = spreadsheet.get_sheet_by_name('people').get_data_range()

table = Table(data_range, backgrounds=True)

for item in table:
if item.get_field_value('name') == 'foo':
item.set_field_value('surname', 'bar') # this set the surname field value
age = item.get_field_value('age')
item.set_field_value('age', age + 1)
item.set_field_background('age', '#ff0000') # this set the field 'age' to red color

# Every set functions are batched for speed performance.
# To send the batch update of every set requests you made,
# you need to commit the table object as follow.
table.commit()/<insert>/<insert>/<code>

地址

https://github.com/socialpoint-labs/sheetfu


分享到:


相關文章: