Class DataSource

資料來源

存取及修改現有資料來源。如要使用新資料來源建立資料來源表格,請參閱 DataSourceTable

請只將這個類別用於與資料庫連線的資料。

方法

方法傳回類型簡短說明
cancelAllLinkedDataSourceObjectRefreshes()void取消目前所有正在執行的重新整理作業,這些作業與連結至這個資料來源的資料來源物件有關。
createCalculatedColumn(name, formula)DataSourceColumn建立計算結果欄。
createDataSourcePivotTableOnNewSheet()DataSourcePivotTable從新工作表的第一個儲存格中,根據這個資料來源建立資料透視表。
createDataSourceTableOnNewSheet()DataSourceTable在新的工作表的第一個儲存格中,從這個資料來源建立資料來源表格。
getCalculatedColumnByName(columnName)DataSourceColumn|null傳回資料來源中與資料欄名稱相符的計算欄。
getCalculatedColumns()DataSourceColumn[]傳回資料來源中的所有計算欄。
getColumns()DataSourceColumn[]傳回資料來源中的所有資料欄。
getDataSourceSheets()DataSourceSheet[]傳回與這個資料來源相關聯的資料來源工作表。
getSpec()DataSourceSpec取得資料來源規格。
refreshAllLinkedDataSourceObjects()void重新整理連結至資料來源的所有資料來源物件。
updateSpec(spec)DataSource更新資料來源規格,並使用新規格重新整理與這個資料來源連結的資料來源物件。
updateSpec(spec, refreshAllLinkedObjects)DataSource更新資料來源規格,並使用新規格重新整理連結的 data source sheets
waitForAllDataExecutionsCompletion(timeoutInSeconds)void等待連結資料來源物件的所有目前執行作業完成,並在指定秒數後逾時。

內容詳盡的說明文件

cancelAllLinkedDataSourceObjectRefreshes()

取消目前與這個資料來源連結的所有資料來源物件重新整理作業。

這個範例說明如何取消資料來源的所有重新整理作業。

SpreadsheetApp.enableBigQueryExecution();
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const dataSource = spreadsheet.getDataSources()[0];
dataSource.cancelAllLinkedDataSourceObjectRefreshes();

如果未啟用資料來源類型,系統會擲回例外狀況。使用 SpreadsheetApp#enable...Execution() 方法,為特定資料來源類型啟用資料執行程序。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

createCalculatedColumn(name, formula)

建立計算結果欄。這個方法僅適用於 BigQuery 資料來源。

參數

名稱類型說明
nameString計算結果欄的名稱。
formulaString計算結果欄公式。

回攻員

DataSourceColumn:新建立的計算結果欄。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

createDataSourcePivotTableOnNewSheet()

在新的工作表的第一個儲存格中,從這個資料來源建立資料透視表。附帶效果是將新工作表設為使用中工作表。

回攻員

DataSourcePivotTable - 新建立的資料來源資料透視表。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

createDataSourceTableOnNewSheet()

在新的工作表的第一個儲存格中,從這個資料來源建立資料來源表格。並將新工作表設為使用中工作表。

這個方法僅適用於 BigQuery 資料來源。

回攻員

DataSourceTable - 新建立的資料來源表。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getCalculatedColumnByName(columnName)

傳回資料來源中與資料欄名稱相符的計算欄。

參數

名稱類型說明
columnNameString要取得的計算結果欄名稱。

回攻員

DataSourceColumn|null:與資料欄名稱相符的計算結果欄,或 null (如果沒有這類計算結果欄)。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getCalculatedColumns()

傳回資料來源中的所有計算欄。

DataSourceType.LOOKER 類型的資料來源規格會傳回空陣列。

回攻員

DataSourceColumn[]:資料來源中所有計算結果欄的陣列。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getColumns()

傳回資料來源中的所有資料欄。

回攻員

DataSourceColumn[]:資料來源中所有 DataSourceColumn 的陣列。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getDataSourceSheets()

傳回與這個資料來源相關聯的資料來源工作表。

回攻員

DataSourceSheet[]:資料來源工作表陣列。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getSpec()

取得資料來源規格。

回攻員

DataSourceSpec:資料來源規格。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

refreshAllLinkedDataSourceObjects()

重新整理連結至資料來源的所有資料來源物件。

如果未啟用資料來源類型,系統會擲回例外狀況。使用 SpreadsheetApp#enable...Execution() 方法,為特定資料來源類型啟用資料執行程序。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

updateSpec(spec)

更新資料來源規格,並使用新規格重新整理與這個資料來源連結的資料來源物件。

如果未啟用資料來源類型,系統會擲回例外狀況。使用 SpreadsheetApp#enable...Execution() 方法,為特定資料來源類型啟用資料執行程序。

如果資料來源規格類型與目前的資料來源類型不同,則會擲回例外狀況。

參數

名稱類型說明
specDataSourceSpec要更新的資料來源規格。

回攻員

DataSource:資料來源。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

updateSpec(spec, refreshAllLinkedObjects)

更新資料來源規格,並使用新規格重新整理連結的 data source sheets

如果未啟用資料來源類型,系統會擲回例外狀況。使用 SpreadsheetApp#enable...Execution() 方法,為特定資料來源類型啟用資料執行程序。

如果資料來源規格類型與目前的資料來源類型不同,則會擲回例外狀況。

參數

名稱類型說明
specDataSourceSpec要更新的資料來源規格。
refreshAllLinkedObjectsBoolean如果 true,也會重新整理與這個資料來源連結的所有資料來源物件。

回攻員

DataSource:資料來源。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

waitForAllDataExecutionsCompletion(timeoutInSeconds)

等待連結資料來源物件的所有目前執行作業完成,並在經過指定秒數後逾時。如果執行作業在逾時時未完成,系統會擲回例外狀況,但不會取消資料執行作業。

參數

名稱類型說明
timeoutInSecondsInteger等待資料執行作業的時間 (以秒為單位)。最多 300 秒。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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