在使用ExternalTable Operator(Ghseets)时,可以在创建operator实例时设置bucket和range参数来指定所需的Google Sheets文档中的位置。具体实现方法如下所示:
from airflow.providers.google.cloud.transfers.gcs_to_gsheet import GCSToGSheetsOperator
from airflow.operators.gsheet_plugin import GSheetsCreateTableOperator, ExternalTableOperator
from airflow.utils.dates import days_ago
bucket_name = 'your-bucket-name'
object_path = 'folder/filename.csv'
sheet_name = 'Sheet1'
gcp_conn_id = 'google_cloud_default'
gsheet_conn_id = 'gsheet_default'
range_ = 'A2:C5'
gcs_to_gsheet_task = GCSToGSheetsOperator(task_id='gcs_to_gsheet',
bucket=bucket_name,
filename=object_path,
sheet= sheet_name,
range_=range_,
gcp_conn_id=gcp_conn_id,
gsheet_conn_id=gsheet_conn_id)
create_gsheets_task = GSheetsCreateTableOperator(task_id='create_gsheets_table',
sheet_name=sheet_name,
range_=range_,
gcp_conn_id=gcp_conn_id,
gsheet_conn_id=gsheet_conn_id)
ext_gsheets_task = ExternalTableOperator(task_id='ext_table_gsheets',
sheet_name=sheet_name,
range_=range_,
gcp_conn_id=gcp_conn_id,
gsheet_conn_id=gsheet_conn_id)
上述代码中首先定义了所需的参数,然后依次创建了3个operator实例,分别对应将csv文件导入到Google Sheets中、创建Google Sheets表和从外部加载Google Sheets表。其中,ExternalTableOperator的range_参数指定了要加载的表的位置。注意:创建Google Sheets表(GSheetsCreateTableOperator)和从外部加载Google Sheets表(ExternalTableOperator)都需要提供gsheet_conn_id参数。在使用这两个Operator之前,必须先设置该连接id,以便连接到Google Sheets帐户