Google Sheets 運算子¶
Sheets API 的最新版本允許開發者透過程式設計方式
讀寫資料
格式化文字和數字
構建透視表
強制單元格驗證
設定凍結行
調整列大小
應用公式
建立圖表……等等!
有關更多資訊,請查閱官方文件。
先決條件任務¶
要使用這些運算子,您需要做一些事情
使用Cloud Console選擇或建立一個 Cloud Platform 專案。
為您的專案啟用結算,詳情請參見Google Cloud 文件中的說明。
啟用 API,詳情請參見Cloud Console 文件中的說明。
透過 pip 安裝 API 庫。
pip install 'apache-airflow[google]'有關安裝的詳細資訊可用。
建立電子表格¶
要建立新的電子表格,您可以使用GoogleSheetsCreateSpreadsheetOperator。
tests/system/google/cloud/gcs/example_sheets.py
create_spreadsheet = GoogleSheetsCreateSpreadsheetOperator(
task_id="create_spreadsheet", spreadsheet=SPREADSHEET, gcp_conn_id=CONNECTION_ID
)
您可以將Jinja 模板用於 spreadsheet, impersonation_chain。
要獲取新建立的電子表格的 URL,請使用 XCom 值
tests/system/google/cloud/gcs/example_sheets.py
print_spreadsheet_url = BashOperator(
task_id="print_spreadsheet_url",
bash_command=f"echo {XComArg(create_spreadsheet, key='spreadsheet_url')}",
)