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傳回有效工作表中的有效範圍清單,或如果沒有有效範圍,則傳回 null
getActiveSheet()Sheet取得試算表中的現用工作表。
getAs(contentType)Blob以轉換為指定內容類型的 Blob 形式,傳回這個物件內的資料。
getBandings()Banding[]傳回這個試算表中的所有帶狀。
getBlob()Blob將這個物件內的資料以 Blob 形式傳回。
getColumnWidth(columnPosition)Integer取得指定資料欄的寬度 (以像素為單位)。
getCurrentCell()Range|null傳回有效工作表中的目前儲存格,或 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
getSheetById(id)Sheet|null取得具有指定 ID 的工作表。
getSheetByName(name)Sheet傳回指定名稱的工作表。
getSheetId()Integer傳回這個物件代表的試算表 ID。
getSheetName()String傳回工作表名稱。
getSheetValues(startRow, startColumn, numRows, numColumns)Object[][]傳回這個範圍的值矩形格線,從指定座標開始。
getSheets()Sheet[]取得這份試算表中的所有工作表。
getSpreadsheetLocale()String取得試算表語言代碼。
getSpreadsheetTheme()SpreadsheetTheme|null傳回試算表的目前主題,如果未套用任何主題,則傳回 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)SpreadsheetSpreadsheet 的編輯者清單中移除指定使用者。
removeEditor(user)SpreadsheetSpreadsheet 的編輯者清單中移除指定使用者。
removeMenu(name)void移除 addMenu(name, subMenus) 新增的選單。
removeNamedRange(name)void刪除指定名稱的已命名範圍。
removeViewer(emailAddress)SpreadsheetSpreadsheet 的觀眾和留言者清單中移除指定使用者。
removeViewer(user)SpreadsheetSpreadsheet 的觀眾和留言者清單中移除指定使用者。
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 - This Spreadsheet,用於鏈結。

授權

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

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

addEditor(user)

將指定使用者新增至 Spreadsheet 的編輯者清單。如果使用者已在觀眾名單中,這個方法會將使用者從觀眾名單中移除。

參數

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

回攻員

Spreadsheet - This Spreadsheet,用於鏈結。

授權

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

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

addEditors(emailAddresses)

將指定的使用者陣列新增至 Spreadsheet 的編輯者清單。如果使用者已在觀眾名單中,這個方法會將他們從觀眾名單中移除。

參數

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

回攻員

Spreadsheet - This 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() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const 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 - This Spreadsheet,用於鏈結。

授權

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

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

addViewer(user)

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

參數

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

回攻員

Spreadsheet - This Spreadsheet,用於鏈結。

授權

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

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

addViewers(emailAddresses)

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

參數

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

回攻員

Spreadsheet - This Spreadsheet,用於鏈結。

授權

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

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

appendRow(rowContents)

在試算表目前資料區域的底部附加資料列。如果儲存格內容以 = 開頭,系統會將其解讀為公式。

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

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

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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
const 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)

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

const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();

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

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

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

參數

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

回攻員

TextFinder:試算表的 TextFinder


deleteActiveSheet()

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

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

回攻員

Sheet:新的有效工作表。

授權

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

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

deleteColumn(columnPosition)

刪除指定欄位置的欄。

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

從指定欄位置開始刪除多個欄。

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

刪除指定資料列位置的資料列。

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

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

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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.
const ss = SpreadsheetApp.getActive();
const 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(),這個項目會傳回目前醒目顯示的儲存格。

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

回攻員

Range:目前編輯中的儲存格。

授權

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

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

getActiveRange()

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

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

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

回攻員

Range:有效範圍。

授權

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

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

另請參閱


getActiveRangeList()

傳回有效工作表中的有效範圍清單,或如果沒有有效範圍,則傳回 null

如果只選取單一範圍,這項操作的行為與 getActiveRange() 呼叫相同。

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

回攻員

RangeList|null - 有效範圍清單。

授權

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

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

另請參閱


getActiveSheet()

取得試算表中的現用工作表。

試算表中的現用工作表是指試算表 UI 中顯示的工作表。

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

回攻員

Sheet:試算表中的有效工作表。


getAs(contentType)

以轉換為指定內容類型的 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)

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

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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 中具有焦點的儲存格,並以深色邊框醒目顯示。目前最多只會有一個儲存格。使用者選取一或多個儲存格範圍時,所選範圍中的其中一個儲存格就是目前的儲存格。

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

回攻員

Range|null:目前的儲存格。

授權

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

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

getDataRange()

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

這在功能上等同於建立以 A1 和 (Sheet.getLastColumn()、Sheet.getLastRow()) 為界的範圍。

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

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

// This logs the spreadsheet in CSV format with a trailing comma
for (let i = 0; i < values.length; i++) {
  let row = '';
  for (let 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()

傳回凍結的欄數。

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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()

傳回凍結的列數。

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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 可從網址中擷取。 舉例來說,在網址 https://docs.google.com/spreadsheets/d/abc1234567/edit#gid=0 中,試算表 ID 為「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()

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

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

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

回攻員

Integer:包含內容的試算表最後一欄。

授權

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

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

getLastRow()

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

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

// This logs the value in the very last cell of this sheet
const lastRow = sheet.getLastRow();
const lastColumn = sheet.getLastColumn();
const 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()

取得文件名稱。

const 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.
const namedRanges = SpreadsheetApp.getActiveSpreadsheet().getNamedRanges();
for (let 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

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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.
const 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.
const ss = SpreadsheetApp.getActive();
const protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (let i = 0; i < protections.length; i++) {
  const protection = protections[i];
  if (protection.canEdit()) {
    protection.remove();
  }
}
// Remove all sheet protections in the spreadsheet that the user has permission
// to edit.
const ss = SpreadsheetApp.getActive();
const protections = ss.getProtections(SpreadsheetApp.ProtectionType.SHEET);
for (let i = 0; i < protections.length; i++) {
  const 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"
const ss = SpreadsheetApp.getActiveSpreadsheet();
const range = ss.getRange('Invoices!A1:D4');

// Get cell A1 on the first sheet
const sheet = ss.getSheets()[0];
const 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.
const 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.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const 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)

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

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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

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

回攻員

Selection:目前選取的項目。

授權

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

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

getSheetById(id)

取得具有指定 ID 的工作表。使用 Sheet.getSheetId()

const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetById(12345);

參數

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

回攻員

Sheet|null:具有指定 ID 的工作表,或如果找不到工作表,則為 null


getSheetByName(name)

傳回具有指定名稱的工作表。

如果多個工作表名稱相同,系統會傳回最左側的工作表。如果沒有指定名稱的工作表,則傳回 null

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

參數

名稱類型說明
nameString要取得的試算表名稱。

回攻員

Sheet:指定名稱的工作表。


getSheetId()

傳回這個物件代表的試算表 ID。

這是試算表的專屬工作表 ID。這個 ID 是在建立工作表時指派的單調遞增整數,與工作表位置無關。這與 Range.copyFormatToRange(gridId, column, columnEnd, row, rowEnd) 等方法搭配使用時非常實用,因為這些方法會採用 gridId 參數,而非 Sheet 例項。

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

Logger.log(sheet.getSheetId());

回攻員

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

授權

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

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

getSheetName()

傳回工作表名稱。

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

傳回這個範圍的值矩形格線,從指定座標開始。如果列或欄位置的值為 -1,就相當於取得工作表中含有資料的最後一列或最後一欄。

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

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

const 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
const sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
// Iterates through the sheets and logs the name and ID of each sheet.
for (const sheet of sheets) {
  Logger.log(`name: ${sheet.getName()}, ID: ${sheet.getSheetId()}`);
}

回攻員

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.
const currentTheme =
    SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTheme();

回攻員

SpreadsheetTheme|null:目前套用的主題。

授權

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

  • 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()

傳回指定試算表的網址。

const 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)

隱藏指定範圍中的欄。

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

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

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

參數

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

授權

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

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

hideRow(row)

隱藏指定範圍內的資料列。

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

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

參數

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

授權

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

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

insertColumnAfter(afterPosition)

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

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

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

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

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

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

在指定欄位置之前插入多個欄。

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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。

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

const binaryData = [];  // TODO(developer): Replace with your binary data.
const 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。

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

const binaryData = [];  // TODO(developer): Replace with your binary data.
const 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)

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

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

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

在指定列和欄中插入圖片,並設定像素偏移量。

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

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

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

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

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

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

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

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

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

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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()

在試算表中插入新工作表,並使用預設工作表名稱。新工作表會成為有效工作表。

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

回攻員

Sheet:新工作表。

授權

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

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

insertSheet(sheetIndex)

在指定索引處將新工作表插入試算表。新工作表會成為有效工作表。

const 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)

在指定索引處將新工作表插入試算表,並使用選用的進階引數。新工作表會成為有效工作表。

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

在試算表中插入新工作表,使用預設工作表名稱和選用的進階引數。新工作表會成為有效工作表。

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

在試算表中插入新工作表,並使用指定名稱。新工作表會成為有效工作表。

const 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)

在試算表的指定索引處,插入具有指定名稱的新工作表。新工作表會成為使用中的工作表。

const 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)

在試算表中指定索引處插入新工作表,並使用選用的進階引數。新工作表會成為有效工作表。

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

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

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

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

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

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

參數

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

回攻員

Boolean:如果資料欄已隱藏,則為 true,否則為 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)

傳回指定資料列是否因篩選器 (而非篩選器檢視畫面) 而隱藏。

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

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

參數

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

回攻員

Boolean:如果資料列已隱藏,則為 true,否則為 false

授權

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

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

isRowHiddenByUser(rowPosition)

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

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

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

參數

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

回攻員

Boolean:如果資料列已隱藏,則為 true,否則為 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.
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// Gets the "first" sheet and activates it.
const 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 工作表,並將提供的圖表移至該工作表。如果圖表已位於專屬工作表,系統會傳回該工作表,不會建立新工作表。

const sheet = SpreadsheetApp.getActiveSheet();
const chart = sheet.newChart().setPosition(1, 1, 0, 0).build();
sheet.insertChart(chart);
const 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 - This Spreadsheet,用於鏈結。

授權

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

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

removeEditor(user)

Spreadsheet 的編輯者清單中移除指定使用者。如果使用者屬於具有一般存取權的使用者類別,例如 Spreadsheet 是與使用者的整個網域共用,或是 Spreadsheet 位於使用者可存取的共用雲端硬碟中,這個方法就無法禁止使用者存取 Spreadsheet

如果是雲端硬碟檔案,系統也會從檢視者清單中移除該使用者。

參數

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

回攻員

Spreadsheet - This 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() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.addMenu('badMenu', [
    {name: 'remove bad menu', functionName: 'removeBadMenu'},
    {name: 'foo', functionName: 'foo'},
  ]);
}
function removeBadMenu() {
  const 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.
const 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)

重新命名文件。

const 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

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

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

參數

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

回攻員

Range:新啟用的範圍。

授權

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

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

setActiveRangeList(rangeList)

將指定範圍清單設為作用中工作表中的 active ranges。清單中的最後一個範圍會設為 active range

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

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

參數

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

回攻員

RangeList:新選取的範圍清單。

授權

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

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

setActiveSelection(range)

為這個工作表設定有效選取區域。

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

const 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 標記法指定的作用中選取範圍。

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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.
const 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 會顯示所選工作表,除非該工作表屬於其他試算表。

const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const firstSheet = spreadsheet.getSheets()[0];
const 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
const range = spreadsheet.getActiveSheet().getSelection().getActiveRange();
Logger.log(range.getA1Notation());

參數

名稱類型說明
sheetSheet新的現用工作表。
restoreSelectionBoolean如果 true,系統會在新工作表啟用時,再次選取您最近選取的新工作表;如果 false,系統會啟用新工作表,但不會變更目前選取項目。

回攻員

Sheet:新的有效工作表。

授權

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

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

setColumnWidth(columnPosition, width)

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

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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 必須由一個儲存格組成,否則會擲回例外狀況。

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

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

參數

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

回攻員

Range:新設定的目前儲存格。

授權

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

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

setFrozenColumns(columns)

凍結指定欄數。如果為零,則不會凍結任何資料欄。

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

凍結指定列數。如果為零,則不會凍結任何資料列。

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

設定是否要在這份試算表中啟用反覆運算。如果啟用計算時,尚未設定計算週期上限和收斂門檻,系統會分別預設為 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如果應啟用反覆運算,則為 true;否則為 false

回攻員

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
const 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)

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

在試算表上設定主題。

const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// The code below sets the second predefined theme as the current theme of the
// spreadsheet.
const 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 應用程式,並指定標題、高度和寬度:

const 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)

依欄遞增排序工作表。

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

依資料欄排序工作表。這個函式會採用參數來指定遞增或遞減。

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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)

取消隱藏指定範圍內的資料欄。

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

// This unhides the first column if it was previously hidden
const range = sheet.getRange('A1');
sheet.unhideColumn(range);

參數

名稱類型說明
columnRange要取消隱藏的範圍 (如有隱藏)。

授權

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

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

unhideRow(row)

取消隱藏指定範圍內的資料列。

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

// This unhides the first row if it was previously hidden
const 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) 完全相同。

const ss = SpreadsheetApp.getActiveSpreadsheet();
const 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

已淘汰的方法