Class DataSourceSpecBuilder

DataSourceSpecBuilder

Trình tạo cho DataSourceSpec. Để tạo một thông số kỹ thuật cho một số loại nhất định, hãy sử dụng phương thức as...(). Để tạo một trình tạo mới, hãy sử dụng SpreadsheetApp.newDataSourceSpec(). Để sử dụng thông số kỹ thuật, hãy xem DataSourceTable.

Chỉ sử dụng lớp này với dữ liệu được kết nối với cơ sở dữ liệu.

Ví dụ này cho biết cách tạo thông số kỹ thuật nguồn dữ liệu BigQuery.

const spec = SpreadsheetApp.newDataSourceSpec()
                 .asBigQuery()
                 .setProjectId('big_query_project')
                 .setRawQuery('select @FIELD from table limit @LIMIT')
                 .setParameterFromCell('FIELD', 'Sheet1!A1')
                 .setParameterFromCell('LIMIT', 'namedRangeCell')
                 .build();

Ví dụ này cho biết cách tạo thông số kỹ thuật nguồn dữ liệu Looker. Phương thức này trả về một đối tượng LookerDataSourceSpec sau khi sử dụng build().

const spec = SpreadsheetApp.newDataSourceSpec()
                 .asLooker()
                 .setInstanceUrl('https://looker_instance_url.com')
                 .setModelName('model_name')
                 .setExploreName('explore_name')
                 .build();

Phương thức

Phương thứcKiểu dữ liệu trả vềMô tả ngắn
asBigQuery()BigQueryDataSourceSpecBuilderNhận trình tạo cho nguồn dữ liệu BigQuery.
asLooker()LookerDataSourceSpecBuilderNhận trình tạo cho nguồn dữ liệu Looker.
build()DataSourceSpecTạo thông số kỹ thuật nguồn dữ liệu từ các chế độ cài đặt trong trình tạo này.
copy()DataSourceSpecBuilderTạo DataSourceSpecBuilder dựa trên các chế độ cài đặt của nguồn dữ liệu này.
getParameters()DataSourceParameter[]Nhận các tham số của nguồn dữ liệu.
getType()DataSourceTypeNhận loại nguồn dữ liệu.
removeAllParameters()DataSourceSpecBuilderXoá tất cả các tham số.
removeParameter(parameterName)DataSourceSpecBuilderXoá tham số đã chỉ định.
setParameterFromCell(parameterName, sourceCell)DataSourceSpecBuilderThêm một tham số hoặc nếu tham số có tên tồn tại, hãy cập nhật ô nguồn của tham số đó cho trình tạo thông số kỹ thuật nguồn dữ liệu thuộc loại DataSourceType.BIGQUERY.

Tài liệu chi tiết

asBigQuery()

Nhận trình tạo cho nguồn dữ liệu BigQuery.

Trả về

BigQueryDataSourceSpecBuilder – Trình tạo thông số kỹ thuật nguồn dữ liệu BigQuery.


asLooker()

Nhận trình tạo cho nguồn dữ liệu Looker.

const spec = SpreadsheetApp.newDataSourceSpec()
                 .asLooker()
                 .setInstanceUrl('https://looker_instance_url.com')
                 .setModelName('model_name')
                 .setExploreName('explore_name')
                 .build();

Trả về

LookerDataSourceSpecBuilder – Trình tạo thông số kỹ thuật nguồn dữ liệu Looker.


build()

Tạo thông số kỹ thuật nguồn dữ liệu từ các chế độ cài đặt trong trình tạo này. Bạn phải sử dụng as...() để chỉ định loại nguồn dữ liệu trước khi tạo.

Mã mẫu sau đây tạo một Thông số kỹ thuật DataSource BigQuery.

const bigQueryDataSourceSpec = SpreadsheetApp.newDataSourceSpec().asBigQuery();
// TODO(developer): Replace with the required dataset, project and table IDs.
bigQueryDataSourceSpec.setDatasetId('my data set id');
bigQueryDataSourceSpec.setProjectId('my project id');
bigQueryDataSourceSpec.setTableId('my table id');

bigQueryDataSourceSpec.build();

Mã mẫu sau đây tạo một Thông số kỹ thuật DataSource Looker.

const lookerDataSourceSpecBuilder =
    SpreadsheetApp.newDataSourceSpec().asLooker();
const lookerSpec = lookerDataSourceSpecBuilder.setExploreName('my explore name')
                       .setInstanceUrl('my instance url')
                       .setModelName('my model name')
                       .build();

Trả về

DataSourceSpec – Thông số kỹ thuật nguồn dữ liệu.


copy()

Tạo DataSourceSpecBuilder dựa trên các chế độ cài đặt của nguồn dữ liệu này.

// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc123456/edit',
);
const spec = ss.getDataSources()[0].getSpec();

const newSpec = spec.copy();

Trả về

DataSourceSpecBuilder – Trình tạo.


getParameters()

Nhận các tham số của nguồn dữ liệu.

// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc123456/edit',
);
const spec = ss.getDataSources()[0].getSpec();
const parameters = spec.getParameters();

Phương thức này chỉ dùng được cho nguồn dữ liệu BigQuery.

Trả về

DataSourceParameter[] – Danh sách tham số.


getType()

Nhận loại nguồn dữ liệu.

// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc123456/edit',
);
const spec = ss.getDataSources()[0].getSpec();
const type = spec.getType();

Trả về

DataSourceType – Loại nguồn dữ liệu.


removeAllParameters()

Xoá tất cả các tham số.

const specBuilder = SpreadsheetApp.newDataSourceSpec();
specBuilder.removeAllParameters();

Trả về

DataSourceSpecBuilder – Trình tạo, để liên kết.


removeParameter(parameterName)

Xoá tham số đã chỉ định.

const specBuilder = SpreadsheetApp.newDataSourceSpec();
specBuilder.removeParameter('x');

Tham số

TênLoạiMô tả
parameterNameStringTên của tham số cần xoá.

Trả về

DataSourceSpecBuilder – Trình tạo, để liên kết.


setParameterFromCell(parameterName, sourceCell)

Thêm một tham số hoặc nếu tham số có tên tồn tại, hãy cập nhật ô nguồn của tham số đó cho trình tạo thông số kỹ thuật nguồn dữ liệu thuộc loại DataSourceType.BIGQUERY.

Phương thức này chỉ dùng được cho nguồn dữ liệu BigQuery.

const specBuilder = SpreadsheetApp.newDataSourceSpec().asBigQuery();
specBuilder.setParameterFromCell('x', 'A1');
const bigQuerySpec = specBuilder.build();

Tham số

TênLoạiMô tả
parameterNameStringTên tham số.
sourceCellStringÔ nguồn, như được chỉ định trong ký hiệu A1.

Trả về

DataSourceSpecBuilder – Trình tạo, để liên kết.