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

這在功能上等同於建立 (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)

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

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

參數

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

回攻員

RangeList:指定位置的範圍清單

授權

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

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

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)

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

群組深度會指出與同一資料列重疊的群組數量。範圍可介於零之間 和八

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 代表 增加工作表建立時指派的整數遞增,與工作表位置無關。這個 適用於接受 gridId 參數 (而非 Sheet 例項) 的 Range.copyFormatToRange(gridId, column, columnEnd, row, rowEnd) 等方法。

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

Logger.log(sheet.getSheetId());

回攻員

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

授權

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

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

getSheetName()

傳回工作表名稱。

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

Logger.log(sheet.getSheetName());

回攻員

String:工作表名稱

授權

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

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

getSheetValues(startRow, startColumn, numRows, numColumns)

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

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

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

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

參數

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

回攻員

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

授權

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

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

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 指數。

如要使用索引隱藏多個資料欄,請使用 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 指數。

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

回攻員

Booleantrue (從右到左);否則為 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...
}

回攻員

Boolean - 如果工作表設為隱藏,則為 true,否則為 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)

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

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

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

參數

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

授權

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

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

setFrozenRows(rows)

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

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

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

參數

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

授權

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

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

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

已淘汰的方法