Class DataSourceTable

DataSourceTable

存取及修改現有的資料來源資料表。如要在新工作表中建立新的資料來源資料表,請使用 Spreadsheet.insertSheetWithDataSourceTable(spec)

請僅將這個類別用於連結至資料庫的資料。

本例顯示如何建立新的資料來源資料表。

SpreadsheetApp.enableBigQueryExecution();
var spreadsheet = SpreadsheetApp.getActive();
var spec = SpreadsheetApp.newDataSourceSpec()
           .asBigQuery()
           .setProjectId('big_query_project')
           .setRawQuery('select @FIELD from table limit @LIMIT')
           .setParameterFromCell('FIELD', 'Sheet1!A1')
           .setParameterFromCell('LIMIT', 'namedRangeCell')
           .build();
// Starts data execution asynchronously.
var dataSheet = spreadsheet.insertSheetWithDataSourceTable(spec);
var dataSourceTable = dataSheet.getDataSourceTables()[0];
// waitForCompletion() blocks script execution until data execution completes.
dataSourceTable.waitForCompletion(60);
// Check status after execution.
Logger.log("Data execution state: %s.", dataSourceTable.getStatus().getExecutionState());

本例說明如何編輯資料來源。

SpreadsheetApp.enableBigQueryExecution();
var dataSheet = SpreadsheetApp.getActive().getSheetByName("Data Sheet 1");
var dataSourceTable = dataSheet.getDataSourceTables()[0];
var dataSource = dataSourceTable.getDataSource();
var newSpec = dataSource.getSpec()
              .copy()
              .asBigQuery()
              .setRawQuery('select name from table limit 2')
              .removeAllParameters()
              .build();
// Updates data source specification and starts data execution asynchronously.
dataSource.updateSpec(newSpec);
// Check status during execution.
Logger.log("Data execution state: %s.", dataSourceTable.getStatus().getExecutionState());
// waitForCompletion() blocks script execution until data execution completes.
dataSourceTable.waitForCompletion(60);
// Check status after execution.
Logger.log("Data execution state: %s.", dataSourceTable.getStatus().getExecutionState());

方法

方法傳回類型簡短說明
addColumns(columnNames)DataSourceTable在資料來源資料表中加入資料欄。
addFilter(columnName, filterCriteria)DataSourceTable加入已套用至資料來源資料表的篩選器。
addSortSpec(columnName, ascending)DataSourceTable對資料來源資料表中的資料欄新增排序規格。
addSortSpec(columnName, sortOrder)DataSourceTable對資料來源資料表中的資料欄新增排序規格。
forceRefreshData()DataSourceTable重新整理這個物件的資料,無論目前狀態為何。
getColumns()DataSourceTableColumn[]取得新增至資料來源資料表的所有資料來源欄。
getDataSource()DataSource取得物件連結的資料來源。
getFilters()DataSourceTableFilter[]傳回已套用至資料來源資料表的所有篩選器。
getRange()Range取得這個資料來源資料表時距的 Range
getRowLimit()Integer傳回資料來源資料表的列數限制。
getSortSpecs()SortSpec[]取得資料來源資料表中的所有排序規格。
getStatus()DataExecutionStatus取得物件的資料執行狀態。
isSyncingAllColumns()Boolean傳回資料來源資料表是否同步處理相關資料來源中的所有資料欄。
refreshData()DataSourceTable重新整理物件資料。
removeAllColumns()DataSourceTable移除資料來源資料表中的所有資料欄。
removeAllSortSpecs()DataSourceTable移除資料來源資料表中的所有排序規格。
setRowLimit(rowLimit)DataSourceTable更新資料來源資料表的列數限制。
syncAllColumns()DataSourceTable將相關聯資料來源中所有現有和日後的資料欄同步到資料來源資料表。
waitForCompletion(timeoutInSeconds)DataExecutionStatus等到目前的執行作業完成,在提供的秒數後逾時。

內容詳盡的說明文件

addColumns(columnNames)

在資料來源資料表中加入資料欄。

參數

名稱類型說明
columnNamesString[]要新增的資料欄名稱清單。

回攻員

DataSourceTable:用於鏈結的資料來源資料表。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

addFilter(columnName, filterCriteria)

加入已套用至資料來源資料表的篩選器。

參數

名稱類型說明
columnNameString要套用此篩選器的資料欄名稱。
filterCriteriaFilterCriteria要套用的篩選條件。

回攻員

DataSourceTable:用於鏈結的資料來源資料表。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

addSortSpec(columnName, ascending)

對資料來源資料表中的資料欄新增排序規格。

參數

名稱類型說明
columnNameString要排序的資料欄名稱。
ascendingBoolean如果值為 true,請遞增排序資料欄;如果是 false,則會以遞減順序排序資料欄。

回攻員

DataSourceTable — 用於鏈結的資料來源工作表。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

addSortSpec(columnName, sortOrder)

對資料來源資料表中的資料欄新增排序規格。

參數

名稱類型說明
columnNameString要排序的資料欄名稱。
sortOrderSortOrder排列順序。

回攻員

DataSourceTable — 用於鏈結的資料來源工作表。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

forceRefreshData()

重新整理這個物件的資料,無論目前狀態為何。詳情請參閱 refreshData()

如果資料來源類型未啟用,就會擲回例外狀況。使用 SpreadsheetApp#enable...Execution() 方法,即可針對特定資料來源類型啟用資料執行功能。

回攻員

DataSourceTable:資料物件。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getColumns()

取得新增至資料來源資料表的所有資料來源欄。

回攻員

DataSourceTableColumn[]:資料來源資料表欄清單。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getDataSource()

取得物件連結的資料來源。

回攻員

DataSource:資料來源。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getFilters()

傳回已套用至資料來源資料表的所有篩選器。

回攻員

DataSourceTableFilter[]:套用至資料來源資料表的所有篩選器陣列。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getRange()

取得這個資料來源資料表時距的 Range

回攻員

Range:範圍。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getRowLimit()

傳回資料來源資料表的列數限制。

回攻員

Integer — 如果未設定資料來源資料表的列數上限,null如果未設定限制,且資料表會使用 Google 試算表 UI 中的預設上限。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getSortSpecs()

取得資料來源資料表中的所有排序規格。

回攻員

SortSpec[]:排序規格的清單。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getStatus()

取得物件的資料執行狀態。

回攻員

DataExecutionStatus:資料執行狀態。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

isSyncingAllColumns()

傳回資料來源資料表是否同步處理相關資料來源中的所有資料欄。

回攻員

Boolean:如果資料來源資料表會同步處理關聯資料來源中的所有資料欄,則值為 True,否則傳回 false

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

refreshData()

重新整理物件資料。

如果目前處於 error 狀態,就會擲回例外狀況。請使用 DataSource#updateSpec() 來更新規格。此方法建議優先於 forceRefreshData(),以免資料來源發生非預期的編輯作業。

如果資料來源類型未啟用,就會擲回例外狀況。使用 SpreadsheetApp#enable...Execution() 方法,即可針對特定資料來源類型啟用資料執行功能。

回攻員

DataSourceTable:資料物件。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

removeAllColumns()

移除資料來源資料表中的所有資料欄。

回攻員

DataSourceTable:用於鏈結的資料來源資料表。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

removeAllSortSpecs()

移除資料來源資料表中的所有排序規格。

回攻員

DataSourceTable — 用於鏈結的資料來源工作表。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

setRowLimit(rowLimit)

更新資料來源資料表的列數限制。如果提供的資料列限制為 null,請更新資料來源資料表,使其使用 Google 試算表 UI 預設的資料列上限。

參數

名稱類型說明
rowLimitInteger資料表的新資料列限制。如為 null,請更新資料表以使用預設資料列限制。

回攻員

DataSourceTable:用於鏈結的資料來源資料表。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

syncAllColumns()

將相關聯資料來源中所有現有和日後的資料欄同步到資料來源資料表。

回攻員

DataSourceTable:用於鏈結的資料來源資料表。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

waitForCompletion(timeoutInSeconds)

等到目前的執行作業完成,在指定的秒數過後逾時。如果執行作業在逾時後未完成,但不會取消資料執行,就會擲回例外狀況。

參數

名稱類型說明
timeoutInSecondsInteger等待資料執行的時間 (以秒為單位)。長度上限為 300 秒。

回攻員

DataExecutionStatus:資料執行狀態。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets