Class Spreadsheet

試算表

存取及修改 Google 試算表檔案。常見的操作是新增試算表 協作者。

方法

方法傳回類型簡短說明
addDeveloperMetadata(key)Spreadsheet在頂層試算表中加入含有指定索引鍵的開發人員中繼資料。
addDeveloperMetadata(key, visibility)Spreadsheet將具有指定索引鍵和瀏覽權限的開發人員中繼資料新增至試算表。
addDeveloperMetadata(key, value)Spreadsheet將含有指定鍵和值的開發人員中繼資料新增至試算表。
addDeveloperMetadata(key, value, visibility)Spreadsheet將開發人員中繼資料加入試算表,包含指定鍵、值和瀏覽權限。
addEditor(emailAddress)Spreadsheet將特定使用者新增至 Spreadsheet 的編輯者清單。
addEditor(user)Spreadsheet將特定使用者新增至 Spreadsheet 的編輯者清單。
addEditors(emailAddresses)Spreadsheet將指定的使用者陣列新增至 Spreadsheet 的編輯者清單。
addMenu(name, subMenus)void在試算表 UI 中建立新選單。
addViewer(emailAddress)Spreadsheet將特定使用者新增至 Spreadsheet 的檢視者清單。
addViewer(user)Spreadsheet將特定使用者新增至 Spreadsheet 的檢視者清單。
addViewers(emailAddresses)Spreadsheet將指定的使用者陣列新增至 Spreadsheet 的檢視者清單。
appendRow(rowContents)Sheet在工作表中目前資料區域的底部附加一列。
autoResizeColumn(columnPosition)Sheet設定指定資料欄的寬度以符合內容。
copy(name)Spreadsheet複製試算表並傳回新的試算表。
createDeveloperMetadataFinder()DeveloperMetadataFinder傳回 DeveloperMetadataFinder,以尋找範圍內的開發人員中繼資料 這份試算表。
createTextFinder(findText)TextFinder為試算表建立文字尋找工具,可用於尋找及取代 。
deleteActiveSheet()Sheet刪除目前使用中的工作表。
deleteColumn(columnPosition)Sheet從指定的資料欄位置刪除欄。
deleteColumns(columnPosition, howMany)void從指定的資料欄位置開始刪除數量的資料欄。
deleteRow(rowPosition)Sheet從指定列位置刪除列。
deleteRows(rowPosition, howMany)void從指定列位置開始刪除多個資料列。
deleteSheet(sheet)void刪除指定的工作表。
duplicateActiveSheet()Sheet複製使用中的工作表,並將其設為使用中的工作表。
getActiveCell()Range傳回這份工作表中正在使用的儲存格。
getActiveRange()Range傳回作用中工作表中的所選範圍;如果沒有有效範圍,則傳回 null
getActiveRangeList()RangeList傳回使用中的工作表中的有效範圍清單;如果沒有使用中的範圍,則傳回 null 範圍。
getActiveSheet()Sheet取得試算表中的工作表。
getAs(contentType)Blob以 blob 的形式傳回這個物件中的資料,做為轉換成指定內容類型的 blob。
getBandings()Banding[]傳回這份試算表中的所有色帶。
getBlob()Blob以 blob 的形式傳回這個物件中的資料。
getColumnWidth(columnPosition)Integer取得指定欄的寬度 (以像素為單位)。
getCurrentCell()Range傳回使用中的工作表中目前的儲存格;如果沒有目前儲存格,則傳回 null
getDataRange()Range傳回與資料所在維度對應的 Range
getDataSourceFormulas()DataSourceFormula[]取得所有資料來源公式。
getDataSourcePivotTables()DataSourcePivotTable[]取得所有資料來源資料透視表。
getDataSourceRefreshSchedules()DataSourceRefreshSchedule[]取得這份試算表的重新整理時間表。
getDataSourceSheets()DataSourceSheet[]傳回試算表中的所有資料來源工作表。
getDataSourceTables()DataSourceTable[]取得所有資料來源資料表。
getDataSources()DataSource[]傳回試算表中的所有資料來源。
getDeveloperMetadata()DeveloperMetadata[]取得與頂層試算表相關聯的開發人員中繼資料。
getEditors()User[]取得這個 Spreadsheet 的編輯者清單。
getFormUrl()String傳回將回應傳送至此試算表的表單網址,若為 null, 這份試算表沒有相關聯的表單。
getFrozenColumns()Integer傳回凍結欄數。
getFrozenRows()Integer傳回凍結列數。
getId()String取得此試算表的專屬 ID。
getImages()OverGridImage[]傳回工作表上所有格線上方圖片。
getIterativeCalculationConvergenceThreshold()Number傳回在反覆運算期間使用的閾值。
getLastColumn()Integer傳回最後一個含有內容的資料欄位置。
getLastRow()Integer傳回含有內容的最後一列位置。
getMaxIterativeCalculationCycles()Integer傳回在疊代計算期間使用的疊代次數上限。
getName()String取得文件名稱。
getNamedRanges()NamedRange[]取得這份試算表中的所有已命名範圍。
getNumSheets()Integer傳回這份試算表中的工作表數量。
getOwner()User傳回文件擁有者,或為共用雲端硬碟中的文件傳回 null
getPredefinedSpreadsheetThemes()SpreadsheetTheme[]傳回預先定義主題的清單。
getProtections(type)Protection[]取得代表試算表中所有受保護範圍或工作表的物件陣列。
getRange(a1Notation)Range傳回 A1 標記法或 R1C1 標記法中指定的範圍。
getRangeByName(name)Range傳回已命名範圍;如果找不到指定名稱的範圍,則傳回 null
getRangeList(a1Notations)RangeList傳回代表指定工作表中範圍的 RangeList 集合 A1 標記或 R1C1 表示法清單的非空白清單
getRecalculationInterval()RecalculationInterval傳回這份試算表的計算間隔。
getRowHeight(rowPosition)Integer取得指定列的高度 (以像素為單位)。
getSelection()Selection傳回試算表中目前的 Selection
getSheetByName(name)Sheet傳回特定名稱的工作表。
getSheetId()Integer傳回此物件所代表的工作表 ID。
getSheetName()String傳回工作表名稱。
getSheetValues(startRow, startColumn, numRows, numColumns)Object[][]傳回此範圍中值的矩形格線,從指定座標開始。
getSheets()Sheet[]取得這份試算表中的所有工作表。
getSpreadsheetLocale()String取得試算表語言代碼。
getSpreadsheetTheme()SpreadsheetTheme傳回試算表目前的主題;如未套用主題,則傳回 null
getSpreadsheetTimeZone()String取得試算表的時區。
getUrl()String傳回指定試算表的網址。
getViewers()User[]取得這個Spreadsheet的檢視者和加註者名單。
hideColumn(column)void隱藏指定範圍內的一或多個資料欄。
hideRow(row)void隱藏指定範圍內的列。
insertColumnAfter(afterPosition)Sheet在指定的欄位置後方插入資料欄。
insertColumnBefore(beforePosition)Sheet在指定的欄位置前面插入資料欄。
insertColumnsAfter(afterPosition, howMany)Sheet在指定的欄位置後方插入指定數量的欄。
insertColumnsBefore(beforePosition, howMany)Sheet在指定資料欄位置之前插入數量欄。
insertDataSourceSheet(spec)DataSourceSheet在試算表中插入新的 DataSourceSheet,並開始執行資料。
insertImage(blobSource, column, row)OverGridImage在指定列和欄中插入 Spreadsheet 做為文件中的圖片。
insertImage(blobSource, column, row, offsetX, offsetY)OverGridImage在指定列和欄中插入 Spreadsheet 做為文件中的圖片, 像素偏移。
insertImage(url, column, row)OverGridImage在文件中在指定列和欄中插入圖片。
insertImage(url, column, row, offsetX, offsetY)OverGridImage以像素偏移,在文件指定的列或欄插入圖片。
insertRowAfter(afterPosition)Sheet在指定列位置後方插入資料列。
insertRowBefore(beforePosition)Sheet在指定列位置之前插入資料列。
insertRowsAfter(afterPosition, howMany)Sheet在指定列位置後方插入數量列。
insertRowsBefore(beforePosition, howMany)Sheet在指定列位置之前插入多個資料列。
insertSheet()Sheet使用預設工作表名稱,在試算表中插入新工作表。
insertSheet(sheetIndex)Sheet在指定索引的試算表中插入新工作表。
insertSheet(sheetIndex, options)Sheet在指定索引的試算表中插入新的工作表,並選用進階選項 引數。
insertSheet(options)Sheet在試算表中插入新工作表,使用預設的工作表名稱,也可選用進階工作表。 引數。
insertSheet(sheetName)Sheet在試算表中插入含有指定名稱的新工作表。
insertSheet(sheetName, sheetIndex)Sheet在試算表中插入含有指定名稱的新工作表。
insertSheet(sheetName, sheetIndex, options)Sheet在試算表中插入含有指定名稱的新工作表,並使用 選用進階引數
insertSheet(sheetName, options)Sheet在試算表中插入具有指定名稱的新工作表,並選用進階選項 引數。
insertSheetWithDataSourceTable(spec)Sheet在試算表中插入新工作表,建立DataSourceTable並橫跨 再開始執行資料執行作業。
isColumnHiddenByUser(columnPosition)Boolean傳回使用者是否已隱藏指定資料欄。
isIterativeCalculationEnabled()Boolean傳回是否要在這個試算表啟用疊代計算。
isRowHiddenByFilter(rowPosition)Boolean傳回特定資料列是否遭到篩選器 (非篩選器檢視畫面) 隱藏。
isRowHiddenByUser(rowPosition)Boolean傳回使用者是否已隱藏指定資料列。
moveActiveSheet(pos)void將使用中的工作表移至工作表清單中的指定位置。
moveChartToObjectSheet(chart)Sheet建立新的 SheetType.OBJECT 工作表,並將提供的圖表移至該工作表。
refreshAllDataSources()void重新整理所有支援的資料來源及連結的資料來源物件,並略過無效動作 資料來源物件
removeEditor(emailAddress)Spreadsheet將特定使用者從 Spreadsheet 的編輯者清單中移除。
removeEditor(user)Spreadsheet將特定使用者從 Spreadsheet 的編輯者清單中移除。
removeMenu(name)void移除 addMenu(name, subMenus) 新增的選單。
removeNamedRange(name)void刪除具有指定名稱的已命名範圍。
removeViewer(emailAddress)Spreadsheet將特定使用者從 Spreadsheet 的檢視者和加註者清單中移除。
removeViewer(user)Spreadsheet將特定使用者從 Spreadsheet 的檢視者和加註者清單中移除。
rename(newName)void重新命名文件。
renameActiveSheet(newName)void將目前使用中的工作表重新命名為指定新名稱。
resetSpreadsheetTheme()SpreadsheetTheme移除套用的主題並設定試算表的預設主題。
setActiveRange(range)Range將使用中工作表中的 active range 設為指定範圍,且 範圍中左上角儲存格的 current cell
setActiveRangeList(rangeList)RangeList將指定的範圍清單設為active ranges 已開啟工作表。
setActiveSelection(range)Range設定這張工作表的有效選取區域。
setActiveSelection(a1Notation)Range設定中的選擇,如 A1 標記法或 R1C1 標記法中所指定的內容。
setActiveSheet(sheet)Sheet將指定的工作表設為試算表中正在使用的工作表。
setActiveSheet(sheet, restoreSelection)Sheet將指定的工作表設為試算表中正在使用的工作表,並可選擇還原 最近期的選取項目
setColumnWidth(columnPosition, width)Sheet設定指定資料欄的寬度 (以像素為單位)。
setCurrentCell(cell)Range將指定儲存格設為 current cell
setFrozenColumns(columns)void凍結指定的欄數。
setFrozenRows(rows)void凍結指定的資料列數量。
setIterativeCalculationConvergenceThreshold(minThreshold)Spreadsheet設定反覆運算的最小門檻值。
setIterativeCalculationEnabled(isEnabled)Spreadsheet設定是否允許在這個試算表使用疊代計算。
setMaxIterativeCalculationCycles(maxIterations)Spreadsheet設定疊代期間應執行的計算疊代次數上限 計算。
setNamedRange(name, range)void為範圍命名。
setRecalculationInterval(recalculationInterval)Spreadsheet設定這份試算表的重新計算頻率。
setRowHeight(rowPosition, height)Sheet設定指定列的列高 (以像素為單位)。
setSpreadsheetLocale(locale)void設定試算表語言代碼。
setSpreadsheetTheme(theme)SpreadsheetTheme設定試算表的主題。
setSpreadsheetTimeZone(timezone)void設定試算表的時區。
show(userInterface)void在使用者的瀏覽器中央顯示對話方塊內,顯示自訂使用者介面元件 檢視區域
sort(columnPosition)Sheet依資料欄遞增排序工作表。
sort(columnPosition, ascending)Sheet依欄排序工作表。
toast(msg)void在試算表右下角顯示含有特定訊息的彈出式視窗。
toast(msg, title)void在試算表右下角顯示彈出式視窗,列出指定訊息和 標題。
toast(msg, title, timeoutSeconds)void在試算表右下角顯示彈出式視窗,內含指定標題和 訊息,顯示一段時間。
unhideColumn(column)void取消隱藏指定範圍內的欄。
unhideRow(row)void取消隱藏指定範圍內的列。
updateMenu(name, subMenus)void更新 addMenu(name, subMenus) 新增的選單。
waitForAllDataExecutionsCompletion(timeoutInSeconds)void等候試算表中的所有目前執行作業完成,並在 指定秒數

內容詳盡的說明文件

addDeveloperMetadata(key)

在頂層試算表中加入含有指定索引鍵的開發人員中繼資料。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Adds the key 'NAME' in the developer metadata for the spreadsheet.
ss.addDeveloperMetadata('NAME')

// Gets the first developer metadata object and logs its key.
const developerMetaData = ss.getDeveloperMetadata()[0]
console.log(developerMetaData.getKey())

參數

名稱類型說明
keyString新開發人員中繼資料的鍵。

回攻員

Spreadsheet:這份試算表用於鏈結。

授權

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

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

addDeveloperMetadata(key, visibility)

將具有指定索引鍵和瀏覽權限的開發人員中繼資料新增至試算表。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Adds the key 'NAME' in the developer metadata for the spreadsheet and sets the visibility
// to the developer project that created the metadata.
ss.addDeveloperMetadata('NAME', SpreadsheetApp.DeveloperMetadataVisibility.PROJECT);

// Gets the first developer metadata object and logs its key and visibility setting.
const developerMetaData = ss.getDeveloperMetadata()[0];
console.log(developerMetaData.getKey());
console.log(`Key: ${developerMetaData.getKey()},
.             Visibility: ${developerMetaData.getVisibility()}`);

參數

名稱類型說明
keyString新開發人員中繼資料的鍵。
visibilityDeveloperMetadataVisibility新開發人員中繼資料的瀏覽權限。

回攻員

Spreadsheet:這份試算表用於鏈結。

授權

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

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

addDeveloperMetadata(key, value)

將含有指定鍵和值的開發人員中繼資料新增至試算表。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Adds the key 'NAME' and sets the value to 'GOOGLE' in the developer metadata for the
// spreadsheet.
ss.addDeveloperMetadata('NAME', 'GOOGLE');

// Gets the first developer metadata object and logs its key and value.
const developerMetaData = ss.getDeveloperMetadata()[0];
console.log(developerMetaData.getKey());
console.log(`Key: ${developerMetaData.getKey()}, Value: ${developerMetaData.getValue()}`);

參數

名稱類型說明
keyString新開發人員中繼資料的鍵。
valueString新開發人員中繼資料的值。

回攻員

Spreadsheet:這份試算表用於鏈結。

授權

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

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

addDeveloperMetadata(key, value, visibility)

將開發人員中繼資料加入試算表,包含指定鍵、值和瀏覽權限。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Adds the key 'NAME', sets the value to 'GOOGLE', and sets the visibility
// to any developer project with document access.
ss.addDeveloperMetadata('NAME', 'GOOGLE', SpreadsheetApp.DeveloperMetadataVisibility.DOCUMENT);

// Gets the first developer metadata object and logs its key, value, and visibility setting.
const developerMetaData = ss.getDeveloperMetadata()[0];
console.log(`Key: ${developerMetaData.getKey()},
             Value: ${developerMetaData.getValue()},
             Visibility: ${developerMetaData.getVisibility()}`);

參數

名稱類型說明
keyString新開發人員中繼資料的鍵。
valueString新開發人員中繼資料的值。
visibilityDeveloperMetadataVisibility新開發人員中繼資料的瀏覽權限。

回攻員

Spreadsheet:這份試算表用於鏈結。

授權

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

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

addEditor(emailAddress)

將特定使用者新增至 Spreadsheet 的編輯者清單。如果使用者已 觀眾也會成為觀眾,因此能在觀影迷清單中向他們宣傳。

參數

名稱類型說明
emailAddressString要新增的使用者電子郵件地址。

回攻員

Spreadsheet - 這個 Spreadsheet 用於鏈結。

授權

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

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

addEditor(user)

將特定使用者新增至 Spreadsheet 的編輯者清單。如果使用者已 觀眾也會成為觀眾,因此能在觀影迷清單中向他們宣傳。

參數

名稱類型說明
userUser代表要新增的使用者。

回攻員

Spreadsheet - 這個 Spreadsheet 用於鏈結。

授權

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

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

addEditors(emailAddresses)

將指定的使用者陣列新增至 Spreadsheet 的編輯者清單。如果有任何 觀看清單中的使用者,這種做法是將他們 觀眾。

參數

名稱類型說明
emailAddressesString[]要新增的使用者電子郵件地址陣列。

回攻員

Spreadsheet - 這個 Spreadsheet 用於鏈結。

授權

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

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

addMenu(name, subMenus)

在試算表 UI 中建立新選單。

每個選單項目都會執行使用者定義函式。通常,您會想從 onOpen() 函式呼叫該函式,讓系統在載入試算表時自動建立選單。

// The onOpen function is executed automatically every time a Spreadsheet is loaded
function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [];
  // When the user clicks on "addMenuExample" then "Menu Entry 1", the function function1 is
  // executed.
  menuEntries.push({name: "Menu Entry 1", functionName: "function1"});
  menuEntries.push(null); // line separator
  menuEntries.push({name: "Menu Entry 2", functionName: "function2"});

  ss.addMenu("addMenuExample", menuEntries);
}

參數

名稱類型說明
nameString要建立的選單名稱。
subMenusObject[]包含 namefunctionName 參數的 JavaScript 對應陣列。您可以使用加入的程式庫中的函式,例如 Library.libFunction1

授權

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

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

addViewer(emailAddress)

將特定使用者新增至 Spreadsheet 的檢視者清單。如果使用者已 在編輯器清單上,這個方法不會有任何作用。

參數

名稱類型說明
emailAddressString要新增的使用者電子郵件地址。

回攻員

Spreadsheet - 這個 Spreadsheet 用於鏈結。

授權

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

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

addViewer(user)

將特定使用者新增至 Spreadsheet 的檢視者清單。如果使用者已 在編輯器清單上,這個方法不會有任何作用。

參數

名稱類型說明
userUser代表要新增的使用者。

回攻員

Spreadsheet - 這個 Spreadsheet 用於鏈結。

授權

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

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

addViewers(emailAddresses)

將指定的使用者陣列新增至 Spreadsheet 的檢視者清單。如果有任何 使用者已在編輯者清單中,這個方法對他們沒有作用。

參數

名稱類型說明
emailAddressesString[]要新增的使用者電子郵件地址陣列。

回攻員

Spreadsheet - 這個 Spreadsheet 用於鏈結。

授權

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

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

appendRow(rowContents)

在工作表中目前資料區域的底部附加一列。如果儲存格的內容開始 傳遞至 =,系統會將其解讀為公式。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Appends a new row with 3 columns to the bottom of the current
// data region in the sheet containing the values in the array.
sheet.appendRow(["a man", "a plan", "panama"]);

參數

名稱類型說明
rowContentsObject[]要在工作表中最後一列之後插入的值陣列。

回攻員

Sheet:適合用於方法鏈結的工作表。

授權

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

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

autoResizeColumn(columnPosition)

設定指定資料欄的寬度以符合內容。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

sheet.getRange('a1').setValue('Whenever it is a damp, drizzly November in my soul...');

// Sets the first column to a width which fits the text
sheet.autoResizeColumn(1);

參數

名稱類型說明
columnPositionInteger指定資料欄可調整大小的位置。

回攻員

Sheet:適合用於方法鏈結的工作表

授權

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

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

copy(name)

複製試算表並傳回新的試算表。

// This code makes a copy of the current spreadsheet and names it appropriately
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.copy("Copy of " + ss.getName());

參數

名稱類型說明
nameString副本的名稱。

回攻員

Spreadsheet:這份試算表用於鏈結。

授權

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

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

createDeveloperMetadataFinder()

傳回 DeveloperMetadataFinder,以尋找範圍內的開發人員中繼資料 這份試算表。根據預設,系統會將所有與試算表相關聯的中繼資料納入考量 工作表、列和欄

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Adds developer metadata to the spreadsheet.
ss.addDeveloperMetadata('NAME', 'CHARLIE');
ss.addDeveloperMetadata('COMPANY', 'EXAMPLE ORGANIZATION');
ss.addDeveloperMetadata('TECHNOLOGY', 'JAVASCRIPT');

// Creates a developer metadata finder.
const developerMetadataFinder = ss.createDeveloperMetadataFinder();

// Finds the developer metadata objects with 'COMPANY' as the key.
const googleMetadataFromSpreadsheet = developerMetadataFinder.withKey('COMPANY').find();

// Gets the first result of developer metadata that has the key 'COMPANY' and logs its value.
console.log(googleMetadataFromSpreadsheet[0].getValue());

回攻員

DeveloperMetadataFinder:開發人員中繼資料搜尋工具,可在這份試算表範圍內搜尋中繼資料。


createTextFinder(findText)

為試算表建立文字尋找工具,可用於尋找及取代 。搜尋作業會從試算表的第一張工作表開始。

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();

// Creates  a text finder.
var textFinder = spreadsheet.createTextFinder('dog');

// Returns the first occurrence of 'dog' in the spreadsheet.
var firstOccurrence = textFinder.findNext();

// Replaces the last found occurrence of 'dog' with 'cat' and returns the number
// of occurrences replaced.
var numOccurrencesReplaced = findOccurrence.replaceWith('cat');

參數

名稱類型說明
findTextString要搜尋的文字。

回攻員

TextFinder:試算表的 TextFinder


deleteActiveSheet()

刪除目前使用中的工作表。

// The code below deletes the currently active sheet and stores the new active sheet in a
// variable
var newSheet = SpreadsheetApp.getActiveSpreadsheet().deleteActiveSheet();

回攻員

Sheet:新的使用中工作表。

授權

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

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

deleteColumn(columnPosition)

從指定的資料欄位置刪除欄。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Columns start at "1" - this deletes the first column
sheet.deleteColumn(1);

參數

名稱類型說明
columnPositionInteger資料欄的位置,從第一欄從 1 開始。

回攻員

Sheet:適合用於方法鏈結的工作表

授權

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

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

deleteColumns(columnPosition, howMany)

從指定的資料欄位置開始刪除數量的資料欄。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Columns start at "1" - this deletes the first two columns
sheet.deleteColumns(1, 2);

參數

名稱類型說明
columnPositionInteger要刪除的第一個資料欄的位置。
howManyInteger要刪除的欄數。

授權

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

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

deleteRow(rowPosition)

從指定列位置刪除列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Rows start at "1" - this deletes the first row
sheet.deleteRow(1);

參數

名稱類型說明
rowPositionInteger資料列的位置,從第一列 1 開始。

回攻員

Sheet:適合用於方法鏈結的工作表

授權

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

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

deleteRows(rowPosition, howMany)

從指定列位置開始刪除多個資料列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Rows start at "1" - this deletes the first two rows
sheet.deleteRows(1, 2);

參數

名稱類型說明
rowPositionInteger第一個要刪除的列的位置。
howManyInteger要刪除的列數。

授權

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

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

deleteSheet(sheet)

刪除指定的工作表。

// The code below deletes the specified sheet.
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('My Sheet');
ss.deleteSheet(sheet);

參數

名稱類型說明
sheetSheet要刪除的工作表。

授權

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

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

duplicateActiveSheet()

複製使用中的工作表,並將其設為使用中的工作表。

// The code below makes a duplicate of the active sheet
SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet();

回攻員

Sheet:新的工作表。

授權

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

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

getActiveCell()

傳回這份工作表中正在使用的儲存格。

注意:建議您使用 getCurrentCell(),這會傳回目前的 醒目顯示的儲存格

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Returns the active cell
var cell = sheet.getActiveCell();

回攻員

Range - 目前使用中的儲存格

授權

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

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

getActiveRange()

傳回作用中工作表中的所選範圍;如果沒有有效範圍,則傳回 null。如果 選取了多個範圍時,這個方法只會傳回最後一個選取的範圍。

「有效範圍」一詞指的是使用者在開啟的工作表中選取的範圍 但在自訂函式中,則是指正在主動重新計算的儲存格。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var activeRange = sheet.getActiveRange();

回攻員

Range:目前使用中的範圍

授權

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

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

另請參閱


getActiveRangeList()

傳回使用中的工作表中的有效範圍清單;如果沒有使用中的範圍,則傳回 null 範圍。

如果只選取單一範圍,則會視為 getActiveRange() 呼叫。

var sheet = SpreadsheetApp.getActiveSheet();
// Returns the list of active ranges.
var activeRangeList = sheet.getActiveRangeList();

回攻員

RangeList:有效範圍清單

授權

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

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

另請參閱


getActiveSheet()

取得試算表中的工作表。

試算表中正在使用的工作表是指正在在試算表中顯示的工作表 第一種是使用無代碼解決方案 AutoML 透過使用者介面建立機器學習模型

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

回攻員

Sheet:試算表中正在使用的工作表。


getAs(contentType)

以 blob 的形式傳回這個物件中的資料,做為轉換成指定內容類型的 blob。這個 方法會在檔案名稱中加入適當的副檔名,例如「myfile.pdf」。不過 假設檔案名稱最後一個句點 (如果有) 之後的部分為現有文件 所有應替換的副檔名。因此,「ShoppingList.12.25.2014」變成 「ShoppingList.12.25.pdf」。

如要查看轉換的每日配額,請參閱 Google 的配額 服務。新建立的 Google Workspace 網域可能會暫時受到更嚴格的限制

參數

名稱類型說明
contentTypeString要轉換的 MIME 類型。大多數 blob 的 'application/pdf' 是 就是唯一有效的選項如為 BMP、GIF、JPEG 或 PNG 格式的圖片,也應採用 'image/bmp''image/gif''image/jpeg''image/png' 格式 有效。如果是 Google 文件,'text/markdown' 也是有效的格式。

回攻員

Blob - 做為 blob 的資料。


getBandings()

傳回這份試算表中的所有色帶。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Gets an array of the bandings in the spreadsheet.
const bandings = ss.getBandings();

// Logs the range of the first banding in the spreadsheet to the console.
console.log(bandings[0].getRange().getA1Notation());

回攻員

Banding[] - 這份試算表中的頻帶。

授權

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

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

getBlob()

以 blob 的形式傳回這個物件中的資料。

回攻員

Blob - 做為 blob 的資料。


getColumnWidth(columnPosition)

取得指定欄的寬度 (以像素為單位)。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Columns start at 1
Logger.log(sheet.getColumnWidth(1));

參數

名稱類型說明
columnPositionInteger要檢查的資料欄位置。

回攻員

Integer:欄寬 (以像素為單位)

授權

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

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

getCurrentCell()

傳回使用中的工作表中目前的儲存格;如果沒有目前儲存格,則傳回 null。 目前的儲存格是 Google 試算表 UI 中位於焦點的儲存格,並以深色醒目顯示 邊框。目前未超過一個儲存格。當使用者選取一或多個儲存格範圍時 選取範圍中的其中一個儲存格是目前的儲存格。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Returns the current highlighted cell in the one of the active ranges.
var currentCell = sheet.getCurrentCell();

回攻員

Range - 目前的儲存格

授權

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

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

getDataRange()

傳回與資料所在維度對應的 Range

這在功能上等同於建立 (Sheet.getLastColumn(), Sheet.getLastRow())。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This represents ALL the data
var range = sheet.getDataRange();
var values = range.getValues();

// This logs the spreadsheet in CSV format with a trailing comma
for (var i = 0; i < values.length; i++) {
  var row = "";
  for (var j = 0; j < values[i].length; j++) {
    if (values[i][j]) {
      row = row + values[i][j];
    }
    row = row + ",";
  }
  Logger.log(row);
}

回攻員

Range:包含試算表所有資料的範圍

授權

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

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

getDataSourceFormulas()

取得所有資料來源公式。

// Opens the spreadsheet by its ID. If you created your script from within a Google Sheets
// file, use SpreadsheetApp.getActiveSpreadsheet().
// TODO(developer): Replace the ID with your own.
const ss = SpreadsheetApp.openById('abc123456');

// Gets Sheet1 by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets an array of the data source formulas on Sheet1.
// To get an array of data source formulas for the entire spreadsheet,
// replace 'sheet' with 'ss'.
const dataSourceFormulas = sheet.getDataSourceFormulas();

// Logs the first data source formula in the array.
console.log(dataSourceFormulas[0].getFormula());

回攻員

DataSourceFormula[]:資料來源公式清單。

授權

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

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

getDataSourcePivotTables()

取得所有資料來源資料透視表。

// Opens the spreadsheet file by its ID. If you created your script from a Google Sheets file,
// use SpreadsheetApp.getActiveSpreadsheet().
// TODO(developer): Replace the ID with your own.
const ss = SpreadsheetApp.openById('abc123456');

// Gets Sheet1 by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets an array of the data source pivot tables on Sheet1.
// To get an array of data source pivot tables for the entire
// spreadsheet, replace 'sheet' with 'ss'.
const dataSourcePivotTables = sheet.getDataSourcePivotTables();

// Logs the last time that the first pivot table in the array was refreshed.
console.log(dataSourcePivotTables[0].getStatus().getLastRefreshedTime());

回攻員

DataSourcePivotTable[]:資料來源資料透視表清單。

授權

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

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

getDataSourceRefreshSchedules()

取得這份試算表的重新整理時間表。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Activates BigQuery operations for the connected spreadsheet.
SpreadsheetApp.enableBigQueryExecution();

// Gets the frequency type of the first referesh schedule in the array.
const frequencyType = ss.getDataSourceRefreshSchedules()[0]
                        .getFrequency()
                        .getFrequencyType()
                        .toString();

// Logs the frequency type to the console.
console.log(frequencyType);

回攻員

DataSourceRefreshSchedule[]:這份試算表的重新整理時間表。

授權

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

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

getDataSourceSheets()

傳回試算表中的所有資料來源工作表。

// Turns data execution on for BigQuery data sources.
SpreadsheetApp.enableBigQueryExecution();

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Gets the first data source sheet in the spreadsheet.
const dataSource = ss.getDataSourceSheets()[0];

// Gets the name of the data source sheet.
console.log(dataSource.asSheet().getName());

回攻員

DataSourceSheet[] — 所有資料來源工作表的陣列。

授權

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

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

getDataSourceTables()

取得所有資料來源資料表。

// Opens the spreadsheet file by its ID. If you created your script from a Google Sheets file,
// use SpreadsheetApp.getActiveSpreadsheet().
// TODO(developer): Replace the ID with your own.
const ss = SpreadsheetApp.openById('abc123456');

// Gets Sheet1 by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets an array of data source tables on Sheet1.
// To get an array of data source tables for the entire spreadsheet,
// replace 'sheet' with 'ss'.
const dataSourceTables = sheet.getDataSourceTables();

// Logs the last completed data execution time on the first data source table.
console.log(dataSourceTables[0].getStatus().getLastExecutionTime());

回攻員

DataSourceTable[]:資料來源資料表的清單。

授權

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

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

getDataSources()

傳回試算表中的所有資料來源。

// Turns data execution on for BigQuery data sources.
SpreadsheetApp.enableBigQueryExecution();

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Gets the data sources on the spreadsheet.
const dataSources = ss.getDataSources();

// Logs the name of the first column on the first data source.
console.log(dataSources[0].getColumns()[0].getName());

回攻員

DataSource[] — 所有資料來源的陣列。

授權

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

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

getDeveloperMetadata()

取得與頂層試算表相關聯的開發人員中繼資料。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Adds 'Google' as a key to the spreadsheet metadata.
ss.addDeveloperMetadata('Google');

// Gets the spreadsheet's metadata.
const ssMetadata = ss.getDeveloperMetadata();

// Gets the first set of the spreadsheet's metadata and logs the key to the console.
console.log(ssMetadata[0].getKey());

回攻員

DeveloperMetadata[]:與這個範圍相關聯的開發人員中繼資料。

授權

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

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

getEditors()

取得這個 Spreadsheet 的編輯者清單。

回攻員

User[]:具備編輯權限的使用者陣列。

授權

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

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

getFormUrl()

傳回表單網址,也就是將回應傳送至此試算表的網址,或傳回 null 這份試算表沒有相關聯的表單。如果有多份表單將回覆傳送到這份試算表, 系統傳回的表單網址不明。或者,您也可以建立個別工作表網址的關聯 透過 Sheet.getFormUrl() 方法擷取。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Gets the form URL from the spreadsheet.
const formUrl = ss.getFormUrl();

// Logs the form URL to the console.
console.log(formUrl);

回攻員

String:將此試算表的回應填入此試算表的網址,或 null (如果 這份試算表沒有相關聯的表單。

授權

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

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

getFrozenColumns()

傳回凍結欄數。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

Logger.log("Number of frozen columns: %s", sheet.getFrozenColumns());

回攻員

Integer:凍結的欄數

授權

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

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

getFrozenRows()

傳回凍結列數。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

Logger.log("Number of frozen rows: %s", sheet.getFrozenRows());

回攻員

Integer:凍結的列數

授權

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

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

getId()

取得此試算表的專屬 ID。您可以從網址中擷取試算表 ID。 例如網址中的試算表 ID https://docs.google.com/spreadsheets/d/abc1234567/edit#gid=0 是「abc1234567」。

// The code below logs the ID for the active spreadsheet.
Logger.log(SpreadsheetApp.getActiveSpreadsheet().getId());

回攻員

String:試算表的專屬 ID。


getImages()

傳回工作表上所有格線上方圖片。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Gets Sheet1 by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets the over-the-grid images from Sheet1.
// To get the over-the-grid images from the entire spreadsheet, use ss.getImages() instead.
const images = sheet.getImages();

// For each image, logs the anchor cell in A1 notation.
for (const image of images) {
 console.log(image.getAnchorCell().getA1Notation());
}

回攻員

OverGridImage[]:格線上方圖片陣列。

授權

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

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

getIterativeCalculationConvergenceThreshold()

傳回在反覆運算期間使用的閾值。連續發生的結果 計算差異時,值小於這個值,系統會停止反覆運算。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Sets the iterative calculation convergence threshold for the spreadsheet.
ss.setIterativeCalculationConvergenceThreshold(2);

// Logs the threshold to the console.
console.log(ss.getIterativeCalculationConvergenceThreshold());

回攻員

Number:收斂臨界值。

授權

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

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

getLastColumn()

傳回最後一個含有內容的資料欄位置。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This logs the value in the very last cell of this sheet
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var lastCell = sheet.getRange(lastRow, lastColumn);
Logger.log(lastCell.getValue());

回攻員

Integer:工作表中最後含有內容的欄

授權

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

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

getLastRow()

傳回含有內容的最後一列位置。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This logs the value in the very last cell of this sheet
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var lastCell = sheet.getRange(lastRow, lastColumn);
Logger.log(lastCell.getValue());

回攻員

Integer:工作表中最後一列含有內容的列

授權

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

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

getMaxIterativeCalculationCycles()

傳回在疊代計算期間使用的疊代次數上限。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Sets the max iterative calculation cycles for the spreadsheet.
ss.setMaxIterativeCalculationCycles(10);

// Logs the max iterative calculation cycles to the console.
console.log(ss.getMaxIterativeCalculationCycles());

回攻員

Integer:計算疊代次數的上限。

授權

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

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

getName()

取得文件名稱。

var ss = SpreadsheetApp.getActiveSpreadsheet();
Logger.log(ss.getName());

回攻員

String:試算表名稱。

授權

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

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

getNamedRanges()

取得這份試算表中的所有已命名範圍。

// The code below logs the name of the first named range.
var namedRanges = SpreadsheetApp.getActiveSpreadsheet().getNamedRanges();
for (var i = 0; i < namedRanges.length; i++) {
  Logger.log(namedRanges[i].getName());
}

回攻員

NamedRange[] - 試算表中所有已命名範圍的陣列。

授權

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

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

getNumSheets()

傳回這份試算表中的工作表數量。

// The code below logs the number of sheets in the active spreadsheet.
Logger.log(SpreadsheetApp.getActiveSpreadsheet().getNumSheets());

回攻員

Integer:試算表中的工作表數量。


getOwner()

傳回文件擁有者,或為共用雲端硬碟中的文件傳回 null

var ss = SpreadsheetApp.getActiveSpreadsheet();
var owner = ss.getOwner();
Logger.log(owner.getEmail());

回攻員

User:文件擁有者;如果文件位於共用雲端硬碟,則為 null

授權

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

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

getPredefinedSpreadsheetThemes()

傳回預先定義主題的清單。

// The code below returns the list of predefined themes.
var predefinedThemesList = SpreadsheetApp.getActiveSpreadsheet()
                               .getPredefinedSpreadsheetThemes();

回攻員

SpreadsheetTheme[]:預先定義的主題清單。

授權

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

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

getProtections(type)

取得代表試算表中所有受保護範圍或工作表的物件陣列。

// Remove all range protections in the spreadsheet that the user has permission to edit.
var ss = SpreadsheetApp.getActive();
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
  var protection = protections[i];
  if (protection.canEdit()) {
    protection.remove();
  }
}
// Remove all sheet protections in the spreadsheet that the user has permission to edit.
var ss = SpreadsheetApp.getActive();
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.SHEET);
for (var i = 0; i < protections.length; i++) {
  var protection = protections[i];
  if (protection.canEdit()) {
    protection.remove();
  }
}

參數

名稱類型說明
typeProtectionType保護區域的類型,可以是 SpreadsheetApp.ProtectionType.RANGESpreadsheetApp.ProtectionType.SHEET

回攻員

Protection[]:代表試算表中所有受保護範圍或工作表的物件陣列。

授權

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

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

getRange(a1Notation)

傳回 A1 標記法或 R1C1 標記法中指定的範圍。

// Get a range A1:D4 on sheet titled "Invoices"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getRange("Invoices!A1:D4");

// Get cell A1 on the first sheet
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A1");

參數

名稱類型說明
a1NotationString要傳回的範圍,如 A1 標記或 R1C1 標記法中所述。

回攻員

Range:指定位置的範圍

授權

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

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

getRangeByName(name)

傳回已命名範圍;如果找不到指定名稱的範圍,則傳回 null。如有多個 試算表的工作表使用相同的範圍名稱,請指定工作表名稱,不要加入額外的 引號:例如 getRangeByName('TaxRates')getRangeByName('Sheet Name!TaxRates'),但不包括 getRangeByName('"Sheet Name"!TaxRates')

// Log the number of columns for the range named 'TaxRates' in the active spreadsheet.
var range = SpreadsheetApp.getActiveSpreadsheet().getRangeByName('TaxRates');
if (range != null) {
  Logger.log(range.getNumColumns());
}

參數

名稱類型說明
nameString要取得的範圍名稱。

回攻員

Range — 含有指定名稱的儲存格範圍。


getRangeList(a1Notations)

傳回代表指定工作表中範圍的 RangeList 集合 A1 標記或 R1C1 表示法清單的非空白清單

// Get a list of ranges A1:D4, F1:H4.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var rangeList  = sheet.getRangeList(['A1:D4', 'F1:H4']);

參數

名稱類型說明
a1NotationsString[]要傳回的範圍清單,如 A1 標記法或 R1C1 標記法中所述。

回攻員

RangeList:指定位置的範圍清單

授權

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

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

getRecalculationInterval()

傳回這份試算表的計算間隔。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Logs the calculation interval for the spreadsheet to the console.
console.log(ss.getRecalculationInterval().toString());

回攻員

RecalculationInterval:這份試算表的計算間隔。

授權

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

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

getRowHeight(rowPosition)

取得指定列的高度 (以像素為單位)。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Rows start at 1
Logger.log(sheet.getRowHeight(1));

參數

名稱類型說明
rowPositionInteger要檢查的資料列位置。

回攻員

Integer:列高 (以像素為單位)

授權

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

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

getSelection()

傳回試算表中目前的 Selection

var selection = SpreadsheetApp.getActiveSpreadsheet().getSelection();
var currentCell = selection.getCurrentCell();

回攻員

Selection:目前選取的項目

授權

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

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

getSheetByName(name)

傳回特定名稱的工作表。

如果多份工作表的名稱相同,系統會傳回最左側的工作表。如果發生以下情況,則傳回 null 沒有指定名稱的工作表。

// The code below logs the index of a sheet named "Expenses"
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Expenses");
if (sheet != null) {
  Logger.log(sheet.getIndex());
}

參數

名稱類型說明
nameString要取得的工作表名稱。

回攻員

Sheet:具有指定名稱的工作表。


getSheetId()

傳回此物件所代表的工作表 ID。

這是該試算表專屬的工作表 ID。ID 代表 增加工作表建立時指派的整數遞增,與工作表位置無關。這個 適用於接受 gridId 參數 (而非 Sheet 例項) 的 Range.copyFormatToRange(gridId, column, columnEnd, row, rowEnd) 等方法。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

Logger.log(sheet.getSheetId());

回攻員

Integer:試算表專屬的工作表 ID

授權

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

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

getSheetName()

傳回工作表名稱。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

Logger.log(sheet.getSheetName());

回攻員

String:工作表名稱

授權

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

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

getSheetValues(startRow, startColumn, numRows, numColumns)

傳回此範圍中值的矩形格線,從指定座標開始。A -1 值相當於取得最後一列或欄的位置。 更是如此

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// The two samples below produce the same output
var values = sheet.getSheetValues(1, 1, 3, 3);
Logger.log(values);

var range = sheet.getRange(1, 1, 3, 3);
values = range.getValues();
Logger.log(values);

參數

名稱類型說明
startRowInteger起始列的位置。
startColumnInteger起始欄的位置。
numRowsInteger要傳回其值的資料列數。
numColumnsInteger要傳回值的資料欄數。

回攻員

Object[][]:值的二維陣列

授權

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

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

getSheets()

取得這份試算表中的所有工作表。

// The code below logs the name of the second sheet
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
if (sheets.length > 1) {
  Logger.log(sheets[1].getName());
}

回攻員

Sheet[] - 試算表中所有工作表的陣列。


getSpreadsheetLocale()

取得試算表語言代碼。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Gets the spreadsheet locale.
const ssLocale = ss.getSpreadsheetLocale();

// Logs the locale to the console.
console.log(ssLocale);

回攻員

String:試算表語言代碼。

授權

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

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

getSpreadsheetTheme()

傳回試算表目前的主題;如未套用主題,則傳回 null

// The code below returns the current theme of the spreadsheet.
var currentTheme = SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTheme();

回攻員

SpreadsheetTheme - 目前套用的主題。

授權

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

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

getSpreadsheetTimeZone()

取得試算表的時區。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Sets the time zone of the spreadsheet.
ss.setSpreadsheetTimeZone('America/New_York');

// Gets the time zone of the spreadsheet.
const ssTimeZone = ss.getSpreadsheetTimeZone();

// Logs the time zone to the console.
console.log(ssTimeZone);

回攻員

String:時區,以「long」指定格式 (例如「America/New_York」,例如: 由 Joda.org 提供)。

授權

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

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

getUrl()

傳回指定試算表的網址。

var ss = SpreadsheetApp.getActiveSpreadsheet();
Logger.log(ss.getUrl());

回攻員

String:指定試算表的網址。

授權

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

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

getViewers()

取得這個Spreadsheet的檢視者和加註者名單。

回攻員

User[]:擁有檢視或註解權限的使用者陣列。

授權

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

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

hideColumn(column)

隱藏指定範圍內的一或多個資料欄。

let ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getSheets()[0];

// This hides the first column
let range = sheet.getRange("A1");
sheet.hideColumn(range);

// This hides the first 3 columns
let range = sheet.getRange("A:C");
sheet.hideColumn(range);

參數

名稱類型說明
columnRange要隱藏的欄範圍。

授權

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

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

hideRow(row)

隱藏指定範圍內的列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This hides the first row
var range = sheet.getRange("A1");
sheet.hideRow(range);

參數

名稱類型說明
rowRange要隱藏的列範圍。

授權

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

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

insertColumnAfter(afterPosition)

在指定的欄位置後方插入資料欄。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This inserts a column after the first column position
sheet.insertColumnAfter(1);

參數

名稱類型說明
afterPositionInteger應在後方新增資料欄。

回攻員

Sheet:適合用於方法鏈結的工作表

授權

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

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

insertColumnBefore(beforePosition)

在指定的欄位置前面插入資料欄。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This inserts a column in the first column position
sheet.insertColumnBefore(1);

參數

名稱類型說明
beforePositionInteger要加入新資料欄的資料欄。

回攻員

Sheet:適合用於方法鏈結的工作表

授權

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

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

insertColumnsAfter(afterPosition, howMany)

在指定的欄位置後方插入指定數量的欄。

let ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getSheets()[0];

// Inserts two columns after the first column on the first sheet of the spreadsheet.
sheet.insertColumnsAfter(1,2);

參數

名稱類型說明
afterPositionInteger應在後方新增資料欄。
howManyInteger要插入的欄數。

回攻員

Sheet:適合用於方法鏈結的工作表

授權

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

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

insertColumnsBefore(beforePosition, howMany)

在指定資料欄位置之前插入數量欄。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This inserts five columns before the first column
sheet.insertColumnsBefore(1, 5);

參數

名稱類型說明
beforePositionInteger要加入新資料欄的資料欄。
howManyInteger要插入的欄數。

回攻員

Sheet:適合用於方法鏈結的工作表

授權

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

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

insertDataSourceSheet(spec)

在試算表中插入新的 DataSourceSheet,並開始執行資料。身為 這也會使新的工作表成為使用中的工作表。

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

// Activates BigQuery operations.
SpreadsheetApp.enableBigQueryExecution();

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Builds a data source specification.
// TODO (developer): Update the project ID to your own Google Cloud project ID.
const dataSourceSpec = SpreadsheetApp.newDataSourceSpec()
 .asBigQuery()
 .setProjectId('project-id-1')
 .setTableProjectId('bigquery-public-data')
 .setDatasetId('ncaa_basketball')
 .setTableId('mbb_historical_teams_games')
 .build();

// Adds the data source and its data to the spreadsheet.
ss.insertDataSourceSheet(dataSourceSpec);

參數

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

回攻員

DataSourceSheet:新的資料來源工作表。

授權

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

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

insertImage(blobSource, column, row)

在指定列和欄中插入 Spreadsheet 做為文件中的圖片。圖片 擷取自 blob 內容的大小。支援的 blob 大小上限為 2 MB。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var blob = Utilities.newBlob(binaryData, 'image/png', 'MyImageName');
sheet.insertImage(blob, 1, 1);

參數

名稱類型說明
blobSourceBlobSource含有圖片內容、MIME 類型和名稱的 blob。
columnInteger資料欄位置。
rowInteger資料列位置。

回攻員

OverGridImage:插入的圖片。

授權

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

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

insertImage(blobSource, column, row, offsetX, offsetY)

在指定列和欄中插入 Spreadsheet 做為文件中的圖片, 像素偏移。圖片大小是擷取自 blob 內容。支援的 blob 上限 大小為 2 MB

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var blob = Utilities.newBlob(binaryData, 'image/png', 'MyImageName');
sheet.insertImage(blob, 1, 1, 10, 10);

參數

名稱類型說明
blobSourceBlobSource含有圖片內容、MIME 類型和名稱的 blob。
columnInteger資料欄位置。
rowInteger資料列位置。
offsetXInteger儲存格角落的水平偏移 (以像素為單位)。
offsetYInteger儲存格角落的垂直偏移 (以像素為單位)。

回攻員

OverGridImage:插入的圖片。

授權

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

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

insertImage(url, column, row)

在文件中在指定列和欄中插入圖片。

您提供的網址必須可公開存取。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

sheet.insertImage("https://www.google.com/images/srpr/logo3w.png", 1, 1);

參數

名稱類型說明
urlString圖片的網址。
columnInteger格線欄位置。
rowInteger格線列位置。

回攻員

OverGridImage:插入的圖片。

授權

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

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

insertImage(url, column, row, offsetX, offsetY)

以像素偏移,在文件指定的列或欄插入圖片。

您提供的網址必須可公開存取。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

sheet.insertImage("https://www.google.com/images/srpr/logo3w.png", 1, 1, 10, 10);

參數

名稱類型說明
urlString圖片的網址。
columnInteger資料欄位置。
rowInteger資料列位置。
offsetXInteger儲存格角落的水平偏移 (以像素為單位)。
offsetYInteger儲存格角落的垂直偏移 (以像素為單位)。

回攻員

OverGridImage:插入的圖片。

授權

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

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

insertRowAfter(afterPosition)

在指定列位置後方插入資料列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This inserts a row after the first row position
sheet.insertRowAfter(1);

參數

名稱類型說明
afterPositionInteger要在哪個資料列之後新增資料列。

回攻員

Sheet:適合用於方法鏈結的工作表

授權

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

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

insertRowBefore(beforePosition)

在指定列位置之前插入資料列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This inserts a row before the first row position
sheet.insertRowBefore(1);

參數

名稱類型說明
beforePositionInteger要新增資料列之前的列。

回攻員

Sheet:適合用於方法鏈結的工作表

授權

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

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

insertRowsAfter(afterPosition, howMany)

在指定列位置後方插入數量列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This inserts five rows after the first row
sheet.insertRowsAfter(1, 5);

參數

名稱類型說明
afterPositionInteger要在哪個資料列之後新增資料列。
howManyInteger要插入的列數。

回攻員

Sheet:適合用於方法鏈結的工作表

授權

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

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

insertRowsBefore(beforePosition, howMany)

在指定列位置之前插入多個資料列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This inserts five rows before the first row
sheet.insertRowsBefore(1, 5);

參數

名稱類型說明
beforePositionInteger要新增資料列之前的列。
howManyInteger要插入的列數。

回攻員

Sheet:適合用於方法鏈結的工作表

授權

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

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

insertSheet()

使用預設工作表名稱,在試算表中插入新工作表。新的工作表會變成 已開啟工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.insertSheet();

回攻員

Sheet:新的工作表。

授權

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

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

insertSheet(sheetIndex)

在指定索引的試算表中插入新工作表。新的工作表就會變成已啟用 工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.insertSheet(1);

參數

名稱類型說明
sheetIndexInteger新建立工作表的索引。如何將工作表插入 將試算表設為 0

回攻員

Sheet:新的工作表。

授權

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

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

insertSheet(sheetIndex, options)

在指定索引的試算表中插入新的工作表,並選用進階選項 引數。新的工作表就會成為目前使用的工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var templateSheet = ss.getSheetByName('Sales');
ss.insertSheet(1, {template: templateSheet});

參數

名稱類型說明
sheetIndexInteger新建立工作表的索引。如何將工作表插入 將試算表設為 0
optionsObject選用的 JavaScript 進階引數。

進階參數

名稱類型說明
templateSheet範本工作表物件的所有資料都會複製到 在新工作表中插入範本工作表範本必須是試算表物件的其中一個工作表。

回攻員

Sheet:新的工作表。

授權

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

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

insertSheet(options)

在試算表中插入新工作表,使用預設的工作表名稱,也可選用進階工作表。 引數。新的工作表就會成為目前使用的工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var templateSheet = ss.getSheetByName('Sales');
ss.insertSheet({template: templateSheet});

參數

名稱類型說明
optionsObject選用的 JavaScript 進階引數,如下所列。

進階參數

名稱類型說明
templateSheet範本工作表物件的所有資料都會複製到 在新工作表中插入範本工作表範本必須是這個試算表物件的其中一個工作表。

回攻員

Sheet:新的工作表。

授權

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

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

insertSheet(sheetName)

在試算表中插入含有指定名稱的新工作表。新的工作表就會變成已啟用 工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.insertSheet('My New Sheet');

參數

名稱類型說明
sheetNameString新工作表的名稱。

回攻員

Sheet:新的工作表。

授權

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

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

insertSheet(sheetName, sheetIndex)

在試算表中插入含有指定名稱的新工作表。新工作表 則會成為使用中的工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.insertSheet('My New Sheet', 1);

參數

名稱類型說明
sheetNameString新工作表的名稱。
sheetIndexInteger新建立工作表的索引。如何將工作表插入 將試算表設為 0

回攻員

Sheet:新的工作表。

授權

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

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

insertSheet(sheetName, sheetIndex, options)

在試算表中插入含有指定名稱的新工作表,並使用 選用進階引數新的工作表就會成為目前使用的工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var templateSheet = ss.getSheetByName('Sales');
ss.insertSheet('My New Sheet', 1, {template: templateSheet});

參數

名稱類型說明
sheetNameString新工作表的名稱。
sheetIndexInteger新插入工作表的索引。如何將工作表插入 將試算表設為 0
optionsObject選用的 JavaScript 進階引數。

進階參數

名稱類型說明
templateSheet範本工作表物件的所有資料都會複製到 在新工作表中插入範本工作表範本必須是試算表物件的其中一個工作表。

回攻員

Sheet:新的工作表。

授權

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

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

insertSheet(sheetName, options)

在試算表中插入具有指定名稱的新工作表,並選用進階選項 引數。新的工作表就會成為目前使用的工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var templateSheet = ss.getSheetByName('Sales');
ss.insertSheet('My New Sheet', {template: templateSheet});

參數

名稱類型說明
sheetNameString新工作表的名稱。
optionsObject選用的 JavaScript 進階引數。

進階參數

名稱類型說明
templateSheet範本工作表物件的所有資料都會複製到 在新工作表中插入範本工作表範本必須是試算表物件的其中一個工作表。

回攻員

Sheet:新的工作表。

授權

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

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

insertSheetWithDataSourceTable(spec)

在試算表中插入新工作表,建立DataSourceTable並橫跨 再開始執行資料執行作業。一起 即可將新工作表設為使用中的工作表。

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

// Activates BigQuery operations.
SpreadsheetApp.enableBigQueryExecution();

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Adds a sheet and sets cell A1 as the parameter cell.
const parameterCell = ss.insertSheet('parameterSheet').getRange('A1');

// Sets the value of the parameter cell to 'Duke'.
parameterCell.setValue('Duke');

const query = 'select * from `bigquery-public-data`.`ncaa_basketball`.' +
  '`mbb_historical_tournament_games` WHERE win_school_ncaa = @SCHOOL';

// Adds a data source with a query parameter.
// TODO(developer): Update the project ID to your own Google Cloud project ID.
const dataSourceSpec = SpreadsheetApp.newDataSourceSpec()
 .asBigQuery()
 .setProjectId('project-id-1')
 .setRawQuery(query)
 .setParameterFromCell('SCHOOL', 'parameterSheet!A1')
 .build();

// Adds sheets for the data source and data source table to the spreadsheet.
ss.insertSheetWithDataSourceTable(dataSourceSpec);

參數

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

回攻員

Sheet:新的工作表。

授權

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

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

isColumnHiddenByUser(columnPosition)

傳回使用者是否已隱藏指定資料欄。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Columns start at 1
Logger.log(sheet.isColumnHiddenByUser(1));

參數

名稱類型說明
columnPositionInteger要檢查的資料欄位置。

回攻員

Booleantrue如果資料欄為隱藏, false 反之。

授權

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

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

isIterativeCalculationEnabled()

傳回是否要在這個試算表啟用疊代計算。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Activates iterative calculation on the spreadsheet.
ss.setIterativeCalculationEnabled(true);

// Logs whether iterative calculation is activated for the spreadsheet.
console.log(ss.isIterativeCalculationEnabled());

回攻員

Boolean — 如果已啟用疊代計算,則為 true,否則為 false

授權

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

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

isRowHiddenByFilter(rowPosition)

傳回特定資料列是否遭到篩選器 (非篩選器檢視畫面) 隱藏。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Rows start at 1
Logger.log(sheet.isRowHiddenByFilter(1));

參數

名稱類型說明
rowPositionInteger要檢查的資料列位置。

回攻員

Booleantrue 表示隱藏列,否則為 false

授權

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

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

isRowHiddenByUser(rowPosition)

傳回使用者是否已隱藏指定資料列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Rows start at 1
Logger.log(sheet.isRowHiddenByUser(1));

參數

名稱類型說明
rowPositionInteger要檢查的資料列位置。

回攻員

Booleantrue 表示隱藏列,否則為 false

授權

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

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

moveActiveSheet(pos)

將使用中的工作表移至工作表清單中的指定位置。如果 位置為負數或大於工作表數量。

// This example assumes that there are 2 sheets in the current
// active spreadsheet: one named "first" in position 1 and another named "second"
// in position 2.
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// Gets the "first" sheet and activates it.
var sheet = spreadsheet.getSheetByName("first").activate();

// Logs 'Current index of sheet: 1'
console.log("Current index of sheet: %s", sheet.getIndex());

spreadsheet.moveActiveSheet(2);

// Logs 'New index of sheet: 2'
console.log("New index of sheet: %s", sheet.getIndex());

參數

名稱類型說明
posInteger系統在工作表清單中將使用中的工作表移至其 1 索引位置。

授權

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

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

moveChartToObjectSheet(chart)

建立新的 SheetType.OBJECT 工作表,並將提供的圖表移至該工作表。如果圖表顯示 已在其工作表上傳回該工作表,而不會建立新的工作表。

var sheet = SpreadsheetApp.getActiveSheet();
var chart = sheet.newChart().setPosition(1, 1, 0, 0).build();
sheet.insertChart(chart);
var objectSheet = SpreadsheetApp.getActive().moveChartToObjectSheet(chart);

參數

名稱類型說明
chartEmbeddedChart要移動的圖表。

回攻員

Sheet:圖表所在的工作表。

授權

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

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

refreshAllDataSources()

重新整理所有支援的資料來源及連結的資料來源物件,並略過無效動作 資料來源物件

使用 SpreadsheetApp#enable...Execution() 方法即可為以下項目啟用資料執行: 特定資料來源類型

// Activates BigQuery operations.
SpreadsheetApp.enableBigQueryExecution();

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Gets the first data source sheet on the spreadsheet.
const dataSheet = ss.getDataSourceSheets()[0];

// Refreshes all data sources on the spreadsheet.
ss.refreshAllDataSources();

// Logs the last refreshed time of the first data source sheet.
console.log(`Last refresh time: ${dataSheet.getStatus().getLastRefreshedTime()}`);

授權

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

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

removeEditor(emailAddress)

將特定使用者從 Spreadsheet 的編輯者清單中移除。這個方法 如果使用者所屬的類別含有以下使用者類別,則禁止使用者存取 Spreadsheet 一般存取權,例如 Spreadsheet 已與整個使用者共用 網域,或是 Spreadsheet 位於使用者可以存取的共用雲端硬碟中。

針對雲端硬碟檔案,這麼做也會將使用者從檢視者清單中移除。

參數

名稱類型說明
emailAddressString要移除的使用者電子郵件地址。

回攻員

Spreadsheet - 這個 Spreadsheet 用於鏈結。

授權

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

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

removeEditor(user)

將特定使用者從 Spreadsheet 的編輯者清單中移除。這個方法 如果使用者所屬的類別含有以下使用者類別,則禁止使用者存取 Spreadsheet 一般存取權,例如 Spreadsheet 已與整個使用者共用 網域,或是 Spreadsheet 位於使用者可以存取的共用雲端硬碟中。

針對雲端硬碟檔案,這麼做也會將使用者從檢視者清單中移除。

參數

名稱類型說明
userUser要移除的使用者表示法。

回攻員

Spreadsheet - 這個 Spreadsheet 用於鏈結。

授權

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

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

removeMenu(name)

移除 addMenu(name, subMenus) 新增的選單。name 引數 值應與對應的 addMenu(name, subMenus) 呼叫相同。

// The onOpen function is executed automatically every time a Spreadsheet is loaded
function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.addMenu("badMenu", [{name: "remove bad menu", functionName: "removeBadMenu"},
                                       {name: "foo", functionName: "foo"}]);
}
function removeBadMenu() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.removeMenu("badMenu");  // name must match the name used when added the menu
}
function foo(){
// Do nothing
}

參數

名稱類型說明
nameString要移除的選單名稱。

授權

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

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

removeNamedRange(name)

刪除具有指定名稱的已命名範圍。如果沒有指定名稱的範圍,就會擲回例外狀況 都能找到這個工作表

// The code below creates a new named range "foo", and then remove it.
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setNamedRange("foo", ss.getActiveRange());
ss.removeNamedRange("foo");

參數

名稱類型說明
nameString範圍名稱。

授權

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

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

removeViewer(emailAddress)

將特定使用者從 Spreadsheet 的檢視者和加註者清單中移除。這個 如果使用者是編輯者,而不是檢視者或加註者,此方法不會生效。這個方法 無法禁止隸屬於特定類別的使用者存取 Spreadsheet 但具備一般存取權,例如將 Spreadsheet 與使用者的 或是使用者可以存取的共用雲端硬碟中,就刪除 Spreadsheet

針對雲端硬碟檔案,這麼做也會將使用者從編輯者清單中移除。

參數

名稱類型說明
emailAddressString要移除的使用者電子郵件地址。

回攻員

Spreadsheet:這個 Spreadsheet 用於鏈結。

授權

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

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

removeViewer(user)

將特定使用者從 Spreadsheet 的檢視者和加註者清單中移除。這個 如果使用者是編輯者,而不是檢視者,此方法不會有任何作用。這個方法也不會 如果使用者屬於一般使用者的類別,就無法存取 Spreadsheet 存取權,例如 Spreadsheet 已與整個網域共用 如果 Spreadsheet 位於使用者有權存取的共用雲端硬碟中。

針對雲端硬碟檔案,這麼做也會將使用者從編輯者清單中移除。

參數

名稱類型說明
userUser要移除的使用者表示法。

回攻員

Spreadsheet:這個 Spreadsheet 用於鏈結。

授權

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

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

rename(newName)

重新命名文件。

var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.rename("This is the new name");

參數

名稱類型說明
newNameString文件的新名稱。

授權

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

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

renameActiveSheet(newName)

將目前使用中的工作表重新命名為指定新名稱。

// The code below renames the active sheet to "Hello world"
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet("Hello world");

參數

名稱類型說明
newNameString目前使用中工作表的新名稱。

授權

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

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

resetSpreadsheetTheme()

移除套用的主題並設定試算表的預設主題。

// The code below applies default theme on the spreadsheet.
SpreadsheetApp.getActiveSpreadsheet().resetSpreadsheetTheme();

回攻員

SpreadsheetTheme:預設主題。

授權

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

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

setActiveRange(range)

將使用中工作表中的 active range 設為指定範圍,且 範圍中左上角儲存格的 current cell

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getRange('A1:D4');
sheet.setActiveRange(range);

var selection = sheet.getSelection();
// Current cell: A1
var currentCell = selection.getCurrentCell();
// Active Range: A1:D4
var activeRange = selection.getActiveRange();

參數

名稱類型說明
rangeRange要設為有效範圍的範圍。

回攻員

Range:新的有效範圍

授權

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

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

setActiveRangeList(rangeList)

將指定的範圍清單設為active ranges 已開啟工作表。清單的最後一個範圍設為 active range

var sheet = SpreadsheetApp.getActiveSheet();
var rangeList = sheet.getRangeList(['D4', 'B2:C4']);
sheet.setActiveRangeList(rangeList);

var selection = sheet.getSelection();
// Current cell: B2
var currentCell = selection.getCurrentCell();
// Active range: B2:C4
var activeRange = selection.getActiveRange();
// Active range list: [D4, B2:C4]
var activeRangeList = selection.getActiveRangeList();

參數

名稱類型說明
rangeListRangeList要選取的範圍清單。

回攻員

RangeList:新選取的範圍清單

授權

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

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

setActiveSelection(range)

設定這張工作表的有效選取區域。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var range = sheet.getRange("A1:D4");
sheet.setActiveSelection(range);

參數

名稱類型說明
rangeRange要設為作用中的選項的範圍。

回攻員

Range:新的有效範圍

授權

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

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

setActiveSelection(a1Notation)

設定中的選擇,如 A1 標記法或 R1C1 標記法中所指定的內容。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

sheet.setActiveSelection("A1:D4");

參數

名稱類型說明
a1NotationString要設為有效範圍的範圍,如 A1 標記或 R1C1 標記法中所述。

回攻員

Range:新的有效範圍

授權

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

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

setActiveSheet(sheet)

將指定的工作表設為試算表中正在使用的工作表。Google 試算表 UI 除非您的工作表屬於不同試算表

// The code below makes the first sheet active in the active spreadsheet.
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.setActiveSheet(spreadsheet.getSheets()[0]);

參數

名稱類型說明
sheetSheet要設為使用中工作表的工作表。

回攻員

Sheet:正在使用的工作表。

授權

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

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

setActiveSheet(sheet, restoreSelection)

將指定的工作表設為試算表中正在使用的工作表,並可選擇還原 最近期的選取項目Google 試算表 UI 會顯示所選工作表。 此工作表所屬的試算表。

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var firstSheet = spreadsheet.getSheets()[0];
var secondSheet = spreadsheet.getSheets()[1];
// Set the first sheet as the active sheet and select the range D4:F4.
spreadsheet.setActiveSheet(firstSheet).getRange('D4:F4').activate();

// Switch to the second sheet to do some work.
spreadsheet.setActiveSheet(secondSheet);
// Switch back to first sheet, and restore its selection.
spreadsheet.setActiveSheet(firstSheet, true);

// The selection of first sheet is restored, and it logs D4:F4
var range = spreadsheet.getActiveSheet().getSelection().getActiveRange();
Logger.log(range.getA1Notation());

參數

名稱類型說明
sheetSheet新的使用中工作表。
restoreSelectionBooleanTf true,這是最近選取的使用中工作表 就會再次選取新的工作表;如果為 false,則新的工作表 而不會變更目前選取的項目

回攻員

Sheet:新的使用中工作表。

授權

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

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

setColumnWidth(columnPosition, width)

設定指定資料欄的寬度 (以像素為單位)。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Sets the first column to a width of 200 pixels
sheet.setColumnWidth(1, 200);

參數

名稱類型說明
columnPositionInteger要設定指定資料欄的位置。
widthInteger要設定的寬度 (以像素為單位)。

回攻員

Sheet:適合用於方法鏈結的工作表

授權

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

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

setCurrentCell(cell)

將指定儲存格設為 current cell

如果指定的儲存格出現在所選範圍內,則該範圍會變成 作用中範圍,並將儲存格設為目前儲存格。

如果指定的儲存格不在任何所選範圍內,則任一個現有選取範圍將是 這個儲存格就會變成目前的儲存格和使用中的範圍

注意:指定的 Range 必須包含一個儲存格,否則會擲回 例外狀況。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cell = sheet.getRange('B5');
sheet.setCurrentCell(cell);

var selection = sheet.getSelection();
// Current cell: B5
var currentCell = selection.getCurrentCell();

參數

名稱類型說明
cellRange要設為目前儲存格的儲存格。

回攻員

Range:新建的儲存格

授權

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

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

setFrozenColumns(columns)

凍結指定的欄數。如為 0,系統就不會凍結任何資料欄。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Freezes the first column
sheet.setFrozenColumns(1);

參數

名稱類型說明
columnsInteger要凍結的欄數。

授權

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

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

setFrozenRows(rows)

凍結指定的資料列數量。如為 0,表示沒有凍結任何列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Freezes the first row
sheet.setFrozenRows(1);

參數

名稱類型說明
rowsInteger要凍結的列數。

授權

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

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

setIterativeCalculationConvergenceThreshold(minThreshold)

設定反覆運算的最小門檻值。連續發生的結果 計算差異時,值小於這個值,系統會停止反覆運算。這個值必須是 非負數,預設值為 0.05。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Sets the iterative calculation convergence threshold for the spreadsheet.
ss.setIterativeCalculationConvergenceThreshold(2);

// Logs the threshold to the console.
console.log(ss.getIterativeCalculationConvergenceThreshold());

參數

名稱類型說明
minThresholdNumber收斂臨界值下限 (不得為負數)。

回攻員

Spreadsheet:這份試算表用於鏈結。

授權

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

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

setIterativeCalculationEnabled(isEnabled)

設定是否允許在這個試算表使用疊代計算。如果 Pod 的 計算時尚未設定計算週期和收斂閾值 預設會分別設為 50 和 0.05。如果先前已設定 保留先前的值

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Activates iterative calculation on the spreadsheet.
ss.setIterativeCalculationEnabled(true);

// Logs whether iterative calculation is activated for the spreadsheet.
console.log(ss.isIterativeCalculationEnabled());

參數

名稱類型說明
isEnabledBoolean如果應啟用疊代計算,則為 truefalse 反之。

回攻員

Spreadsheet:這份試算表用於鏈結。

授權

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

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

setMaxIterativeCalculationCycles(maxIterations)

設定疊代期間應執行的計算疊代次數上限 計算。這個值必須介於 1 至 10,000 (含) 之間,預設值為 50。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Sets the max iterative calculation cycles for the spreadsheet.
ss.setMaxIterativeCalculationCycles(10);

// Logs the max iterative calculation cycles to the console.
console.log(ss.getMaxIterativeCalculationCycles());

參數

名稱類型說明
maxIterationsInteger計算疊代次數上限 (介於 1 至 10,000 之間),

回攻員

Spreadsheet:這份試算表用於鏈結。

授權

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

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

setNamedRange(name, range)

為範圍命名。

// The code below creates a new named range "TaxRates" in the active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setNamedRange("TaxRates", SpreadsheetApp.getActiveRange());

參數

名稱類型說明
nameString為範圍命名的名稱。
rangeRange範圍規格。

授權

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

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

setRecalculationInterval(recalculationInterval)

設定這份試算表的重新計算頻率。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Sets the  calculation interval for the spreadsheet to 'ON_CHANGE'.
const interval = ss.setRecalculationInterval(SpreadsheetApp.RecalculationInterval.ON_CHANGE);

// Logs the calculation interval to the console.
console.log(interval);

參數

名稱類型說明
recalculationIntervalRecalculationInterval新的重新計算間隔。

回攻員

Spreadsheet:這份試算表用於鏈結。

授權

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

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

setRowHeight(rowPosition, height)

設定指定列的列高 (以像素為單位)。根據預設,列會隨儲存格內容調整大小。如果 如要強制將列強制設為指定高度,請使用 Sheet.setRowHeightsForced(startRow, numRows, height)

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Sets the first row to a height of 200 pixels
sheet.setRowHeight(1, 200);

參數

名稱類型說明
rowPositionInteger要變更的列位置。
heightInteger設定的高度 (以像素為單位)。

回攻員

Sheet:適合用於方法鏈結的工作表。

授權

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

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

setSpreadsheetLocale(locale)

設定試算表語言代碼。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Sets the spreadsheet locale.
ss.setSpreadsheetLocale('fr');

// Gets the spreadsheet locale.
const ssLocale = ss.getSpreadsheetLocale();

// Logs the locale to the console.
console.log(ssLocale);

參數

名稱類型說明
localeString要使用的語言代碼 (例如「en」、「fr」或「en_US」)。

授權

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

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

setSpreadsheetTheme(theme)

設定試算表的主題。

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// The code below sets the second predefined theme as the current theme of the spreadsheet.
var predefinedThemesList = spreadsheet.getPredefinedSpreadsheetThemes();
spreadsheet.setSpreadsheetTheme(predefinedThemesList[1]);

參數

名稱類型說明
themeSpreadsheetTheme要套用的主題。

回攻員

SpreadsheetTheme:目前的主題。

授權

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

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

setSpreadsheetTimeZone(timezone)

設定試算表的時區。

// Opens the spreadsheet file by its URL. If you created your script from within a
// Google Sheets spreadsheet, you can use SpreadsheetApp.getActiveSpreadsheet() instead.
// TODO(developer): Replace the URL with your own.
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit');

// Sets the time zone of the spreadsheet.
ss.setSpreadsheetTimeZone('America/New_York');

// Gets the time zone of the spreadsheet.
const ssTimeZone = ss.getSpreadsheetTimeZone();

// Logs the time zone to the console.
console.log(ssTimeZone);

參數

名稱類型說明
timezoneString時區,指定「long」格式 (例如「America/New_York」、 Joda.org 列出)。

授權

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

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

show(userInterface)

在使用者的瀏覽器中央顯示對話方塊內,顯示自訂使用者介面元件 檢視區域伺服器端指令碼執行不會暫停。為了與 使用者介面元件必須向伺服器端發出非同步回呼 指令碼

如果伺服器端指令碼先前會顯示尚未關閉的對話方塊, 則現有的對話方塊會替換成新要求的對話方塊使用者介面。

下列程式碼片段會在對話方塊中顯示簡易的 HtmlService 應用程式, 指定的標題、高度與寬度:

var htmlApp = HtmlService
    .createHtmlOutput('<p>A change of speed, a change of style...</p>')
    .setTitle('My HtmlService Application')
    .setWidth(250)
    .setHeight(300);

SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);

// The script resumes execution immediately after showing the dialog.

參數

名稱類型說明
userInterfaceObjectHtmlOutput

授權

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

  • https://www.googleapis.com/auth/script.container.ui

sort(columnPosition)

依資料欄遞增排序工作表。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Sorts the sheet by the first column, ascending
sheet.sort(1);

參數

名稱類型說明
columnPositionInteger要做為排序依據的資料欄。

回攻員

Sheet:適合用於方法鏈結的工作表

授權

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

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

sort(columnPosition, ascending)

依欄排序工作表。需要使用參數來指定遞增或遞減。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Sorts the sheet by the first column, descending
sheet.sort(1, false);

參數

名稱類型說明
columnPositionInteger要做為排序依據的資料欄。
ascendingBooleantrue 代表遞增排序,false 代表遞減。

回攻員

Sheet:適合用於方法鏈結的工作表

授權

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

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

toast(msg)

在試算表右下角顯示含有特定訊息的彈出式視窗。

// Show a popup with the message "Task started".
SpreadsheetApp.getActiveSpreadsheet().toast('Task started');

參數

名稱類型說明
msgString要在浮動式訊息中顯示的訊息。

授權

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

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

toast(msg, title)

在試算表右下角顯示彈出式視窗,列出指定訊息和 標題。

// Show a popup with the title "Status" and the message "Task started".
SpreadsheetApp.getActiveSpreadsheet().toast('Task started', 'Status');

參數

名稱類型說明
msgString要在浮動式訊息中顯示的訊息。
titleString選擇性的浮動式訊息標題。

授權

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

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

toast(msg, title, timeoutSeconds)

在試算表右下角顯示彈出式視窗,內含指定標題和 訊息,顯示一段時間。

// Show a 3-second popup with the title "Status" and the message "Task started".
SpreadsheetApp.getActiveSpreadsheet().toast('Task started', 'Status', 3);

參數

名稱類型說明
msgString要在浮動式訊息中顯示的訊息。
titleString選擇性的浮動式訊息標題。
timeoutSecondsNumber逾時 (以秒為單位);如果 null,浮動式訊息預設為 5 秒。 如果為陰性,浮動式訊息會一直保持關閉,直到關閉為止。

授權

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

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

unhideColumn(column)

取消隱藏指定範圍內的欄。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This unhides the first column if it was previously hidden
var range = sheet.getRange("A1");
sheet.unhideColumn(range);

參數

名稱類型說明
columnRange要取消隱藏的範圍 (如果隱藏的話)。

授權

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

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

unhideRow(row)

取消隱藏指定範圍內的列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// This unhides the first row if it was previously hidden
var range = sheet.getRange("A1");
sheet.unhideRow(range);

參數

名稱類型說明
rowRange要取消隱藏的範圍 (如果隱藏的話)。

授權

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

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

updateMenu(name, subMenus)

更新 addMenu(name, subMenus) 新增的選單。運作方式與 addMenu(name, subMenus) 完全相同。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [];
menuEntries.push({name: "Lone Menu Entry", functionName: "function1"});
ss.updateMenu("addMenuExample", menuEntries);

參數

名稱類型說明
nameString要更新的選單名稱。
subMenusObject[]包含 namefunctionName 參數的 JavaScript 對應陣列。您可以使用加入的程式庫中的函式,例如 Library.libFunction1

授權

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

  • 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

已淘汰的方法