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。
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)OverGridImageSpreadsheet 做為圖片,在指定的列和欄的文件中插入 (像素偏移)。
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()

在試算表中取得使用中的工作表。

試算表中使用中的工作表就是試算表 UI 中顯示的工作表。

var 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' 中的任一個也是有效的格式。

回攻員

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

這等同於建立受到 A1 和 (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。例如,網址 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()

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

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)

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

// 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 是在工作表建立時指派的單調整數,與工作表位置無關。這在搭配方法 (例如 Range.copyFormatToRange(gridId, column, columnEnd, row, rowEnd)) 使用 gridId 參數 (而非 Sheet 例項) 時很實用。

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)

從指定座標開始,傳回這個範圍的值矩形格線。指定為列或欄位置的 -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」格式指定的時區 (例如 Joda.org 列出的「America/New_York」)。

授權

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

  • 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-index 位置。

授權

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

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

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

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)

設定是否要在這個試算表中啟用疊代計算。如果先前在啟用計算功能時尚未設定最大計算週期和收斂門檻,則預設值為 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());

參數

名稱類型說明
isEnabledBooleantrue 表示應啟用疊代計算,否則為 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
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

已淘汰的方法