Class Sheet

工作表

存取及修改試算表工作表。常見的作業是重新命名工作表,並從工作表存取範圍物件。

方法

方法傳回類型簡短說明
activate()Sheet啟用這份工作表。
addDeveloperMetadata(key)Sheet在工作表中新增具有指定金鑰的開發人員中繼資料。
addDeveloperMetadata(key, visibility)Sheet在工作表中新增具有指定金鑰和瀏覽權限的開發人員中繼資料。
addDeveloperMetadata(key, value)Sheet在工作表中新增具有指定金鑰和值的開發人員中繼資料。
addDeveloperMetadata(key, value, visibility)Sheet為工作表新增具有指定金鑰、值和瀏覽權限的開發人員中繼資料。
appendRow(rowContents)Sheet在工作表中目前資料區域底部附加一列。
asDataSourceSheet()DataSourceSheet如果工作表為 SheetType.DATASOURCE 類型,則傳回工作表為 DataSourceSheet,否則傳回 null
autoResizeColumn(columnPosition)Sheet設定指定資料欄的寬度以符合內容。
autoResizeColumns(startColumn, numColumns)Sheet設定所有欄的寬度,從指定資料欄的位置開始,以符合內容大小。
autoResizeRows(startRow, numRows)Sheet設定所有列的高度,從指定資料列位置開始符合內容。
clear()Sheet清除工作表內容和格式資訊。
clear(options)Sheet根據指定的進階選項清除目錄和/或格式。
clearConditionalFormatRules()void從工作表中移除所有條件式格式規則。
clearContents()Sheet清除內容工作表,同時保留格式資訊。
clearFormats()Sheet清除格式工作表,同時保留內容。
clearNotes()Sheet清除所有記事的工作表。
collapseAllColumnGroups()Sheet收合工作表上所有的欄群組。
collapseAllRowGroups()Sheet收合工作表上所有的列群組。
copyTo(spreadsheet)Sheet將工作表複製到指定試算表,可以是來源試算表。
createDeveloperMetadataFinder()DeveloperMetadataFinder傳回 DeveloperMetadataFinder,用於尋找這份工作表範圍內的開發人員中繼資料。
createTextFinder(findText)TextFinder為工作表建立文字搜尋工具,可用於尋找和取代工作表中的文字。
deleteColumn(columnPosition)Sheet在指定資料欄位置刪除資料欄。
deleteColumns(columnPosition, howMany)void從指定資料欄位置開始刪除多個資料欄。
deleteRow(rowPosition)Sheet在指定資料列位置刪除資料列。
deleteRows(rowPosition, howMany)void從指定資料列位置開始刪除列數。
expandAllColumnGroups()Sheet展開工作表中的所有欄群組。
expandAllRowGroups()Sheet展開工作表上所有的列群組。
expandColumnGroupsUpToDepth(groupDepth)Sheet將所有資料欄群組展開至指定深度,並收合所有其他資料欄。
expandRowGroupsUpToDepth(groupDepth)Sheet將所有列群組展開至指定深度,並收合所有其他資料列。
getActiveCell()Range傳回這份工作表中的有效儲存格。
getActiveRange()Range傳回使用中工作表中選取的範圍;如果沒有使用中的範圍,則傳回 null
getActiveRangeList()RangeList傳回使用中工作表中的有效範圍清單;如果沒有有效範圍,則傳回 null
getBandings()Banding[]傳回這份工作表中的所有色帶。
getCharts()EmbeddedChart[]傳回這份工作表中的圖表陣列。
getColumnGroup(columnIndex, groupDepth)Group傳回指定索引和群組深度的資料欄群組。
getColumnGroupControlPosition()GroupControlTogglePosition傳回工作表上所有欄群組的 GroupControlTogglePosition
getColumnGroupDepth(columnIndex)Integer傳回指定索引的資料欄深度。
getColumnWidth(columnPosition)Integer取得指定資料欄的寬度 (以像素為單位)。
getConditionalFormatRules()ConditionalFormatRule[]取得這份工作表中的所有條件式格式規則。
getCurrentCell()Range傳回目前工作表中目前的儲存格;如果沒有目前儲存格,則傳回 null
getDataRange()Range傳回與顯示資料的維度相對應的 Range
getDataSourceFormulas()DataSourceFormula[]取得所有資料來源公式。
getDataSourcePivotTables()DataSourcePivotTable[]取得所有資料來源資料透視表。
getDataSourceTables()DataSourceTable[]取得所有資料來源資料表。
getDeveloperMetadata()DeveloperMetadata[]取得與這份工作表相關聯的所有開發人員中繼資料。
getDrawings()Drawing[]傳回工作表上繪圖的陣列。
getFilter()Filter傳回這份工作表中的篩選器;如果沒有篩選器,則傳回 null
getFormUrl()String回傳將回應傳送至此工作表的表單網址;如果這個工作表沒有相關聯的表單,則傳回 null
getFrozenColumns()Integer傳回凍結欄數。
getFrozenRows()Integer傳回凍結的資料列數。
getImages()OverGridImage[]傳回工作表上所有在格線上疊加的圖片。
getIndex()Integer取得工作表在父項試算表中的位置。
getLastColumn()Integer傳回最後一欄包含內容的位置。
getLastRow()Integer傳回最後一列包含內容的位置。
getMaxColumns()Integer傳回工作表中目前的欄數 (無論內容為何)。
getMaxRows()Integer傳回工作表中目前的資料列數量,不論內容為何。
getName()String傳回工作表名稱。
getNamedRanges()NamedRange[]取得這份工作表中的所有已命名範圍。
getParent()Spreadsheet傳回包含此工作表的 Spreadsheet
getPivotTables()PivotTable[]傳回這份工作表中的所有資料透視表。
getProtections(type)Protection[]取得代表工作表中所有受保護範圍的物件陣列,或代表工作表本身上保護範圍的單一元素陣列。
getRange(row, column)Range傳回指定座標左上方的儲存格範圍。
getRange(row, column, numRows)Range傳回指定座標的左上角儲存格範圍,以及指定列數的範圍。
getRange(row, column, numRows, numColumns)Range傳回指定座標上左上角儲存格的範圍,此範圍包含指定的資料列與欄數。
getRange(a1Notation)Range傳回 A1 標記或 R1C1 標記法中指定的範圍。
getRangeList(a1Notations)RangeList傳回 RangeList 集合,代表由 A1 標記或 R1C1 標記的非空白清單指定同一工作表中的範圍。
getRowGroup(rowIndex, groupDepth)Group傳回指定索引和群組深度的資料列群組。
getRowGroupControlPosition()GroupControlTogglePosition傳回工作表上所有列群組的 GroupControlTogglePosition
getRowGroupDepth(rowIndex)Integer傳回指定索引的資料列深度。
getRowHeight(rowPosition)Integer取得指定資料列的高度 (以像素為單位)。
getSelection()Selection傳回試算表中目前的 Selection
getSheetId()Integer傳回這個物件所代表的工作表 ID。
getSheetName()String傳回工作表名稱。
getSheetValues(startRow, startColumn, numRows, numColumns)Object[][]從指定座標開始,傳回這個範圍的值矩形格線。
getSlicers()Slicer[]傳回工作表上篩選器控制項的陣列。
getTabColorObject()Color取得工作表分頁顏色;如果工作表分頁沒有顏色,則傳回 null
getType()SheetType傳回工作表類型。
hasHiddenGridlines()Boolean如果工作表的格線隱藏,會傳回 true;否則會傳回 false
hideColumn(column)void隱藏指定範圍內的一或多個資料欄。
hideColumns(columnIndex)void在指定索引處隱藏單一資料欄。
hideColumns(columnIndex, numColumns)void隱藏從指定索引開始的一或多個連續資料欄。
hideRow(row)void隱藏指定範圍內的資料列。
hideRows(rowIndex)void隱藏指定索引的資料列。
hideRows(rowIndex, numRows)void隱藏從指定索引開始的一或多個連續資料列。
hideSheet()Sheet隱藏這張工作表。
insertChart(chart)void將新圖表加入這份工作表。
insertColumnAfter(afterPosition)Sheet在指定資料欄位置之後插入資料欄。
insertColumnBefore(beforePosition)Sheet在指定資料欄位置之前插入資料欄。
insertColumns(columnIndex)void在指定位置的工作表中插入空白欄。
insertColumns(columnIndex, numColumns)void從指定的位置開始,在工作表中插入一或多個連續的空白欄。
insertColumnsAfter(afterPosition, howMany)Sheet在指定資料欄位置之後插入指定數量的欄。
insertColumnsBefore(beforePosition, howMany)Sheet在指定資料欄位置之前插入指定欄數。
insertImage(blobSource, column, row)OverGridImage在指定的列和欄中將 BlobSource 以圖片形式插入文件中。
insertImage(blobSource, column, row, offsetX, offsetY)OverGridImageBlobSource 做為圖片,在指定的列和欄的文件中插入 (像素偏移)。
insertImage(url, column, row)OverGridImage在文件中的指定列和欄插入圖片。
insertImage(url, column, row, offsetX, offsetY)OverGridImage在文件中的指定列和欄插入圖片 (採用像素偏移)。
insertRowAfter(afterPosition)Sheet在指定列位置後方插入列。
insertRowBefore(beforePosition)Sheet在指定資料列位置之前插入一列。
insertRows(rowIndex)void在指定位置的工作表中插入空白列。
insertRows(rowIndex, numRows)void從指定的位置開始,在工作表中插入一或多個連續的空白列。
insertRowsAfter(afterPosition, howMany)Sheet在指定資料列位置後方插入列數。
insertRowsBefore(beforePosition, howMany)Sheet在給定列位置前插入列數。
insertSlicer(range, anchorRowPos, anchorColPos)Slicer將篩選器控制項新增至這張工作表。
insertSlicer(range, anchorRowPos, anchorColPos, offsetX, offsetY)Slicer將篩選器控制項新增至這張工作表。
isColumnHiddenByUser(columnPosition)Boolean傳回使用者是否隱藏了指定的資料欄。
isRightToLeft()Boolean如果這個工作表版面配置為從右到左,會傳回 true
isRowHiddenByFilter(rowPosition)Boolean傳回篩選器是否隱藏指定資料列 (而非篩選器檢視畫面)。
isRowHiddenByUser(rowPosition)Boolean傳回使用者是否隱藏指定的資料列。
isSheetHidden()Boolean如果工作表目前設為隱藏,系統會傳回 true
moveColumns(columnSpec, destinationIndex)void將指定範圍選取的資料欄移至 destinationIndex 指定的位置。
moveRows(rowSpec, destinationIndex)void將指定範圍選取的資料列移至 destinationIndex 指定的位置。
newChart()EmbeddedChartBuilder傳回建構工具,為這張工作表建立新圖表。
protect()Protection建立可防止工作表編輯的物件,但具備權限的使用者除外。
removeChart(chart)void從父項工作表中移除圖表。
setActiveRange(range)Range將指定範圍設為使用中工作表中的 active range,範圍的左上方儲存格為 current cell
setActiveRangeList(rangeList)RangeList將指定範圍清單設為使用中工作表中的 active ranges
setActiveSelection(range)Range設定這張工作表的有效選取區域。
setActiveSelection(a1Notation)Range設定使用中的選項,如 A1 標記法或 R1C1 標記法中所述。
setColumnGroupControlPosition(position)Sheet設定欄群組控制項在工作表上的切換位置。
setColumnWidth(columnPosition, width)Sheet設定指定欄的寬度 (以像素為單位)。
setColumnWidths(startColumn, numColumns, width)Sheet設定指定欄的寬度 (以像素為單位)。
setConditionalFormatRules(rules)void使用輸入規則取代工作表中所有現有的條件式格式規則。
setCurrentCell(cell)Range將指定的儲存格設為 current cell
setFrozenColumns(columns)void凍結指定的欄數。
setFrozenRows(rows)void凍結指定的資料列數。
setHiddenGridlines(hideGridlines)Sheet隱藏或顯示工作表格線。
setName(name)Sheet設定工作表名稱。
setRightToLeft(rightToLeft)Sheet將工作表版面配置設定成從右到左。
setRowGroupControlPosition(position)Sheet設定列群組控制項在工作表上的切換位置。
setRowHeight(rowPosition, height)Sheet設定指定列的列高 (以像素為單位)。
setRowHeights(startRow, numRows, height)Sheet設定指定列的高度 (以像素為單位)。
setRowHeightsForced(startRow, numRows, height)Sheet設定指定列的高度 (以像素為單位)。
setTabColor(color)Sheet設定工作表分頁的顏色。
setTabColorObject(color)Sheet設定工作表分頁的顏色。
showColumns(columnIndex)void將指定索引取消隱藏欄。
showColumns(columnIndex, numColumns)void取消隱藏一或多個連續的資料欄 (從指定索引開始)。
showRows(rowIndex)void將指定索引取消隱藏列。
showRows(rowIndex, numRows)void將從指定索引開始的一或多個連續資料列取消隱藏。
showSheet()Sheet顯示工作表。
sort(columnPosition)Sheet依欄遞增排序工作表。
sort(columnPosition, ascending)Sheet依欄排序工作表。
unhideColumn(column)void取消隱藏指定範圍內的資料欄。
unhideRow(row)void取消隱藏指定範圍內的資料列。
updateChart(chart)void更新這份工作表中的圖表。

內容詳盡的說明文件

activate()

啟用這份工作表。不會變更工作表本身,只會變更顯示中工作表的父項概念。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
first.activate();

回攻員

Sheet:新使用中的工作表。


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');

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

// Adds the key 'NAME' to the developer metadata for the sheet.
sheet.addDeveloperMetadata('NAME');

// Gets the updated metadata info and logs it to the console.
console.log(sheet.getDeveloperMetadata()[0].getKey());

參數

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

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

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

// Adds the key 'NAME' and sets the developer metadata visibility to PROJECT
// for the sheet.
sheet.addDeveloperMetadata('NAME', SpreadsheetApp.DeveloperMetadataVisibility.PROJECT);

// Gets the updated metadata info and logs it to the console.
const developerMetaData = sheet.getDeveloperMetadata()[0];
console.log(developerMetaData.getKey());
console.log(developerMetaData.getVisibility().toString());

參數

名稱類型說明
keyString新開發人員中繼資料的金鑰。
visibilityDeveloperMetadataVisibility新開發人員中繼資料的顯示設定。

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

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

// Adds the key 'COMPANY' with the value 'TECH' to the developer metadata for the sheet.
sheet.addDeveloperMetadata('COMPANY', 'TECH');

// Gets the updated metadata info and logs it to the console.
const developerMetaData = sheet.getDeveloperMetadata()[0];
console.log(developerMetaData.getKey());
console.log(developerMetaData.getValue());

參數

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

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

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

// Adds the key 'COMPANY' with the value 'TECH' to the developer metadata and sets the
// visibility to DOCUMENT for the sheet.
sheet.addDeveloperMetadata(
  'COMPANY',
  'TECH',
  SpreadsheetApp.DeveloperMetadataVisibility.DOCUMENT);

// Gets the updated metadata info and logs it to the console.
const developerMetaData = sheet.getDeveloperMetadata()[0];
console.log(developerMetaData.getKey());
console.log(developerMetaData.getValue());
console.log(developerMetaData.getVisibility().toString());

參數

名稱類型說明
keyString新開發人員中繼資料的金鑰。
valueString新開發人員中繼資料的值。
visibilityDeveloperMetadataVisibility新開發人員中繼資料的顯示設定。

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

  • 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

asDataSourceSheet()

如果工作表為 SheetType.DATASOURCE 類型,則傳回工作表為 DataSourceSheet,否則傳回 null

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

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

// Gets the data source sheet value if the sheet is of type
// SpreadsheetApp.SheetType.DATASOURCE, otherwise this returns a null value.
const dataSourceSheet = sheet.asDataSourceSheet();

// Gets the data source sheet value and logs it to the console.
console.log(dataSourceSheet);
console.log(sheet.getType().toString());

回攻員

DataSourceSheet:資料來源工作表。

授權

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

  • 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

autoResizeColumns(startColumn, numColumns)

設定所有欄的寬度,起始於指定資料欄位置,以符合內容。

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

// Sets the first 15 columns to a width that fits their text.
sheet.autoResizeColumns(1, 15);

參數

名稱類型說明
startColumnInteger用於自動調整大小的起始資料欄。
numColumnsInteger要自動調整大小的欄數。

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

autoResizeRows(startRow, numRows)

設定所有資料列的高度,從指定資料列位置開始符合內容。

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

// Sets the first 15 rows to a height that fits their text.
sheet.autoResizeRows(1, 15);

參數

名稱類型說明
startRowInteger要自動調整大小的起始列。
numRowsInteger要自動調整大小的列數。

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

clear()

清除工作表內容和格式資訊。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
first.clear();

回攻員

Sheet - 清除的工作表。

授權

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

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

clear(options)

根據指定的進階選項清除目錄和/或格式。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
sheet.clear({ formatOnly: true, contentsOnly: true });

參數

名稱類型說明
optionsObject包含下方所列進階選項的 JavaScript 地圖。

進階參數

名稱類型說明
contentsOnlyBoolean是否要清除內容。
formatOnlyBoolean是否要清除格式。

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

clearConditionalFormatRules()

從工作表中移除所有條件式格式規則。相當於以空白陣列做為輸入內容呼叫 setConditionalFormatRules(rules)

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

授權

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

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

clearContents()

清除內容工作表,同時保留格式資訊。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
first.clearContents();

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

clearFormats()

清除格式工作表,同時保留內容。

「格式設定」是指「格式」選單的選項 (例如粗體、斜體、條件式格式設定) 允許的資料格式,以及儲存格的寬度或高度。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
first.clearFormats();

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

clearNotes()

清除所有記事的工作表。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
first.clearNotes();

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

collapseAllColumnGroups()

收合工作表上所有的欄群組。

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

// All column groups on the sheet are collapsed.
sheet.collapseAllColumnGroups();

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

collapseAllRowGroups()

收合工作表上所有的列群組。

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

// All row groups on the sheet are collapsed.
sheet.collapseAllRowGroups();

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

copyTo(spreadsheet)

將工作表複製到指定試算表,可以是來源試算表。複製的工作表命名為「[原始名稱] 的副本」。

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

var destination = SpreadsheetApp.openById('ID_GOES HERE');
sheet.copyTo(destination);

參數

名稱類型說明
spreadsheetSpreadsheet要複製這份工作表的試算表,可以是與來源相同的試算表。

回攻員

Sheet — 用於鏈結的新工作表。

授權

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

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

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

// Adds developer metadata for testing.
sheet.addDeveloperMetadata('CITY', 'PARIS');

// Creates the developer metadata finder.
const metadatafinder = sheet.createDeveloperMetadataFinder();

// Finds the metadata with value 'PARIS' and displays its key in the console.
console.log(metadatafinder.withValue('PARIS').find()[0].getKey());

回攻員

DeveloperMetadataFinder:開發人員中繼資料搜尋工具,用於搜尋這份工作表範圍內的中繼資料。


createTextFinder(findText)

為工作表建立文字搜尋工具,可用於尋找和取代工作表中的文字。

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

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

// Returns the first occurrence of 'dog' in the sheet.
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


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

expandAllColumnGroups()

展開工作表中的所有欄群組。這個方法需要至少一個欄群組。

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

// All column groups on the sheet are expanded.
sheet.expandAllColumnGroups();

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

expandAllRowGroups()

展開工作表上所有的列群組。這個方法需要至少一個資料列群組。

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

// All row groups on the sheet are expanded.
sheet.expandAllRowGroups();

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

expandColumnGroupsUpToDepth(groupDepth)

將所有欄群組展開至指定深度,並收合所有其他資料欄。

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

// All column groups of depth 2 and lower are expanded, and groups with depth
// 3 and higher are collapsed.
sheet.expandColumnGroupsUpToDepth(2);

參數

名稱類型說明
groupDepthInteger展開資料欄群組的範圍。

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

expandRowGroupsUpToDepth(groupDepth)

將所有列群組展開至指定深度,並收合所有其他資料列。

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

// All row groups of depth 2 and lower are expanded, and groups with depth
// 3 and higher are collapsed.
sheet.expandRowGroupsUpToDepth(2);

參數

名稱類型說明
groupDepthInteger展開列群組的目標深度。

回攻員

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

另請參閱


getBandings()

傳回這份工作表中的所有色帶。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets the banding info for the sheet.
const bandings = sheet.getBandings();

// Gets info on the bandings' second row color and logs it to the console.
for (const banding of bandings) {
  console.log(banding.getSecondRowColor());
}

回攻員

Banding[] - 這份工作表中的所有頻帶。

授權

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

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

getCharts()

傳回這份工作表中的圖表陣列。

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

for (var i in charts) {
  var chart = charts[i];
  // Do something with the chart
}

回攻員

EmbeddedChart[]:一組圖表。

授權

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

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

getColumnGroup(columnIndex, groupDepth)

傳回指定索引和群組深度的資料欄群組。

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

// Returns the group whose control index is at column 2 and has a depth of 1, or
// null if the group doesn’t exist.
var columnGroup = sheet.getColumnGroup(2, 1);

參數

名稱類型說明
columnIndexInteger群組控制項切換或群組內的索引欄索引。
groupDepthInteger群組深度。

回攻員

Group:位於控制索引和深度的資料欄群組;如果群組不存在,則會擲回例外狀況。

授權

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

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

getColumnGroupControlPosition()

傳回工作表上所有欄群組的 GroupControlTogglePosition

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

// GroupControlTogglePosition.AFTER if the column grouping control toggle is shown after the
// group.
var columnGroupControlPosition = sheet.getColumnGroupControlPosition();

回攻員

GroupControlTogglePosition:如果這份工作表中的群組之後顯示資料欄分組控制項切換按鈕,則為 true;否則為 false

授權

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

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

getColumnGroupDepth(columnIndex)

傳回指定索引的資料欄深度。

群組深度代表與資料欄重疊的群組數量。範圍可介於 0 到 8 之間。

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

// 1 if there is a group over columns 1 through 3
var groupDepth = sheet.getColumnGroupDepth(1);

參數

名稱類型說明
columnIndexInteger資料欄的索引。

回攻員

Integer:指定索引中的資料欄深度,

授權

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

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

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

getConditionalFormatRules()

取得這份工作表中的所有條件式格式規則。

// Logs the conditional format rules in a sheet.
var rules = SpreadsheetApp.getActiveSheet().getConditionalFormatRules();
for (var i = 0; i < rules.length; i++) {
  var rule = rules[i];
  Logger.log(rule);
}

回攻員

ConditionalFormatRule[]:工作表中所有規則的陣列。

授權

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

  • 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

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

getDeveloperMetadata()

取得與這份工作表相關聯的所有開發人員中繼資料。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Adds developer metadata for testing.
sheet.addDeveloperMetadata('CITY', 'PARIS');

// Gets all the developer metadata for the sheet.
const developerMetaDataList = sheet.getDeveloperMetadata();

// Logs the developer metadata to the console.
for (const developerMetaData of developerMetaDataList) {
  console.log(developerMetaData.getKey());
}

回攻員

DeveloperMetadata[]:與這份工作表相關聯的開發人員中繼資料。

授權

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

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

getDrawings()

傳回工作表上繪圖的陣列。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets all the drawings from the sheet.
const allDrawings = sheet.getDrawings();

 // Logs the number of drawings present on the sheet.
console.log(allDrawings.length);

回攻員

Drawing[]:這份工作表中的繪圖清單。

授權

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

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

getFilter()

傳回這份工作表中的篩選器;如果沒有篩選器,則傳回 null

// Gets the filter on the active sheet.
let ss = SpreadsheetApp.getActiveSheet();
let filter = ss.getFilter();

回攻員

Filter:篩選器。

授權

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

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

getFormUrl()

傳回表單網址,用於將回應傳送至這個工作表;如果這個工作表沒有相關聯的表單,則傳回 null

var sheet = SpreadsheetApp.getActiveSheet();
var url = sheet.getFormUrl();

回攻員

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

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

getIndex()

取得工作表在父項試算表中的位置。從 1 開始。

var ss = SpreadsheetApp.getActiveSpreadsheet();
// Note that the JavaScript index is 0, but this logs 1
var sheet = ss.getSheets()[0];
// ... because spreadsheets are 1-indexed
Logger.log(sheet.getIndex());

回攻員

Integer:工作表在父項試算表中的位置。

授權

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

  • 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

getMaxColumns()

傳回工作表中目前的欄數 (無論內容為何)。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
Logger.log(first.getMaxColumns());

回攻員

Integer:工作表的寬度上限。

授權

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

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

getMaxRows()

傳回工作表中目前的資料列數量,不論內容為何。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
Logger.log(first.getMaxRows());

回攻員

Integer:工作表的高度上限。

授權

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

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

getName()

傳回工作表名稱。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
Logger.log(sheet.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.getActiveSheet().getNamedRanges();
if (namedRanges.length > 1) {
  Logger.log(namedRanges[0].getName());
}

回攻員

NamedRange[]:工作表中所有已命名範圍的陣列。

授權

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

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

getParent()

傳回包含這份工作表的 Spreadsheet

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// parent is identical to ss
var parent = sheet.getParent();

回攻員

Spreadsheet:父項試算表。

授權

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

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

getPivotTables()

傳回這份工作表中的所有資料透視表。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets all the pivot table info for the sheet.
const pivotTables = sheet.getPivotTables();

// Logs the pivot tables to the console.
for (const pivotTable of pivotTables) {
  console.log(pivotTable.getSourceDataRange().getValues());
}

回攻員

PivotTable[]:這份工作表中的資料透視表,

授權

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

  • 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 sheet = SpreadsheetApp.getActiveSheet();
var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
  var protection = protections[i];
  if (protection.canEdit()) {
    protection.remove();
  }
}
// Remove sheet protection from the active sheet, if the user has permission to edit it.
var sheet = SpreadsheetApp.getActiveSheet();
var protection = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
if (protection && 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(row, column)

傳回指定座標頂端儲存格左上方的範圍。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Passing only two arguments returns a "range" with a single cell.
var range = sheet.getRange(1, 1);
var values = range.getValues();
Logger.log(values[0][0]);

參數

名稱類型說明
rowInteger要傳回的儲存格所在的列索引;列索引會從 1 開始。
columnInteger要傳回的儲存格所在的欄索引;欄索引會從 1 開始。

回攻員

Range:僅包含這個儲存格的範圍。

授權

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

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

getRange(row, column, numRows)

傳回指定座標的左上角儲存格範圍,以及指定列數的範圍。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// When the "numRows" argument is used, only a single column of data is returned.
var range = sheet.getRange(1, 1, 3);
var values = range.getValues();

// Prints 3 values from the first column, starting from row 1.
for (var row in values) {
  for (var col in values[row]) {
    Logger.log(values[row][col]);
  }
}

參數

名稱類型說明
rowInteger範圍的起始列索引;列索引會從 1 開始。
columnInteger範圍的欄索引;欄索引會從 1 開始。
numRowsInteger要傳回的列數。

回攻員

Range — 單一欄包含指定列數的範圍。

授權

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

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

getRange(row, column, numRows, numColumns)

傳回指定座標上左上角儲存格的範圍,此範圍包含指定的資料列和欄數。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange(1, 1, 3, 3);
var values = range.getValues();

// Print values from a 3x3 box.
for (var row in values) {
  for (var col in values[row]) {
    Logger.log(values[row][col]);
  }
}

參數

名稱類型說明
rowInteger範圍的起始列索引;列索引會從 1 開始。
columnInteger範圍的起始欄索引;欄索引會從 1 開始。
numRowsInteger要傳回的列數。
numColumnsInteger要傳回的欄數。

回攻員

Range:與指定區域相對應的範圍。

授權

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

  • 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

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

getRowGroup(rowIndex, groupDepth)

傳回指定索引和群組深度的資料列群組。

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

// Returns the group whose control index is at row 2 and has a depth of 1, or
// null if the group doesn’t exist.
var rowGroup = sheet.getRowGroup(2, 1);

參數

名稱類型說明
rowIndexInteger群組控制項切換或群組中索引的資料列索引。
groupDepthInteger群組深度。

回攻員

Group:位於控制索引和深度的資料列群組;如果群組不存在,則會擲回例外狀況。

授權

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

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

getRowGroupControlPosition()

傳回工作表上所有資料列群組的 GroupControlTogglePosition

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

// GroupControlTogglePosition.AFTER if the row grouping control toggle is shown after the
// group.
var rowGroupControlPosition = sheet.getRowGroupControlPosition();

回攻員

GroupControlTogglePosition:如果這份工作表中的群組之後顯示列分組控制項切換鈕,則為 true;否則為 false

授權

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

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

getRowGroupDepth(rowIndex)

傳回指定索引的資料列深度。

「群組深度」代表與資料列重疊的群組數量。範圍可介於 0 到 8 之間。

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

// 1 if there is a group over rows 1 through 3
var groupDepth = sheet.getRowGroupDepth(1);

參數

名稱類型說明
rowIndexInteger資料列的索引。

回攻員

Integer:指定索引的資料列群組深度。

授權

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

  • 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

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

getSlicers()

傳回工作表上篩選器控制項的陣列。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets all slicers in the spreadsheet.
const slicers = sheet.getSlicers();

// Logs the slicer titles to the console.
for (const slicer of slicers) {
  console.log(slicer.getTitle());
}

回攻員

Slicer[] — 這份工作表中的篩選器控制項清單。

授權

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

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

getTabColorObject()

取得工作表分頁顏色;如果工作表分頁沒有顏色,則傳回 null

// This example assumes there is a sheet named "Sheet1"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("Sheet1");
var color = first.getTabColorObject();

回攻員

Color:工作表分頁顏色;如果工作表分頁沒有顏色,則為 null

授權

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

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

getType()

傳回工作表的類型。

工作表的預設類型為 SheetType.GRID。含有單一內嵌物件的工作表 (例如 EmbeddedChart) 是 SheetType.OBJECT 工作表。

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

回攻員

SheetType:工作表類型。

授權

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

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

hasHiddenGridlines()

如果工作表的格線隱藏,會傳回 true;否則會傳回 false。格線預設為顯示。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Checks if the spreadsheet has hidden gridelines and logs the result to the console.
console.log(sheet.hasHiddenGridlines());

回攻員

Boolean - 如果隱藏格線,則為 true,否則則為 false

授權

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

  • 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

hideColumns(columnIndex)

在指定索引處隱藏單一資料欄。為此方法使用 1-index。

如要使用索引隱藏多個資料欄,請使用 hideColumns(columnIndex, numColumns)

如要使用範圍隱藏多個資料欄,請使用 hideColumn()

let ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getSheets()[0];
// Hides the first column
sheet.hideColumns(1);

參數

名稱類型說明
columnIndexInteger要隱藏的資料欄索引。

授權

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

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

hideColumns(columnIndex, numColumns)

隱藏從指定索引開始的一或多個連續資料欄。為此方法使用 1-index。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Hides the first three columns
sheet.hideColumns(1, 3);

參數

名稱類型說明
columnIndexInteger要隱藏的資料欄起始索引。
numColumnsInteger要隱藏的欄數。

授權

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

  • 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

hideRows(rowIndex)

隱藏指定索引的資料列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Hides the first row
sheet.hideRows(1);

參數

名稱類型說明
rowIndexInteger要隱藏的資料列索引。

授權

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

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

hideRows(rowIndex, numRows)

隱藏從指定索引開始的一或多個連續資料列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Hides the first three rows
sheet.hideRows(1, 3);

參數

名稱類型說明
rowIndexInteger要隱藏的資料列起始索引。
numRowsInteger要隱藏的列數。

授權

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

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

hideSheet()

隱藏這張工作表。如果工作表已隱藏,則不會產生任何效果。如果只在可見的工作表上呼叫此方法,就會擲回例外狀況。

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

回攻員

Sheet — 目前的工作表。

授權

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

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

insertChart(chart)

將新圖表加入這份工作表。

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

// This creates a simple bar chart from the first three rows
// of the first two columns of the spreadsheet
var chart = sheet.newChart()
    .setChartType(Charts.ChartType.BAR)
    .addRange(sheet.getRange("A1:B4"))
    .setPosition(5, 5, 0, 0)
    .setOption("title", "Dynamic Chart")
    .build();
sheet.insertChart(chart);

參數

名稱類型說明
chartEmbeddedChart要插入的圖表。

授權

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

  • 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

insertColumns(columnIndex)

在指定位置的工作表中插入空白欄。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Shifts all columns by one
sheet.insertColumns(1);

參數

名稱類型說明
columnIndexInteger指出要插入資料欄的索引。

授權

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

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

insertColumns(columnIndex, numColumns)

從指定的位置開始,在工作表中插入一或多個連續的空白欄。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Shifts all columns by three
sheet.insertColumns(1, 3);

參數

名稱類型說明
columnIndexInteger指出要插入資料欄的索引。
numColumnsInteger要插入的欄數。

授權

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

  • 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

insertImage(blobSource, column, row)

在指定的列和欄中將 BlobSource 以圖片形式插入文件中。系統會從 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)

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

insertRows(rowIndex)

在指定位置的工作表中插入空白列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Shifts all rows down by one
sheet.insertRows(1);

參數

名稱類型說明
rowIndexInteger指出應插入資料列的索引。

授權

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

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

insertRows(rowIndex, numRows)

從指定的位置開始,在工作表中插入一或多個連續的空白列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Shifts all rows down by three
sheet.insertRows(1, 3);

參數

名稱類型說明
rowIndexInteger指出應插入資料列的索引。
numRowsInteger要插入的列數。

授權

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

  • 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

insertSlicer(range, anchorRowPos, anchorColPos)

在此工作表中新增篩選器控制項。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets the range of the sheet.
const range = sheet.getRange('A1:D10');

// Inserts the slicer with a random range into the sheet.
const insertSlicers = sheet.insertSlicer(range.randomize(), 1, 10);

// Logs the insert slicer result to the console.
console.log(insertSlicers);

參數

名稱類型說明
rangeRange建立篩選器控制項篩選器的範圍。
anchorRowPosInteger篩選器控制項的頂端會固定在這個列中。
anchorColPosInteger在這個顏色中,篩選器控制項的頂部會固定於該欄。

回攻員

Slicer — 新插入的篩選器控制項。

授權

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

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

insertSlicer(range, anchorRowPos, anchorColPos, offsetX, offsetY)

在此工作表中新增篩選器控制項。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Gets the range.
const range = sheet.getRange('A1:D10');

// Inserts a slicer using the random range function.
const insertSlicers = sheet.insertSlicer(range.randomize(), 1, 10, 0, 0);

// Logs the insert slicer result to the console.
console.log(insertSlicers);

參數

名稱類型說明
rangeRange建立篩選器控制項篩選器的範圍。
anchorRowPosInteger篩選器控制項的頂端會固定在這個列中。
anchorColPosInteger在這個顏色中,篩選器控制項的頂部會固定於該欄。
offsetXInteger與儲存格角落的水平偏移 (以像素為單位)。
offsetYInteger與儲存格角落的垂直偏移 (以像素為單位)。

回攻員

Slicer — 新插入的篩選器控制項。

授權

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

  • 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

isRightToLeft()

如果這個工作表版面配置為從右到左,會傳回 true。如果工作表使用預設的從左到右版面配置,則傳回 false

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Checks if a spreadsheet is ordered from right to left and logs the result to the console.
console.log(sheet.isRightToLeft());

回攻員

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

isSheetHidden()

如果工作表目前設為隱藏,系統會傳回 true

var sheet = SpreadsheetApp.getActiveSheet();
if (sheet.isSheetHidden()) {
  // do something...
}

回攻員

Booleantrue如果工作表設為隱藏,false 則否。

授權

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

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

moveColumns(columnSpec, destinationIndex)

將指定範圍選取的資料欄移至 destinationIndex 指定的位置。columnSpec 本身不必代表要移動的整個資料欄或一組資料欄,它會選取範圍涵蓋的所有資料欄。

// The code below moves rows A-B to destination index 5.
// This results in those columns becoming columns C-D.
var sheet = SpreadsheetApp.getActiveSheet();
// Selects column A and column B to be moved.
var columnSpec = sheet.getRange("A1:B1");
sheet.moveColumns(columnSpec, 5);

參數

名稱類型說明
columnSpecRange範圍,涵蓋應移動的資料欄。
destinationIndexInteger資料欄應移動的索引。請注意,這個索引是以資料欄移動前的座標為基礎。現有資料將向右移,為移動的資料欄騰出空間,同時來源資料欄會從格線中移除。因此,資料最終的索引可能會與原先指定的索引不同。

授權

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

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

moveRows(rowSpec, destinationIndex)

將指定範圍選取的資料列移至 destinationIndex 指定的位置。rowSpec 本身不必代表要移動的整列或整組資料列,而是會選取範圍涵蓋的所有資料列。

// The code below moves rows 1-2 to destination index 5.
// This results in those rows becoming rows 3-4.
var sheet = SpreadsheetApp.getActiveSheet();
// Selects row 1 and row 2 to be moved.
var rowSpec = sheet.getRange("A1:A2");
sheet.moveRows(rowSpec, 5);

參數

名稱類型說明
rowSpecRange範圍涵蓋應移動資料列的範圍。
destinationIndexInteger資料列應移動的索引。請注意,這個索引是以資料列移動前的座標為基礎。現有資料會向下移,以便為移動的資料列騰出空間,同時來源列已從格狀檢視畫面中移除。因此,資料最終的索引可能會與原先指定的索引不同。

授權

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

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

newChart()

傳回建構工具,為這張工作表建立新圖表。

以下範例說明如何建立新圖表:

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B8");
var chartBuilder = sheet.newChart();
chartBuilder.addRange(range)
    .setChartType(Charts.ChartType.LINE)
    .setPosition(2, 2, 0, 0)
    .setOption('title', 'My Line Chart!');
sheet.insertChart(chartBuilder.build());

回攻員

EmbeddedChartBuilder:用來建立新圖表的建構工具。


protect()

建立可防止工作表編輯的物件,但具備權限的使用者除外。在指令碼實際變更工作表的編輯者清單 (透過呼叫 Protection.removeEditor(emailAddress)Protection.removeEditor(user)Protection.removeEditors(emailAddresses)Protection.addEditor(emailAddress)Protection.addEditor(user)Protection.addEditors(emailAddresses) 或為 Protection.setDomainEdit(editable) 設定新值) 之前,權限會反映試算表本身的權限,也就是說,工作表會維持未保護狀態。如果工作表已保護,這個方法會傳回代表其現有保護設定的物件。受保護的工作表可能包含未受保護的區域。

// Protect the active sheet, then remove all other users from the list of editors.
var sheet = SpreadsheetApp.getActiveSheet();
var protection = sheet.protect().setDescription('Sample protected sheet');

// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script throws an exception upon removing the group.
var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}

回攻員

Protection:代表防護設定的物件。

授權

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

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

removeChart(chart)

從父項工作表中移除圖表。

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

// This removes all the embedded charts from the spreadsheet
var charts = sheet.getCharts();
for (var i in charts) {
  sheet.removeChart(charts[i]);
}

參數

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

授權

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

  • 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

setColumnGroupControlPosition(position)

設定欄群組控制項的切換鈕位置。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
sheet.setColumnGroupControlPosition(SpreadsheetApp.GroupControlTogglePosition.AFTER);

參數

名稱類型說明
positionGroupControlTogglePosition欄群組控制項切換鈕的位置。

回攻員

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

setColumnWidths(startColumn, numColumns, width)

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

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

// Sets the first three columns to a width of 200 pixels
sheet.setColumnWidths(1, 3, 200);

參數

名稱類型說明
startColumnInteger要變更的起始資料欄位置。
numColumnsInteger要變更的欄數。
widthInteger要設定的寬度 (以像素為單位)。

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

setConditionalFormatRules(rules)

使用輸入規則取代工作表中目前所有的現有條件式格式規則。 系統會按照規則的輸入順序評估規則。

// Remove one of the existing conditional format rules.
var sheet = SpreadsheetApp.getActiveSheet();
var rules = sheet.getConditionalFormatRules();
rules.splice(1, 1); // Deletes the 2nd format rule.
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
rulesConditionalFormatRule[]新的條件式格式規則。

授權

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

  • 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

setHiddenGridlines(hideGridlines)

隱藏或顯示工作表格線。

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

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

// Hides the gridlines in the sheet.
sheet.setHiddenGridlines(true);

參數

名稱類型說明
hideGridlinesBoolean如果為 true,請隱藏這份工作表中的格線,否則顯示格線。

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

setName(name)

設定工作表名稱。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
first.setName("not first anymore");

參數

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

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

setRightToLeft(rightToLeft)

設定或取消工作表版面配置為從右到左。

// 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 a sheet by its name.
const sheet = ss.getSheetByName('Sheet1');

// Sets the sheet layout, so that the sheet is ordered from right to left.
sheet.setRightToLeft(true);

參數

名稱類型說明
rightToLeftBoolean如為 true,則工作表版面配置會設為從右到左,儲存格 A1 位於右上角。如果設為 false,工作表版面配置會設為預設的從左到右,儲存格 A1 位於左上角。

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

setRowGroupControlPosition(position)

設定工作表上「列群組控制項」切換鈕的位置。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
sheet.setRowGroupControlPosition(SpreadsheetApp.GroupControlTogglePosition.AFTER);

參數

名稱類型說明
positionGroupControlTogglePosition列群組控制項切換鈕的位置。

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

setRowHeight(rowPosition, height)

設定指定列的列高 (以像素為單位)。根據預設,列會根據儲存格內容調整列高。如果您想強制資料列達到指定高度,請使用 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

setRowHeights(startRow, numRows, height)

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

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

// Sets the first three rows to a height of 20 pixels
sheet.setRowHeights(1, 3, 20);

參數

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

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

setRowHeightsForced(startRow, numRows, height)

設定指定列的高度 (以像素為單位)。根據預設,列會根據儲存格內容調整列高。當您使用 setRowHeightsForced 時,即使儲存格內容高度高於列高度,資料列仍會強制設為指定的高度。

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

// Sets the first three rows to a height of 5 pixels.
sheet.setRowHeightsForced(1, 3, 5);

參數

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

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

setTabColor(color)

設定工作表分頁的顏色。

// This example assumes there is a sheet named "first"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("first");
first.setTabColor("ff0000"); // Set the color to red.
first.setTabColor(null); // Unset the color.

參數

名稱類型說明
colorStringCSS 標記法 (例如 '#ffffff''white') 中的顏色代碼,或 null 可重設分頁顏色。

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

setTabColorObject(color)

設定工作表分頁的顏色。

// This example assumes there is a sheet named "Sheet1"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var first = ss.getSheetByName("Sheet1");
var color = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT1)
    .build();
first.setTabColorObject(color); // Set the color to theme accent 1.
first.setTabColorObject(null); // Unset the color.

參數

名稱類型說明
colorColor要設定的工作表分頁顏色。

回攻員

Sheet — 這份工作表 (用於鏈結)。

授權

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

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

showColumns(columnIndex)

將指定索引取消隱藏欄。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Unhides the first column
sheet.showColumns(1);

參數

名稱類型說明
columnIndexInteger要取消隱藏的資料欄索引。

授權

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

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

showColumns(columnIndex, numColumns)

取消隱藏一或多個連續資料欄 (從指定索引開始)。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Unhides the first three columns
sheet.showColumns(1, 3);

參數

名稱類型說明
columnIndexInteger要取消隱藏的資料欄起始索引。
numColumnsInteger要取消隱藏的欄數。

授權

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

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

showRows(rowIndex)

取消隱藏指定索引的資料列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Unhides the first row
sheet.showRows(1);

參數

名稱類型說明
rowIndexInteger要取消隱藏的資料列索引。

授權

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

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

showRows(rowIndex, numRows)

取消隱藏一或多個連續的資料列 (從指定索引開始)。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Unhides the first three rows
sheet.showRows(1, 3);

參數

名稱類型說明
rowIndexInteger要取消隱藏的資料列起始索引。
numRowsInteger要取消隱藏的列數。

授權

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

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

showSheet()

顯示工作表。如果工作表已顯示,則不會產生任何效果。

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

回攻員

Sheet — 目前的工作表。

授權

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

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

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

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

updateChart(chart)

更新這份工作表中的圖表。

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

// This code is going to loop through all the charts and change them to
// column charts
var charts = sheet.getCharts();
for (var i in charts) {
  var chart = charts[i];
  var newChart = chart
      .modify()
      .setChartType(Charts.ChartType.COLUMN)
      .build();
  sheet.updateChart(newChart);
}

參數

名稱類型說明
chartEmbeddedChart要更新的圖表。

授權

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

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

已淘汰的方法