Class Range

範圍

存取及修改試算表範圍。範圍可以是工作表中的單一儲存格,或工作表中的一組相鄰儲存格。

方法

方法傳回類型簡短說明
activate()Range將指定範圍設為 active range,並將範圍左上方的儲存格設為 current cell
activateAsCurrentCell()Range將指定儲存格設為 current cell
addDeveloperMetadata(key)Range將含有指定鍵的開發人員中繼資料新增至範圍。
addDeveloperMetadata(key, visibility)Range將含有指定鍵和瀏覽權限的開發人員中繼資料新增至範圍。
addDeveloperMetadata(key, value)Range將含有指定鍵和值的開發人員中繼資料新增至範圍。
addDeveloperMetadata(key, value, visibility)Range新增含有指定鍵、值和瀏覽權限的開發人員中繼資料。
applyColumnBanding()Banding將預設欄頻帶主題套用至範圍。
applyColumnBanding(bandingTheme)Banding將指定的欄寬主題主題套用至範圍。
applyColumnBanding(bandingTheme, showHeader, showFooter)Banding使用指定標頭和頁尾設定,將指定資料欄的頻帶主題套用至範圍。
applyRowBanding()Banding將預設列頻帶主題套用至範圍。
applyRowBanding(bandingTheme)Banding將指定的列寬主題主題套用至範圍。
applyRowBanding(bandingTheme, showHeader, showFooter)Banding將指定資料列列主題主題套用至指定標頭和頁尾設定的範圍。
autoFill(destination, series)void根據這個範圍內的資料填入 destinationRange
autoFillToNeighbor(series)void根據鄰近儲存格資料計算一個新範圍,並填入該範圍內的資料,然後自動填入新範圍。
breakApart()Range再次將範圍內的所有多欄儲存格切割成個別儲存格。
canEdit()Boolean決定使用者是否有權編輯範圍內的所有儲存格。
check()Range將範圍內的範圍狀態變更為「已勾選」。
clear()Range清除內容和格式。
clear(options)Range清除透過指定進階選項指定的內容、格式、資料驗證規則和/或註解範圍。
clearContent()Range清除範圍的內容,保持格式完整。
clearDataValidations()Range清除這個範圍的資料驗證規則。
clearFormat()Range清除這個範圍的格式設定。
clearNote()Range清除指定儲存格或儲存格中的附註。
collapseGroups()Range收合群組範圍內的所有群組。
copyFormatToRange(gridId, column, columnEnd, row, rowEnd)void將範圍的格式複製到指定位置。
copyFormatToRange(sheet, column, columnEnd, row, rowEnd)void將範圍的格式複製到指定位置。
copyTo(destination)void將資料從儲存格範圍複製到其他儲存格範圍。
copyTo(destination, copyPasteType, transposed)void將資料從儲存格範圍複製到其他儲存格範圍。
copyTo(destination, options)void將資料從儲存格範圍複製到其他儲存格範圍。
copyValuesToRange(gridId, column, columnEnd, row, rowEnd)void將範圍的內容複製到指定位置。
copyValuesToRange(sheet, column, columnEnd, row, rowEnd)void將範圍的內容複製到指定位置。
createDataSourcePivotTable(dataSource)DataSourcePivotTable從資料來源建立空白的資料來源資料透視表,並固定在這個範圍內的第一個儲存格。
createDataSourceTable(dataSource)DataSourceTable從資料來源建立空白資料來源資料表,並固定於此範圍的第一個儲存格。
createDeveloperMetadataFinder()DeveloperMetadataFinder傳回 DeveloperMetadataFinderApi,尋找該範圍內的開發人員中繼資料。
createFilter()Filter建立篩選器,並套用至工作表中的指定範圍。
createPivotTable(sourceData)PivotTable依據此範圍的第一個儲存格中的指定 sourceData 建立空白的資料透視表。
createTextFinder(findText)TextFinder為範圍建立文字尋找工具,藉此尋找並取代這個範圍中的文字。
deleteCells(shiftDimension)void刪除這個儲存格範圍。
expandGroups()Range展開收合的群組或控制範圍與這個範圍的交互影響。
getA1Notation()String傳回範圍 (A1 標記法) 的字串說明。
getBackground()String傳回範圍內有效儲存格的背景顏色 (例如 '#ffffff')。
getBackgroundObject()Color傳回範圍內有效儲存格的背景顏色。
getBackgroundObjects()Color[][]傳回範圍內儲存格的背景顏色。
getBackgrounds()String[][]傳回範圍內儲存格的背景顏色 (例如 '#ffffff')。
getBandings()Banding[]傳回該範圍內任何儲存格的所有頻帶。
getCell(row, column)Range傳回範圍內的特定儲存格。
getColumn()Integer傳回這個範圍的起始欄位置。
getDataRegion()Range傳回在四個基數 Direction 中展開的範圍副本,以涵蓋所有含有資料的其餘儲存格。
getDataRegion(dimension)Range如果指定維度為 Dimension.ROWS,則傳回範圍為 Direction.UPDirection.DOWN 的副本;如果維度是 Dimension.COLUMNS,則會傳回 Direction.NEXTDirection.PREVIOUS
getDataSourceFormula()DataSourceFormula傳回範圍內第一個儲存格的 DataSourceFormula,如果儲存格不含資料來源公式,則傳回 null
getDataSourceFormulas()DataSourceFormula[]傳回範圍內儲存格的 DataSourceFormula
getDataSourcePivotTables()DataSourcePivotTable[]取得所有與範圍重疊的資料來源資料透視表。
getDataSourceTables()DataSourceTable[]取得與範圍相交的所有資料來源資料表。
getDataSourceUrl()String傳回這個範圍內資料的網址,用於建立圖表和查詢。
getDataTable()DataTable傳回這個物件中的資料,並以 DataTable 的形式傳回。
getDataTable(firstRowIsHeader)DataTable傳回這個範圍內的資料,並以 DataTable 的形式傳回。
getDataValidation()DataValidation傳回範圍內有效儲存格的資料驗證規則。
getDataValidations()DataValidation[][]傳回範圍內所有儲存格的資料驗證規則。
getDeveloperMetadata()DeveloperMetadata[]取得與這個範圍相關聯的開發人員中繼資料。
getDisplayValue()String傳回範圍內有效儲存格上方顯示的值。
getDisplayValues()String[][]傳回這個範圍內值的矩形格線。
getFilter()Filter傳回這個範圍所屬的工作表上的篩選器,如果工作表上沒有任何篩選器,則傳回「null」。
getFontColorObject()Color傳回該範圍左上角的儲存格顏色。
getFontColorObjects()Color[][]傳回範圍內儲存格的字型顏色。
getFontFamilies()String[][]傳回範圍內儲存格的字型系列。
getFontFamily()String傳回該範圍左上角的儲存格系列。
getFontLine()String取得範圍左上角的儲存格樣式 ('underline''line-through''none')。
getFontLines()String[][]取得範圍內儲存格的線條樣式 ('underline''line-through''none')。
getFontSize()Integer傳回範圍左上角儲存格的字型大小。
getFontSizes()Integer[][]傳回範圍內儲存格的字型大小。
getFontStyle()String傳回範圍左上角部分的儲存格的字型樣式 ('italic''normal')。
getFontStyles()String[][]傳回範圍內儲存格的字型樣式。
getFontWeight()String傳回範圍左上角儲存格的字型粗細 (一般/粗體),
getFontWeights()String[][]傳回範圍內儲存格的字型粗細。
getFormula()String傳回範圍左上方儲存格的公式 (A1 標記),如果儲存格為空白或不含公式,則傳回空字串。
getFormulaR1C1()String傳回指定儲存格的公式 (R1C1 標記法),無則傳回 null
getFormulas()String[][]傳回範圍內儲存格的公式 (A1 標記法)。
getFormulasR1C1()String[][]傳回範圍內儲存格的公式 (R1C1 標記法)。
getGridId()Integer傳回範圍父項工作表的格線 ID。
getHeight()Integer傳回範圍的高度。
getHorizontalAlignment()String傳回儲存格左上角對齊文字的水平對齊方式 (左側/置中/圓)。
getHorizontalAlignments()String[][]傳回範圍內儲存格的水平對齊。
getLastColumn()Integer傳回結束欄位置。
getLastRow()Integer傳回結束列位置。
getMergedRanges()Range[]傳回 Range 物件陣列,該陣列代表全部位於目前範圍內,或是至少含有目前範圍內的一個儲存格。
getNextDataCell(direction)Range從指定第一欄和資料列範圍的儲存格開始,從指定方向傳回下一個儲存格,該儲存格也就是包含資料範圍的連續儲存格邊緣,或是指定該方向的試算表儲存格。
getNote()String傳回與指定範圍相關的附註。
getNotes()String[][]傳回與範圍內儲存格相關的附註。
getNumColumns()Integer傳回這個範圍內的欄數。
getNumRows()Integer傳回這個範圍內的資料列數。
getNumberFormat()String取得指定範圍左上角的數字或日期格式。
getNumberFormats()String[][]傳回範圍內儲存格的數值或日期格式。
getRichTextValue()RichTextValue傳回範圍左上方的儲存格的 Rich Media 值,如果儲存格不是文字,則傳回 null
getRichTextValues()RichTextValue[][]傳回範圍內儲存格的 RTF 格式值。
getRow()Integer傳回這個範圍的列位置。
getRowIndex()Integer傳回這個範圍的列位置。
getSheet()Sheet傳回這個範圍所屬的工作表。
getTextDirection()TextDirection傳回範圍左上方的儲存格文字方向。
getTextDirections()TextDirection[][]傳回範圍內儲存格的文字路線。
getTextRotation()TextRotation傳回該範圍左方的文字旋轉設定。
getTextRotations()TextRotation[][]傳回範圍內儲存格的文字旋轉設定。
getTextStyle()TextStyle傳回範圍左上方的儲存格文字樣式。
getTextStyles()TextStyle[][]傳回範圍內儲存格的文字樣式。
getValue()Object傳回範圍內有效儲存格的值。
getValues()Object[][]傳回這個範圍內值的矩形格線。
getVerticalAlignment()String傳回儲存格左上角的垂直對齊方式 (上方/中間/左側)。
getVerticalAlignments()String[][]傳回範圍內儲存格的垂直對齊方式。
getWidth()Integer傳回資料欄中範圍的寬度。
getWrap()Boolean傳回儲存格文字是否換行。
getWrapStrategies()WrapStrategy[][]傳回範圍內儲存格的文字換行策略。
getWrapStrategy()WrapStrategy傳回範圍左上方文字的文字包裝策略。
getWraps()Boolean[][]傳回儲存格文字是否換行。
insertCells(shiftDimension)Range將空白儲存格插入這個範圍。
insertCheckboxes()Range在範圍中的每個儲存格中插入核取方塊,勾選 true 為已勾選,false 則取消勾選。
insertCheckboxes(checkedValue)Range將核取方塊插入範圍內每個儲存格,然後根據已勾選的自訂值設定核取方塊,並將未勾選的空白字串取消勾選。
insertCheckboxes(checkedValue, uncheckedValue)Range將核取方塊插入範圍內每個儲存格,然後針對已勾選和未勾選的狀態設定自訂值。
isBlank()Boolean如果範圍總計為空白,則傳回 true
isChecked()Boolean傳回範圍內所有儲存格的核取方塊狀態是否為「已勾選」。
isEndColumnBounded()Boolean決定範圍的邊界是否與特定欄繫結。
isEndRowBounded()Boolean決定範圍的邊界是否與特定列重疊。
isPartOfMerge()Boolean如果目前範圍中的儲存格與任何合併的儲存格重疊,則傳回 true
isStartColumnBounded()Boolean決定範圍的起始結果是否與特定欄連結。
isStartRowBounded()Boolean決定範圍的起始結果是否與特定列重複。
merge()Range將範圍內的儲存格合併成一個區塊。
mergeAcross()Range將範圍內欄的儲存格合併。
mergeVertically()Range合併範圍內的儲存格。
moveTo(target)void將選取範圍中的值 (包括格式和值) 剪下並貼入目標範圍。
offset(rowOffset, columnOffset)Range傳回以特定列數和欄為負數的新範圍 (可以是負數)。
offset(rowOffset, columnOffset, numRows)Range傳回與目前範圍相關的新範圍。現有範圍是從指定列和欄的目前範圍與目前範圍的偏移,以及儲存格中的指定高度。
offset(rowOffset, columnOffset, numRows, numColumns)Range傳回與目前範圍相關的新範圍。現有範圍是從指定列和欄的目前範圍與目前範圍的偏移,以及儲存格中的指定高度和寬度。
protect()Protection建立可保護範圍的編輯權限物件 (但具備權限的使用者除外)。
randomize()Range隨機排序指定範圍內的資料列順序。
removeCheckboxes()Range從範圍中移除所有核取方塊。
removeDuplicates()Range移除這個範圍內的資料列,其中包含與前一個資料列中值為重複的值。
removeDuplicates(columnsToCompare)Range移除這個範圍內的資料列,其中包含的指定值與先前資料列重複的值。
setBackground(color)Range設定 CSS 標記法中所有範圍的背景顏色 (例如 '#ffffff''white')。
setBackgroundObject(color)Range設定範圍內所有儲存格的背景顏色。
setBackgroundObjects(color)Range設定背景顏色的矩形格線 (必須符合此範圍的維度)。
setBackgroundRGB(red, green, blue)Range使用 RGB 值 (介於 0 到 255 (含) 之間的整數) 將背景設為指定顏色。
setBackgrounds(color)Range設定背景顏色的矩形格線 (必須符合此範圍的維度)。
setBorder(top, left, bottom, right, vertical, horizontal)Range設定邊框屬性。
setBorder(top, left, bottom, right, vertical, horizontal, color, style)Range設定顏色和/或樣式的邊框屬性。
setDataValidation(rule)Range為範圍內的所有儲存格設定一項資料驗證規則。
setDataValidations(rules)Range設定範圍內所有儲存格的資料驗證規則。
setFontColor(color)Range設定 CSS 標記法中的字型顏色 (例如 '#ffffff''white')。
setFontColorObject(color)Range設定指定範圍的字型顏色。
setFontColorObjects(colors)Range設定字型顏色的矩形格線 (必須符合此範圍的維度)。
setFontColors(colors)Range設定字型顏色的矩形格線 (必須符合此範圍的維度)。
setFontFamilies(fontFamilies)Range設定字型系列的矩形格線 (必須符合此範圍的維度)。
setFontFamily(fontFamily)Range設定字型系列,例如「Њ」或「Helvetica」。
setFontLine(fontLine)Range設定指定範圍的字型行樣式 ('underline''line-through''none')。
setFontLines(fontLines)Range設定線條樣式的矩形格線 (必須符合此範圍的維度)。
setFontSize(size)Range設定字型大小,以及要使用的點大小。
setFontSizes(sizes)Range設定字型大小的矩形格線 (必須符合這個範圍的尺寸)。
setFontStyle(fontStyle)Range設定指定範圍的字型樣式 ('italic''normal')。
setFontStyles(fontStyles)Range設定字型樣式的矩形格線 (必須符合此範圍的維度)。
setFontWeight(fontWeight)Range設定指定範圍的字型粗細 (正常/粗體)。
setFontWeights(fontWeights)Range設定字型粗細的矩形格線 (必須符合這個範圍的維度)。
setFormula(formula)Range更新這個範圍的公式。
setFormulaR1C1(formula)Range更新這個範圍的公式。
setFormulas(formulas)Range設定公式的矩形格線 (必須符合此範圍的維度)。
setFormulasR1C1(formulas)Range設定公式的矩形格線 (必須符合此範圍的維度)。
setHorizontalAlignment(alignment)Range設定指定範圍 (左/中/左) 的水平 (由左至右) 對齊。
setHorizontalAlignments(alignments)Range設定水平對齊的矩形格線。
setNote(note)Range將附註設為指定值。
setNotes(notes)Range設定記事的矩形格線 (必須符合此範圍的維度)。
setNumberFormat(numberFormat)Range將數字或日期格式設為指定格式字串。
setNumberFormats(numberFormats)Range設定數字或日期格式的矩形格線 (必須符合此範圍的維度)。
setRichTextValue(value)Range設定範圍內儲存格的 RTF 格式值。
setRichTextValues(values)Range設定 RTF 格式的矩形格線。
setShowHyperlink(showHyperlink)Range設定範圍是否應顯示超連結。
setTextDirection(direction)Range設定範圍內的儲存格文字方向。
setTextDirections(directions)Range設定文字方向的矩形格線。
setTextRotation(degrees)Range設定範圍內的儲存格文字旋轉設定,
setTextRotation(rotation)Range設定範圍內的儲存格文字旋轉設定,
setTextRotations(rotations)Range設定文字旋轉的矩形格線。
setTextStyle(style)Range設定範圍內的儲存格文字樣式。
setTextStyles(styles)Range設定文字樣式的矩形格線。
setValue(value)Range設定範圍的值。
setValues(values)Range設定值的矩形格線 (必須符合這個範圍的維度)。
setVerticalAlignment(alignment)Range設定指定範圍 (頂端/中間/底部) 的指定範圍 (頂端/中間/底部)。
setVerticalAlignments(alignments)Range設定垂直對齊的矩形格線 (必須符合這個範圍的維度)。
setVerticalText(isVertical)Range設定是否要設定範圍內儲存格的文字文字。
setWrap(isWrapEnabled)Range設定指定範圍的儲存格換行碼。
setWrapStrategies(strategies)Range設定包裝策略的矩形格線。
setWrapStrategy(strategy)Range設定範圍內儲存格的文字換行策略。
setWraps(isWrapEnabled)Range設定文字換行政策的矩形格線 (必須符合這個範圍的維度)。
shiftColumnGroupDepth(delta)Range依指定金額變更範圍的欄深度。
shiftRowGroupDepth(delta)Range依指定金額變更範圍的列高深度。
sort(sortSpecObj)Range依欄和指定順序排序指定範圍的儲存格。
splitTextToColumns()void使用自動偵測的分隔符號將一段文字分割成多個欄。
splitTextToColumns(delimiter)void使用指定字串做為自訂分隔符號,將一個資料欄分割為多個資料欄。
splitTextToColumns(delimiter)void根據指定的分隔符號,將一欄分割為多個資料欄。
trimWhitespace()Range剪輯這個範圍內每個儲存格中的空白字元 (例如空格、分頁或換行),
uncheck()Range將範圍中的核取方塊狀態變更為「未勾選」。

內容詳盡的說明文件

activate()

將指定範圍設為 active range,並將範圍左上方的儲存格設為 current cell

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var range = sheet.getRange('A1:D10');
range.activate();

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

回攻

Range — 這個範圍代表鏈結。


activateAsCurrentCell()

將指定儲存格設為 current cell

如果指定儲存格出現在現有範圍中,則該範圍就會成為使用中的有效範圍,並將儲存格做為目前的儲存格。

如果指定的儲存格不存在任何現有的範圍,則系統會移除現有的選取範圍,並將儲存格轉換為目前的儲存格,以及使用中的範圍。

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

// Gets the first sheet of the spreadsheet.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

// Gets the cell B5 and sets it as the active cell.
var range = sheet.getRange('B5');
var currentCell = range.activateAsCurrentCell();

// Logs the activated cell.
console.log(currentCell.getA1Notation());

回攻

Range — 這個範圍代表鏈結。

授權

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

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

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

// Gets row 2 on the sheet.
const range = sheet.getRange('2:2');

// Adds the key 'NAME' to the developer metadata for row 2.
range.addDeveloperMetadata('NAME');

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

參數

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

回攻

Range — 這個範圍代表鏈結。

授權

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

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

// Gets row 2 on Sheet1.
const range = sheet.getRange('2:2');

// Adds the key 'NAME' and sets the developer metadata visibility to 'DOCUMENT'
// for row 2 on Sheet1.
range.addDeveloperMetadata('NAME', SpreadsheetApp.DeveloperMetadataVisibility.DOCUMENT);

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

參數

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

回攻

Range — 這個範圍代表鏈結。

授權

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

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

// Gets row 2 on the sheet.
const range = sheet.getRange('2:2');

// Adds the key 'NAME' and sets the value to 'GOOGLE' for the metadata of row 2.
range.addDeveloperMetadata('NAME', 'GOOGLE');

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

參數

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

回攻

Range — 這個範圍代表鏈結。

授權

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

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

// Gets row 2 on the sheet.
const range = sheet.getRange('2:2');

// Adds the key 'NAME', sets the value to 'GOOGLE', and sets the visibility
// to PROJECT for row 2 on the sheet.
range.addDeveloperMetadata(
  'NAME',
  'GOOGLE',
  SpreadsheetApp.DeveloperMetadataVisibility.PROJECT);

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

參數

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

回攻

Range — 這個範圍代表鏈結。

授權

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

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

applyColumnBanding()

將預設欄頻帶主題套用至範圍。根據預設,頻帶有標頭且沒有頁尾顏色。

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

// Gets row 2 on the sheet.
const range = sheet.getRange('2:2');

// Applies column banding to row 2.
const colBanding = range.applyColumnBanding();

// Gets the first banding on the sheet and logs the color of the header column.
console.log(sheet.getBandings()[0].getHeaderColumnColorObject().asRgbColor().asHexString());

// Gets the first banding on the sheet and logs the color of the second column.
console.log(sheet.getBandings()[0].getSecondColumnColorObject().asRgbColor().asHexString());

回攻

Banding — 全新錶帶。

授權

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

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

applyColumnBanding(bandingTheme)

將指定的欄寬主題主題套用至範圍。根據預設,頻帶具有標頭且沒有頁尾顏色。

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

// Gets row 2 on the sheet.
const range = sheet.getRange('2:2');

// Applies the INDIGO color banding theme to the columns in row 2.
const colBanding = range.applyColumnBanding(SpreadsheetApp.BandingTheme.INDIGO);

// Gets the first banding on the sheet and logs the color of the second column.
console.log(sheet.getBandings()[0].getSecondColumnColorObject().asRgbColor().asHexString());

參數

姓名類型說明
bandingThemeBandingTheme要套用至範圍內資料欄的色彩主題。

回攻

Banding — 全新錶帶。

授權

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

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

applyColumnBanding(bandingTheme, showHeader, showFooter)

使用指定標頭和頁尾設定,將指定資料欄的頻帶主題套用至範圍。

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

// Gets rows 12-22 on the sheet.
const range = sheet.getRange('12:22');

// Applies the BLUE color banding theme to rows 12-22.
// Sets the header visibility to false and the footer visibility to true.
const colBanding = range.applyColumnBanding(SpreadsheetApp.BandingTheme.BLUE, false, true);

// Gets the banding color and logs it to the console.
console.log(sheet.getBandings()[0].getSecondColumnColorObject().asRgbColor().asHexString());

// Gets the header color object and logs it to the console. Returns null because the header
// visibility is set to false.
console.log(sheet.getBandings()[0].getHeaderColumnColorObject());

// Gets the footer color and logs it to the console.
console.log(sheet.getBandings()[0].getFooterColumnColorObject().asRgbColor().asHexString());

參數

姓名類型說明
bandingThemeBandingTheme要套用至範圍內資料欄的色彩主題。
showHeaderBoolean如果設為 true,系統會將主題主題標題標題套用至第一欄。
showFooterBoolean如為 true,則最後一個主題主題頁尾顏色會套用至最後一欄。

回攻

Banding — 全新錶帶。

授權

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

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

applyRowBanding()

將預設列頻帶主題套用至範圍。根據預設,頻帶具有標頭且沒有頁尾顏色。

回攻

Banding — 全新錶帶。

授權

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

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

applyRowBanding(bandingTheme)

將指定的列寬主題主題套用至範圍。根據預設,頻帶具有標頭且沒有頁尾顏色。

參數

姓名類型說明
bandingThemeBandingTheme要套用至範圍內資料列的色彩主題。

回攻

Banding — 全新錶帶。

授權

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

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

applyRowBanding(bandingTheme, showHeader, showFooter)

將指定資料列列主題主題套用至指定標頭和頁尾設定的範圍。

參數

姓名類型說明
bandingThemeBandingTheme要套用至範圍內資料列的色彩主題。
showHeaderBoolean如果設為 true,系統會將第一個主題主題標題顏色套用至第一列。
showFooterBoolean如果設為 true,系統會將最後一個主題主題頁尾顏色套用至最後一列。

回攻

Banding — 全新錶帶。

授權

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

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

autoFill(destination, series)

根據這個範圍內的資料填入 destinationRange。新值也會由指定的 series 類型決定。目的地範圍必須包含這個範圍,而且只能往單一方向擴充。舉例來說,以下函式會根據 A1:A4 目前的值,透過 A1:A20 供應一系列遞增數值:

var sheet = SpreadsheetApp.getActiveSheet();

// Has values [1, 2, 3, 4].
var sourceRange = sheet.getRange("A1:A4");
// The range to fill with values.
var destination = sheet.getRange("A1:A20");

// Inserts new values in A5:A20, continuing the pattern expressed in A1:A4
sourceRange.autoFill(destination, SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);

參數

姓名類型說明
destinationRange自動填入值的範圍。目的地範圍應包含這個範圍,並只會朝一個方向 (上、下、左、右或右方) 延伸。
seriesAutoFillSeries應該用來計算新值的 AutoFill 系列類型。此系列的成效取決於來源資料類型和數量。

授權

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

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

autoFillToNeighbor(series)

根據鄰近儲存格資料計算一個新範圍,並填入該範圍內的資料,然後自動填入新範圍。這些新值也會由指定的 series 類型決定。

計算的目的地範圍會考量周圍資料,以判斷應將新值插入在哪個位置:如果資料欄要預先填入資料,且該資料欄的左側或右側資料有新的值,則新值只會延伸到最接近此資料的資料。

舉例來說,如果 A1:A20 填入一系列遞增數字,且在包含一系列日期的 B1:B4 範圍內呼叫這個方法,則新的值只會插入 B5:B20。如此一來,這些新值就會「容納」至 A 欄中含有值的儲存格。

var sheet = SpreadsheetApp.getActiveSheet();

// A1:A20 has values [1, 2, 3, ... 20].
// B1:B4 has values [1/1/2017, 1/2/2017, ...]
var sourceRange = sheet.getRange("B1:B4");

// Results in B5:B20 having values [1/5/2017, ... 1/20/2017]
sourceRange.autoFillToNeighbor(SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);

參數

姓名類型說明
seriesAutoFillSeries應該用來計算新值的 AutoFill 系列類型。此系列的成效取決於來源資料類型和數量。

授權

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

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

breakApart()

再次將範圍內的任何多欄儲存格切割成個別儲存格。

對特定範圍呼叫此函式等同於選取範圍,然後依序點選「格式」->「合併」->「取消合併」。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

canEdit()

決定使用者是否有權編輯範圍內的所有儲存格。試算表擁有者則一律可以編輯受保護的範圍和工作表。

回攻

Booleantrue 如果使用者有權編輯範圍內的所有儲存格,否則傳回 false

授權

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

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

check()

將範圍中的核取方塊狀態變更為「已勾選」。忽略該範圍中目前未設定或未勾選值的儲存格。

// Changes the state of cells which currently contain either the checked or unchecked value
// configured in the range A1:B10 to 'checked'.
var range = SpreadsheetApp.getActive().getRange('A1:B10');
range.check();

回攻

Range — 這個範圍代表鏈結。

授權

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

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

clear()

清除內容和格式。

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

var range = sheet.getRange("A1:D10");
range.clear();

回攻

Range — 這個範圍代表鏈結。

授權

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

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

clear(options)

清除透過指定進階選項指定的內容、格式、資料驗證規則和/或註解範圍。根據預設,系統會清除所有資料。

// The code below clears range C2:G8 in the active sheet, but preserves the format,
// data validation rules, and comments.
SpreadsheetApp.getActiveSheet().getRange(2, 3, 6, 5).clear({contentsOnly: true});

參數

姓名類型說明
optionsObject指定進階參數的 JavaScript 物件,如下所示。

進階參數

姓名類型說明
commentsOnlyBoolean是否只清除註解。
contentsOnlyBoolean是否只清除內容。
formatOnlyBoolean是否僅清除格式?請注意,清除格式也會清除資料驗證規則。
validationsOnlyBoolean是否只清除資料驗證規則。
skipFilteredRowsBoolean是否要清除篩除資料列。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

clearContent()

清除範圍的內容,保持格式完整。

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

var range = sheet.getRange("A1:D10");
range.clearContent();

回攻

Range — 這個範圍代表鏈結。

授權

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

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

clearDataValidations()

清除這個範圍的資料驗證規則。

// Clear the data validation rules for cells A1:B5.
var range = SpreadsheetApp.getActive().getRange('A1:B5');
range.clearDataValidations();

回攻

Range — 這個範圍代表鏈結。

授權

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

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

clearFormat()

清除這個範圍的格式設定。

這會清除範圍內儲存格或儲存格的文字格式,但不會重設任何數字格式規則。

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

var range = sheet.getRange("A1:D10");
range.clearFormat();

回攻

Range — 這個範圍代表鏈結。

授權

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

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

clearNote()

清除指定儲存格或儲存格中的附註。

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

var range = sheet.getRange("A1:D10");
range.clearNote();

回攻

Range — 這個範圍代表鏈結。

授權

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

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

collapseGroups()

收合群組範圍內的所有群組。如果該範圍內沒有群組,則系統會收合在範圍內的部分最大展開群組。

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

// All row and column groups within the range are collapsed.
range.collapseGroups();

回攻

Range — 這個範圍代表鏈結。

授權

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

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

copyFormatToRange(gridId, column, columnEnd, row, rowEnd)

將範圍的格式複製到指定位置。如果目的地大於或等於來源範圍,來源會重複或截斷。請注意,這個方法只會複製格式。

如需 GridId 參數的詳細說明,請參閱 getGridId()

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

var range = source.getRange("B2:D4");

// This copies the formatting in B2:D4 in the source sheet to
// D4:F6 in the sheet with gridId 1555299895. Note that you can get the gridId
// of a sheet by calling sheet.getSheetId() or range.getGridId().
range.copyFormatToRange(1555299895, 4, 6, 4, 6);

參數

姓名類型說明
gridIdInteger試算表中工作表的不重複 ID (無論位置為何)。
columnInteger目標範圍的第一欄。
columnEndInteger目標範圍的結束欄。
rowInteger目標範圍的起始列。
rowEndInteger目標範圍的結束列。

授權

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

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

另請參閱


copyFormatToRange(sheet, column, columnEnd, row, rowEnd)

將範圍的格式複製到指定位置。如果目的地大於或等於來源範圍,來源會重複或截斷。請注意,這個方法只會複製格式。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheets()[0];
var destination = ss.getSheets()[1];

var range = source.getRange("B2:D4");

// This copies the formatting in B2:D4 in the source sheet to
// D4:F6 in the second sheet
range.copyFormatToRange(destination, 4, 6, 4, 6);

參數

姓名類型說明
sheetSheet目標工作表。
columnInteger目標範圍的第一欄。
columnEndInteger目標範圍的結束欄。
rowInteger目標範圍的起始列。
rowEndInteger目標範圍的結束列。

授權

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

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

copyTo(destination)

將資料從儲存格範圍複製到其他儲存格範圍。系統會複製值和格式。

// The code below copies the first 5 columns over to the 6th column.
var sheet = SpreadsheetApp.getActiveSheet();
var rangeToCopy = sheet.getRange(1, 1, sheet.getMaxRows(), 5);
rangeToCopy.copyTo(sheet.getRange(1, 6));

參數

姓名類型說明
destinationRange要複製的目的地範圍。只有左上方儲存格的位置才相關。

授權

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

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

copyTo(destination, copyPasteType, transposed)

將資料從某儲存格範圍複製到另一個儲存格範圍。

// The code below copies only the values of the first 5 columns over to the 6th column.
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange("A:E").copyTo(sheet.getRange("F1"), SpreadsheetApp.CopyPasteType.PASTE_VALUES,
false);

參數

姓名類型說明
destinationRange要複製的目的地範圍。只有左上方儲存格的位置才相關。
copyPasteTypeCopyPasteType指定範圍內容貼到目的地的類型。
transposedBoolean是否應將範圍貼在音譯方向上。

授權

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

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

copyTo(destination, options)

將資料從儲存格範圍複製到其他儲存格範圍。根據預設,系統會複製值和格式,但您可以使用進階引數覆寫這個值。

// The code below copies only the values of the first 5 columns over to the 6th column.
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange("A:E").copyTo(sheet.getRange("F1"), {contentsOnly:true});

參數

姓名類型說明
destinationRange要複製的目的地範圍。只有左上方儲存格的位置才相關。
optionsObject指定進階參數的 JavaScript 物件,如下所示。

進階參數

姓名類型說明
formatOnlyBoolean表示只應複製格式
contentsOnlyBoolean表示只有應複製內容

授權

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

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

copyValuesToRange(gridId, column, columnEnd, row, rowEnd)

將範圍的內容複製到指定位置。如果目的地大於來源範圍,則系統會重複來源或截斷來源。

如需 GridId 參數的詳細說明,請參閱 getGridId()

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

var range = source.getRange("B2:D4");

// This copies the data in B2:D4 in the source sheet to
// D4:F6 in the sheet with gridId 0
range.copyValuesToRange(0, 4, 6, 4, 6);

參數

姓名類型說明
gridIdInteger試算表中工作表的不重複 ID (無論位置為何)。
columnInteger目標範圍的第一欄。
columnEndInteger目標範圍的結束欄。
rowInteger目標範圍的起始列。
rowEndInteger目標範圍的結束列。

授權

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

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

另請參閱


copyValuesToRange(sheet, column, columnEnd, row, rowEnd)

將範圍的內容複製到指定位置。如果目的地大於來源範圍,則系統會重複來源或截斷來源。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheets()[0];
var destination = ss.getSheets()[1];

var range = source.getRange("B2:D4");

// This copies the data in B2:D4 in the source sheet to
// D4:F6 in the second sheet
range.copyValuesToRange(destination, 4, 6, 4, 6);

參數

姓名類型說明
sheetSheet目標工作表。
columnInteger目標範圍的第一欄。
columnEndInteger目標範圍的結束欄。
rowInteger目標範圍的起始列。
rowEndInteger目標範圍的結束列。

授權

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

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

createDataSourcePivotTable(dataSource)

從資料來源建立空白的資料來源資料透視表,並固定在這個範圍內的第一個儲存格。

本例說明如何建立及設定新的資料來源資料透視表。

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var anchorCell = spreadsheet.getSheets()[0].getRange('A1');
var dataSource = spreadsheet.getDataSources()[0];

var pivotTable = anchorCell.createDataSourcePivotTable(dataSource);
pivotTable.addRowGroup('dataColumnA');
pivotTable.addColumnGroup('dataColumnB');
pivotTable.addPivotValue('dataColumnC', SpreadsheetApp.PivotTableSummarizeFunction.SUM);
pivotTable.addFilter('dataColumnA',
                     SpreadsheetApp.newFilterCriteria().whenTextStartsWith('A').build());

參數

姓名類型說明
dataSourceDataSource用來建立資料透視表的資料來源。

回攻

DataSourcePivotTable:新建立的資料來源資料透視表。

授權

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

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

createDataSourceTable(dataSource)

從資料來源建立空白資料來源資料表,並固定於這個範圍的第一個儲存格。

本例說明如何建立及設定新的資料來源資料表。

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var anchorCell = spreadsheet.getSheets()[0].getRange('A1');
var dataSource = spreadsheet.getDataSources()[0];

var dataSourceTable = anchorCell.createDataSourceTable(dataSource);
    .addColumns('dataColumnA', 'dataColumnB', 'dataColumnC')
    .addSortSpec('dataColumnA', /* ascending= *\/ true)
    .addSortSpec('dataColumnB', /* ascending= *\/ false);

參數

姓名類型說明
dataSourceDataSource用來建立資料透視表的資料來源。

回攻

DataSourceTable:新建立的資料來源資料表。

授權

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

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

createDeveloperMetadataFinder()

傳回 DeveloperMetadataFinderApi,尋找該範圍內的開發人員中繼資料。中繼資料只有在範圍內,否則就不在範圍範圍內。例如,與資料列「3:3」相關聯的中繼資料不在「A1:D5」這個範圍內,但位於「1:5」範圍內。

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

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

// Creates a developer metadata finder to search for metadata in the scope of this range.
const developerMetaDataFinder = range.createDeveloperMetadataFinder();

// Logs information about the developer metadata finder to the console.
const developerMetaData = developerMetaDataFinder.find()[0];
console.log(developerMetaData.getKey());
console.log(developerMetaData.getValue());
console.log(developerMetaData.getVisibility().toString());

回攻

DeveloperMetadataFinder — 開發人員中繼資料搜尋工具,用於搜尋這個範圍內的中繼資料。


createFilter()

建立篩選器,並套用至工作表中的指定範圍。你無法在工作表中建立多個篩選器。如要在建立後存取及修改篩選器,請使用 getFilter()Sheet.getFilter()

let ss = SpreadsheetApp.getActiveSheet();
let range = ss.getRange("A1:C20");

// Creates a new filter and applies it to the range A1:C20 on the active sheet.
function createFilter() {
  range.createFilter();
}
// Gets the filter and applies criteria that only shows cells that aren't empty.
function getFilterAddCriteria() {
  let filter = range.getFilter();
  let criteria = SpreadsheetApp.newFilterCriteria()
    .whenCellNotEmpty()
    .build();
  filter.setColumnFilterCriteria(2, criteria);
}
這個方法可用於建立Grid工作表 (預設試算表類型) 的篩選器。 格狀工作表是未連結到資料庫的工作表。如要建立其他類型的篩選器,請參閱以下內容:

回攻

Filter:新的篩選器。

授權

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

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

createPivotTable(sourceData)

依據此範圍的第一個儲存格,從指定的 sourceData 建立空白的資料透視表。

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

// Gets cell A1 as a range in order to place the pivot table.
const range = sheet.getRange('A1');

// Gets the range of the source data for the pivot table.
const dataRange = sheet.getRange('E12:G20');

// Creates an empty pivot table from the specified source data.
const pivotTable = range.createPivotTable(dataRange);

// Logs the values from the pivot table's source data to the console.
console.log(pivotTable.getSourceDataRange().getValues());

參數

姓名類型說明
sourceDataRange用於建立資料透視表的資料。

回攻

PivotTable:新建立的 PivotTable

授權

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

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

createTextFinder(findText)

為範圍建立文字尋找工具,藉此尋找並取代這個範圍中的文字。

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

// Creates  a text finder for the range.
var textFinder = range.createTextFinder('dog');

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


deleteCells(shiftDimension)

刪除這個儲存格範圍。工作表中與現有維度相關的現有資料將轉移至刪除的範圍。

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

var range = sheet.getRange("A1:D10");
range.deleteCells(SpreadsheetApp.Dimension.COLUMNS);

參數

姓名類型說明
shiftDimensionDimension要移動現有資料的維度。

授權

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

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

expandGroups()

展開收合的群組或控制範圍與這個範圍的交互影響。控制項切換位置是指根據設定,在群組之前或之後直接顯示控制開關的索引。如果某個位置有多個群組,系統就會展開圓形群組。

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

// All row and column groups within the range are expanded.
range.expandGroups();

回攻

Range — 這個範圍代表鏈結。

授權

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

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

getA1Notation()

傳回範圍 (A1 標記法中) 的字串說明。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange(1, 1, 2, 5);

// Logs "A1:E2"
Logger.log(range.getA1Notation());

回攻

String:A1 標記法中字串的說明。

授權

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

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

getBackground()

傳回範圍內有效儲存格的背景顏色 (例如 '#ffffff')。

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

var cell = sheet.getRange("B5");
Logger.log(cell.getBackground());

回攻

String — 背景的色彩代碼。

授權

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

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

getBackgroundObject()

傳回範圍內有效儲存格的背景顏色。

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

var cell = sheet.getRange("B5");
Logger.log(cell.getBackgroundObject().asRgbColor().asHexString());

回攻

Color — 範圍內儲存格的背景顏色。

授權

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

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

getBackgroundObjects()

傳回範圍內儲存格的背景顏色。

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

var range = sheet.getRange("B5:C6");
var bgColors = range.getBackgroundObjects();
for (var i in bgColors) {
  for (var j in bgColors[i]) {
    Logger.log(bgColors[i][j].asRgbColor().asHexString());
  }
}

回攻

Color[][]:2 維背景背景顏色的陣列。

授權

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

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

getBackgrounds()

傳回範圍內儲存格的背景顏色 (例如 '#ffffff')。

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

var range = sheet.getRange("B5:C6");
var bgColors = range.getBackgrounds();
for (var i in bgColors) {
  for (var j in bgColors[i]) {
    Logger.log(bgColors[i][j]);
  }
}

回攻

String[][] — 背景的彩色雙數陣列。

授權

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

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

// Sets a range.
const range = sheet.getRange('A1:K50');

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

// Logs the second row color for each banding to the console.
for (let banding of bandings) {
  console.log(banding.getSecondRowColor());
}

回攻

Banding[] — 套用至這個範圍內任何儲存格的所有頻帶。

授權

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

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

getCell(row, column)

傳回範圍內的特定儲存格。

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

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

// The row and column here are relative to the range
// getCell(1,1) in this code returns the cell at B2
var cell = range.getCell(1, 1);
Logger.log(cell.getValue());

參數

姓名類型說明
rowInteger儲存格與範圍的相對列。
columnInteger相對於範圍的儲存格的儲存格。

回攻

Range:指定範圍中包含一個儲存格的範圍。

授權

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

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

getColumn()

傳回這個範圍的起始欄位置。

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

var range = sheet.getRange("B2:D4");
// Logs "2.0"
Logger.log(range.getColumn());

回攻

Integer:該欄的起始欄位置。

授權

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

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

getDataRegion()

傳回在四個基數 Direction 中展開的範圍副本,以涵蓋所有含有資料的其餘儲存格。如果範圍周圍的儲存格不含對角線,就會傳回範圍本身。這與在編輯器中選取範圍和輸入 Ctrl+A 類似。

// Assume the active spreadsheet is blank.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
sheet.getRange("C2").setValue(100);
sheet.getRange("B3").setValue(100);
sheet.getRange("D3").setValue(100);
sheet.getRange("C4").setValue(100);
// Logs "B2:D4"
Logger.log(sheet.getRange("C3").getDataRegion().getA1Notation());

回攻

Range:整個試算表的資料範圍或範圍。

授權

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

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

getDataRegion(dimension)

如果指定維度為 Dimension.ROWS,則傳回範圍為 Direction.UPDirection.DOWN 的副本;如果維度是 Dimension.COLUMNS,則會傳回 Direction.NEXTDirection.PREVIOUS。這個範圍的擴展依據是依資料表形式匯總範圍偵測資料。擴展範圍會涵蓋所有具有指定維度 (包括資料表邊界) 的資料,鄰近的所有儲存格。如果原始範圍周圍的指定儲存格周圍有空白儲存格,則會傳回範圍本身。這個方法與為資料欄選取範圍並輸入 Ctrl+Space 或在編輯器中的資料列中,輸入 Shift+Space 類似。

// Assume the active spreadsheet is blank.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
sheet.getRange("C2").setValue(100);
sheet.getRange("B3").setValue(100);
sheet.getRange("D3").setValue(100);
sheet.getRange("C4").setValue(100);
// Logs "C2:C4"
Logger.log(sheet.getRange("C3").getDataRegion(SpreadsheetApp.Dimension.ROWS).getA1Notation());
// Logs "B3:D3"
Logger.log(
    sheet.getRange("C3").getDataRegion(SpreadsheetApp.Dimension.COLUMNS).getA1Notation());

參數

姓名類型說明
dimensionDimension要用來擴大範圍的維度。

回攻

Range — 範圍的資料地區,或涵蓋每個資料欄或涵蓋原始範圍的資料列。

授權

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

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

getDataSourceFormula()

傳回範圍內第一個儲存格的 DataSourceFormula,如果儲存格不含資料來源公式,則傳回 null

回攻

DataSourceFormula:儲存格的 DataSourceFormula

授權

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

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

getDataSourceFormulas()

傳回範圍內儲存格的 DataSourceFormula

回攻

DataSourceFormula[]DataSourceFormula 的陣列。

授權

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

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

getDataSourcePivotTables()

取得所有與範圍重疊的資料來源資料透視表。

回攻

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

授權

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

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

getDataSourceTables()

取得與範圍相交的所有資料來源資料表。

回攻

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

授權

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

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

getDataSourceUrl()

傳回這個範圍內資料的網址,用於建立圖表和查詢。

Code.gs

function doGet() {
  var ss = SpreadsheetApp.openById('1khO6hBWTNNyvyyxvob7aoZTI9ZvlqqASNeq0e29Tw2c');
  var sheet = ss.getSheetByName('ContinentData');
  var range = sheet.getRange('A1:B8');

  var template = HtmlService.createTemplateFromFile('piechart');
  template.dataSourceUrl = range.getDataSourceUrl();
  return template.evaluate();
}

圓餅圖.html

<!DOCTYPE html>
<html>
  <head>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      // Load the Visualization API and the corechart package.
      google.charts.load('current', {'packages': ['corechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.charts.setOnLoadCallback(queryData);

      function queryData() {
        var query = new google.visualization.Query('<?= dataSourceUrl ?>');
        query.send(drawChart);
      }

      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart(response) {
        if (response.isError()) {
          alert('Error: ' + response.getMessage() + ' ' + response.getDetailedMessage());
          return;
        }
        var data = response.getDataTable();

        // Set chart options.
        var options = {
          title: 'Population by Continent',
          width: 400,
          height: 300
        };

        // Instantiate and draw the chart, passing in some options.
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <!-- Div that holds the pie chart. -->
    <div id="chart_div"></div>
  </body>
</html>

回攻

String — 這個範圍的網址,可傳遞給其他 API,例如圖表。

授權

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

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

getDataTable()

傳回這個物件中的資料,並以 DataTable 的形式傳回。

回攻

DataTable:以資料表格形式儲存的資料。


getDataTable(firstRowIsHeader)

傳回這個範圍內的資料,並以 DataTable 的形式傳回。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A1:B7");

// Calling this method with "true" sets the first line to be the title of the axes
var datatable = range.getDataTable(true);

// Note that this doesn't build an EmbeddedChart, so you can't just use
// Sheet#insertChart(). To do that, use sheet.newChart().addRange() instead.
var chart = Charts.newBarChart()
    .setDataTable(datatable)
    .setOption("title", "Your Title Here")
    .build();

參數

姓名類型說明
firstRowIsHeaderBoolean是否要將第一列視為標頭。

回攻

DataTable:以資料表格形式儲存的資料。

授權

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

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

getDataValidation()

傳回範圍內有效儲存格的資料驗證規則。如果尚未在儲存格中設定資料驗證,這個方法會傳回 null

// Log information about the data validation rule for cell A1.
var cell = SpreadsheetApp.getActive().getRange('A1');
var rule = cell.getDataValidation();
if (rule != null) {
  var criteria = rule.getCriteriaType();
  var args = rule.getCriteriaValues();
  Logger.log('The data validation rule is %s %s', criteria, args);
} else {
  Logger.log('The cell does not have a data validation rule.')
}

回攻

DataValidation:範圍內範圍內儲存格的資料驗證規則。

授權

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

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

getDataValidations()

傳回範圍內所有儲存格的資料驗證規則。如未在特定儲存格中設定資料驗證,這個方法會傳回陣列中該儲存格的位置的 null

// Change existing data validation rules that require a date in 2013 to require a date in 2014.
var oldDates = [new Date('1/1/2013'), new Date('12/31/2013')];
var newDates = [new Date('1/1/2014'), new Date('12/31/2014')];
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns());
var rules = range.getDataValidations();

for (var i = 0; i < rules.length; i++) {
  for (var j = 0; j < rules[i].length; j++) {
    var rule = rules[i][j];

    if (rule != null) {
      var criteria = rule.getCriteriaType();
      var args = rule.getCriteriaValues();

      if (criteria == SpreadsheetApp.DataValidationCriteria.DATE_BETWEEN
          && args[0].getTime() == oldDates[0].getTime()
          && args[1].getTime() == oldDates[1].getTime()) {
        // Create a builder from the existing rule, then change the dates.
        rules[i][j] = rule.copy().withCriteria(criteria, newDates).build();
      }
    }
  }
}
range.setDataValidations(rules);

回攻

DataValidation[][]:與範圍內儲存格相關聯的資料驗證規則,以 2D 格式陣列呈現。

授權

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

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

getDeveloperMetadata()

取得與這個範圍相關聯的開發人員中繼資料。

回攻

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

授權

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

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

getDisplayValue()

傳回範圍內有效儲存格上方顯示的值。這個值是 String。顯示值會考量日期、時間和貨幣格式設定格式,包括試算表的語言代碼設定所自動套用的格式。空白儲存格會傳回空字串。

回攻

String — 這個儲存格中顯示的值。

授權

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

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

getDisplayValues()

傳回這個範圍內值的矩形格線。

傳回值的兩維陣列,依列排序,然後是欄。值為 String 物件。顯示的值會考量日期、時間和貨幣格式,包括試算表的語言代碼設定所自動套用的格式。空白儲存格會以陣列中的空白字串表示。請記住,範圍索引是從 1, 1 開始,但 JavaScript 陣列則是從 [0][0] 建立索引。

// The code below gets the displayed values for the range C2:G8
// in the active spreadsheet.  Note that this is a JavaScript array.
var values = SpreadsheetApp.getActiveSheet().getRange(2, 3, 6, 4).getDisplayValues();
Logger.log(values[0][0]);

回攻

String[][]:雙維度的值陣列,

授權

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

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

getFilter()

傳回這個範圍所屬的工作表上的篩選器,如果工作表上沒有篩選器,則傳回「null」。

let ss = SpreadsheetApp.getActiveSheet();
let range = ss.getRange("A1:C20");
// Gets the existing filter on the sheet that the given range belongs to.
let filter = range.getFilter();

回攻

Filter:篩選器。

授權

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

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

getFontColorObject()

傳回該範圍左上角的字型顏色。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

Logger.log(range.getFontColorObject().asRgbColor().asHexString());

回攻

Color — 範圍內儲存格的字型顏色。

授權

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

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

getFontColorObjects()

傳回範圍內儲存格的字型顏色。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

var results = range.getFontColorObjects();

for (var i in results) {
  for (var j in results[i]) {
    Logger.log(results[i][j].asRgbColor().asHexString());
  }
}

回攻

Color[][]:與範圍內儲存格相關聯的字型顏色,以 2D 結構為準。

授權

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

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

getFontFamilies()

傳回範圍內儲存格的字型系列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

var results = range.getFontFamilies();

for (var i in results) {
  for (var j in results[i]) {
    Logger.log(results[i][j]);
  }
}

回攻

String[][] — 範圍中與儲存格相關聯的一系列字型系列。

授權

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

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

getFontFamily()

傳回該範圍左上角的字型系列。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

Logger.log(range.getFontFamily());

回攻

String — 儲存格的字型系列。

授權

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

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

getFontLine()

取得範圍左上角的儲存格樣式 ('underline''line-through''none')。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

Logger.log(range.getFontLine());

回攻

String — 字型行。

授權

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

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

getFontLines()

取得範圍內儲存格的線條樣式 ('underline''line-through''none')。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

var results = range.getFontLines();

for (var i in results) {
  for (var j in results[i]) {
    Logger.log(results[i][j]);
  }
}

回攻

String[][]:與範圍內儲存格相關聯的一系列線條,

授權

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

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

getFontSize()

傳回範圍左上角儲存格的字型大小。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

Logger.log(range.getFontSize());

回攻

Integer:點號的字型大小。

授權

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

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

getFontSizes()

傳回範圍內儲存格的字型大小。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

var results = range.getFontSizes();

for (var i in results) {
  for (var j in results[i]) {
    Logger.log(results[i][j]);
  }
}

回攻

Integer[][]:範圍中與儲存格相關的文字,以 2D 維度顯示。

授權

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

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

getFontStyle()

傳回範圍左上角部分的儲存格的字型樣式 ('italic''normal')。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

Logger.log(range.getFontStyle());

回攻

String — 儲存格文字的字型樣式。

授權

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

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

getFontStyles()

傳回範圍內儲存格的字型樣式。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

var results = range.getFontStyles();

for (var i in results) {
  for (var j in results[i]) {
    Logger.log(results[i][j]);
  }
}

回攻

String[][]:範圍中與儲存格關聯的文字樣式 2D 陣列。

授權

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

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

getFontWeight()

傳回範圍左上角儲存格的字型粗細 (一般/粗體)。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

Logger.log(range.getFontWeight());

回攻

String — 儲存格文字的字型粗細。

授權

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

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

getFontWeights()

傳回範圍內儲存格的字型粗細。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

var results = range.getFontWeights();

for (var i in results) {
  for (var j in results[i]) {
    Logger.log(results[i][j]);
  }
}

回攻

String[][]:範圍中與儲存格相關的文字粗細陣列。

授權

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

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

getFormula()

傳回範圍左上方儲存格的公式 (A1 標記),如果儲存格為空白或不含公式,則傳回空字串。

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

// This assumes you have a function in B5 that sums up
// B2:B4
var range = sheet.getRange("B5");

// Logs the calculated value and the formula
Logger.log("Calculated value: %s Formula: %s",
           range.getValue(),
           range.getFormula());

回攻

String:儲存格的公式。

授權

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

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

getFormulaR1C1()

傳回指定儲存格的公式 (R1C1 標記法),無則傳回 null

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

var range = sheet.getRange("B5");
var formula = range.getFormulaR1C1();
Logger.log(formula);

回攻

String:R1C1 標記法中的公式。

授權

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

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

getFormulas()

傳回範圍內儲存格的公式 (A1 標記法)。如果儲存格沒有公式,2D 陣列中的項目就會是空白字串。

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

var range = sheet.getRange("B5:C6");
var formulas = range.getFormulas();
for (var i in formulas) {
  for (var j in formulas[i]) {
    Logger.log(formulas[i][j]);
  }
}

回攻

String[][]:以二維公式組成的字串格式。

授權

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

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

getFormulasR1C1()

傳回範圍內儲存格的公式 (R1C1 標記法)。如果儲存格沒有公式,2D 陣列中的項目就會是 null

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

var range = sheet.getRange("B5:C6");
var formulas = range.getFormulasR1C1();
for (var i in formulas) {
  for (var j in formulas[i]) {
    Logger.log(formulas[i][j]);
  }
}

回攻

String[][]:R1C1 標記法中的 2 維公式陣列。

授權

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

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

getGridId()

傳回範圍父項工作表的格線 ID。ID 是隨機的非負整數值。

// Log the grid ID of the first sheet (by tab position) in the spreadsheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

Logger.log(range.getGridId());

回攻

Integer:父項工作表的格線 ID。

授權

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

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

getHeight()

傳回範圍的高度。

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

var range = sheet.getRange("B2:D4");
// logs 3.0
Logger.log(range.getHeight());

回攻

Integer:範圍的高度。

授權

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

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

getHorizontalAlignment()

傳回儲存格左上角文字的文字水平對齊 (靠左/置中/中心)。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

Logger.log(range.getHorizontalAlignment());

回攻

String:儲存格中的文字水平對齊方式。

授權

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

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

getHorizontalAlignments()

傳回範圍內儲存格的水平對齊。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

var results = range.getHorizontalAlignments();

for (var i in results) {
  for (var j in results[i]) {
    Logger.log(results[i][j]);
  }
}

回攻

String[][]:當前範圍內與文字對應的水平軸陣列。

授權

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

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

getLastColumn()

傳回結束欄位置。

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

var range = sheet.getRange("B2:D4");
// Logs "4.0"
Logger.log(range.getLastColumn());

回攻

Integer:該欄的結尾欄位置。

授權

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

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

getLastRow()

傳回結束列位置。

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

var range = sheet.getRange("B2:D4");
// Logs "4.0"
Logger.log(range.getLastRow());

回攻

Integer:該範圍的試算表結尾列位置。

授權

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

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

getMergedRanges()

傳回 Range 物件陣列,該陣列代表全部在目前範圍內,或是至少含有目前範圍內的一個儲存格。

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

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

var mergedRanges = range.getMergedRanges();
for (var i = 0; i < mergedRanges.length; i++) {
  Logger.log(mergedRanges[i].getA1Notation());
  Logger.log(mergedRanges[i].getDisplayValue());
}

回攻

Range[]Range 物件的陣列,代表與範圍重疊的合併儲存格。


getNextDataCell(direction)

從指定第一欄和資料列範圍的儲存格開始,從指定方向傳回下一個儲存格,該儲存格也就是包含資料範圍的連續儲存格邊緣,或是指定該方向的試算表儲存格。相當於在編輯器中輸入 Ctrl+[arrow key]

// Assume the active spreadsheet is blank.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var range = sheet.getRange("C3:E5");
// Logs "C1"
Logger.log(range.getNextDataCell(SpreadsheetApp.Direction.UP).getA1Notation());

參數

姓名類型說明
directionDirection尋找下一個資料區域邊緣儲存格的方向。

回攻

Range:資料區域邊緣儲存格,或是試算表邊緣的儲存格。

授權

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

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

getNote()

傳回與指定範圍相關的附註。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

Logger.log(range.getNote());

回攻

String:與指定儲存格相關聯的附註。

授權

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

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

getNotes()

傳回與範圍內儲存格相關的附註。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

var results = range.getNotes();

for (var i in results) {
  for (var j in results[i]) {
    Logger.log(results[i][j]);
  }
}

回攻

String[][]:與範圍內儲存格相關聯的兩個附註陣列。

授權

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

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

getNumColumns()

傳回這個範圍內的欄數。

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

var range = sheet.getRange("B2:D5");
Logger.log(range.getNumColumns());

回攻

Integer:這個範圍內的資料欄數。

授權

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

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

getNumRows()

傳回這個範圍內的資料列數。

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

var range = sheet.getRange("B2:D5");
Logger.log(range.getNumRows());

回攻

Integer:這個範圍內的資料列數。

授權

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

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

getNumberFormat()

取得指定範圍左上角的數字或日期格式。請參閱 Sheets API 說明文件,瞭解傳回的格式模式。

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

var cell = sheet.getRange("C4");
Logger.log(cell.getNumberFormat());

回攻

String:範圍的上方儲存格數字格式。

授權

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

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

getNumberFormats()

傳回範圍內儲存格的數值或日期格式。請參閱 Sheets API 說明文件,瞭解傳回的格式模式。

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

var range = sheet.getRange("B5:C6");
var formats = range.getNumberFormats();
for (var i in formats) {
  for (var j in formats[i]) {
    Logger.log(formats[i][j]);
  }
}

回攻

String[][]:以二維格式呈現的數字格式陣列,

授權

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

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

getRichTextValue()

傳回該範圍左方的 Text 值,如果儲存格不是文字,則傳回 null

// Gets the Rich Text value of cell D4.
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("D4:F6");
var richText = range.getRichTextValue();
console.log(richText.getText());

回攻

RichTextValue — 範圍中左上方的儲存格的複合式文字值,如果儲存格不是文字,則為 null

授權

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

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

getRichTextValues()

傳回範圍內儲存格的 RTF 格式值。

// Gets the Rich Text values for all cells in range B5:C6
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("B5:C6");
var values = range.getRichTextValues();

for (var i = 0; i < values.length; i++) {
  for (var j = 0; j < values[i].length; j++) {
    console.log(values[i][j].getText());
  }
}

回攻

RichTextValue[][]:2 個 RTF 格式陣列的陣列。

授權

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

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

getRow()

傳回這個範圍的列位置。與 getRowIndex() 相同。

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

var range = sheet.getRange("B2");
Logger.log(range.getRow());

回攻

Integer:範圍的資料列位置。

授權

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

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

getRowIndex()

傳回這個範圍的列位置。與 getRow() 相同。

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

var range = sheet.getRange("B2");
Logger.log(range.getRowIndex());

回攻

Integer:範圍的資料列位置。

授權

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

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

另請參閱


getSheet()

傳回這個範圍所屬的工作表。

回攻

Sheet:這個範圍所屬的工作表。

授權

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

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

getTextDirection()

傳回範圍左上方的儲存格文字方向。如果儲存格文字方向為自動偵測,則傳回 null

// Get the text direction of cell B1.
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("B1:D4");
Logger.log(range.getTextDirection());

回攻

TextDirection:範圍中左上方的儲存格文字方向。

授權

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

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

getTextDirections()

傳回範圍內儲存格的文字路線。在 2D 陣列中,系統會針對採用自動偵測功能的儲存格,null

// Get the text directions for all cells in range B5:C6
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("B5:C6");
var directions = range.getTextDirections();

for (var i = 0; i < directions.length; i++) {
  for (var j = 0; j < directions[i].length; j++) {
    Logger.log(directions[i][j]);
  }
}

回攻

TextDirection[][]:雙維文字文字陣列。

授權

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

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

getTextRotation()

傳回範圍左上方文字旋轉設定。

// Log the text rotation settings for a cell.
var sheet = SpreadsheetApp.getActiveSheet();

var cell = sheet.getRange("A1");
Logger.log(cell.getTextRotation());

回攻

TextRotation — 文字旋轉設定。

授權

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

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

getTextRotations()

傳回範圍內儲存格的文字旋轉設定。

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("B2:D4");

var results = range.getTextRotations();

for (var i in results) {
  for (var j in results[i]) {
    var rotation = results[i][j];
    Logger.log("Cell [%s, %s] has text rotation: %v", i, j, rotation);
  }
}

回攻

TextRotation[][]:當前範圍內儲存格的旋轉文字 2 維陣列。

授權

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

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

getTextStyle()

傳回範圍左上方的儲存格文字樣式。

// Get the text style of cell D4.
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("D4:F6");
var style = range.getTextStyle();
Logger.log(style);

回攻

TextStyle:範圍中左上方的儲存格的文字樣式。

授權

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

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

getTextStyles()

傳回範圍內儲存格的文字樣式。

// Get the text styles for all cells in range B5:C6
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("B5:C6");
var styles = range.getTextStyles();

for (var i = 0; i < styles.length; i++) {
  for (var j = 0; j < styles[i].length; j++) {
    Logger.log(styles[i][j]);
  }
}

回攻

TextStyle[][]:雙維文字樣式陣列。

授權

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

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

getValue()

傳回範圍內有效儲存格的值。這個值可以是 NumberBooleanDateString 類型,視儲存格的值而定。空白儲存格會傳回空字串。

回攻

Object:這個儲存格中的值,

授權

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

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

getValues()

傳回這個範圍內值的矩形格線。

這會傳回含有 2D 值的陣列,依資料列索引,然後依欄排序。這些值可以是 NumberBooleanDateString 類型,視儲存格的值而定。空白儲存格會以陣列中的空白字串表示。請注意,雖然範圍索引是從 1, 1 開始,但 JavaScript 陣列則是從 [0][0] 建立索引。

// The code below gets the values for the range C2:G8
// in the active spreadsheet.  Note that this is a JavaScript array.
var values = SpreadsheetApp.getActiveSheet().getRange(2, 3, 6, 4).getValues();
Logger.log(values[0][0]);
在網頁應用程式中,Date 值不是法律參數。如果範圍包含附帶 Date 值的儲存格,getValues() 就無法將資料傳回網頁應用程式。請將從試算表擷取的所有值轉換為支援的 JavaScript 原始物件,例如 NumberBooleanString

回攻

Object[][]:雙維度的值陣列,

授權

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

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

getVerticalAlignment()

傳回儲存格左上角的垂直對齊方式 (上方/中間/左側)。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

Logger.log(range.getVerticalAlignment());

回攻

String:儲存格中的文字垂直對齊。

授權

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

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

getVerticalAlignments()

傳回範圍內儲存格的垂直對齊方式。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

var results = range.getVerticalAlignments();

for (var i in results) {
  for (var j in results[i]) {
    Logger.log(results[i][j]);
  }
}

回攻

String[][]:範圍中與儲存格連結垂直排列的雙維陣列陣列。

授權

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

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

getWidth()

傳回資料欄中範圍的寬度。

回攻

Integer:範圍中的欄數。

授權

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

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

getWrap()

傳回儲存格文字是否換行。如要取得更精細的包裝策略,請使用 getWrapStrategy()

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

Logger.log(range.getWrap());

回攻

Boolean - 這個儲存格中的文字是否換行。

授權

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

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

getWrapStrategies()

傳回範圍內儲存格的文字換行策略。

// Get the text wrapping strategies for all cells in range B5:C6
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("B5:C6");
var strategies = range.getWrapStrategies();

for (var i = 0; i < strategies.length; i++) {
  for (var j = 0; j < strategies[i].length; j++) {
    Logger.log(strategies[i][j]);
  }
}

回攻

WrapStrategy[][] — 二維文字層包裝策略。

授權

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

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

getWrapStrategy()

傳回範圍左上方的儲存格文字換行策略。

// Get the text wrapping strategy of cell B1.
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("B1:D4");
Logger.log(range.getWrapStrategy());

回攻

WrapStrategy — 範圍內左上方的儲存格文字換行策略。

授權

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

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

getWraps()

傳回儲存格文字是否換行。如要取得更精細的包裝策略,請使用 getWrapStrategies()

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

var results = range.getVerticalAlignments();

for (var i in results) {
  for (var j in results[i]) {
    var isWrapped = results[i][j];
    if (isWrapped) {
       Logger.log("Cell [%s, %s] has wrapped text", i, j);
    }
  }
}

回攻

Boolean[][]:範圍中與儲存格連結垂直排列的雙維陣列陣列。

授權

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

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

insertCells(shiftDimension)

將空白儲存格插入這個範圍。新的儲存格會保留先前佔用此範圍的儲存格中的任何格式設定。工作表中與現有維度相關的現有資料,將移出已插入的範圍。

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

var range = sheet.getRange("A1:D10");
range.insertCells(SpreadsheetApp.Dimension.COLUMNS);

參數

姓名類型說明
shiftDimensionDimension要移動現有資料的維度。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

insertCheckboxes()

在範圍中的每個儲存格中插入核取方塊,勾選 true 為已勾選,false 則取消勾選。將範圍內的所有儲存格的值設為 false

var range = SpreadsheetApp.getActive().getRange('A1:B10');

// Inserts checkboxes into each cell in the range A1:B10 configured with 'true' for checked
// and 'false' for unchecked. Also, sets the value of each cell in the range A1:B10 to 'false'.
range.insertCheckboxes();

回攻

Range — 這個範圍代表鏈結。

授權

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

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

insertCheckboxes(checkedValue)

將核取方塊插入範圍內每個儲存格,然後根據已勾選的自訂值設定核取方塊,並將未勾選的空白字串取消勾選。將範圍內每個儲存格的值設為空白字串。

var range = SpreadsheetApp.getActive().getRange('A1:B10');

// Inserts checkboxes into each cell in the range A1:B10 configured with 'yes' for checked
// and the empty string for unchecked. Also, sets the value of each cell in the range A1:B10 to
//  the empty string.
range.insertCheckboxes('yes');

參數

姓名類型說明
checkedValueObject核取方塊資料勾選的勾選值。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

insertCheckboxes(checkedValue, uncheckedValue)

將核取方塊插入範圍內每個儲存格,然後針對已勾選和未勾選的狀態設定自訂值。將範圍內每個儲存格的值設為自訂未勾選值。

var range = SpreadsheetApp.getActive().getRange('A1:B10');

// Inserts checkboxes into each cell in the range A1:B10 configured with 'yes' for checked
// and 'no' for unchecked. Also, sets the value of each cell in the range A1:B10 to 'no'.
range.insertCheckboxes('yes', 'no');

參數

姓名類型說明
checkedValueObject核取方塊資料勾選的勾選值。
uncheckedValueObject核取方塊資料驗證的未勾選值。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

isBlank()

如果範圍一完全空白,則傳回 true

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B2:D4");

Logger.log(range.isBlank());

回攻

Boolean — 如果範圍空白,則為 true;否則為 false

授權

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

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

isChecked()

傳回範圍內所有儲存格的核取方塊狀態是否為「已勾選」。如果已勾選部分儲存格、未勾選其他儲存格,或是部分儲存格沒有核取方塊資料驗證,則傳回 null

var range = SpreadsheetApp.getActive().getRange('A1:A3');

// Inserts checkboxes and sets each cell value to 'no' in the range A1:A3.
range.insertCheckboxes('yes', 'no');

var range1 = SpreadsheetApp.getActive().getRange('A1');
range1.setValue('yes');
// Sets the value of isRange1Checked as true as it contains the checked value.
var isRange1Checked = range1.isChecked();

var range2 = SpreadsheetApp.getActive().getRange('A2');
range2.setValue('no');
// Sets the value of isRange2Checked as false as it contains the unchecked value.
var isRange2Checked = range2.isChecked();

var range3 = SpreadsheetApp.getActive().getRange('A3');
range3.setValue('random');
// Sets the value of isRange3Checked as null, as it contains an invalid checkbox value.
var isRange3Checked = range3.isChecked();

回攻

Boolean - true,如果範圍中的所有儲存格都勾選,則為 false;如果未勾選範圍內所有儲存格,則取消勾選 null,或未勾選任何核取方塊資料驗證。

授權

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

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

isEndColumnBounded()

決定範圍的邊界是否與特定欄繫結。舉例來說,針對 A1:B10B:B 範圍 (在範圍末端的資料欄繫結),這個方法會傳回 true;對於 3:7A1:5 的範圍,範圍只會限制在範圍結尾的特定列,因此這個方法會傳回 false

回攻

Boolean — 如果範圍結尾與特定欄相關聯,則為 true;否則傳回 false

授權

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

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

isEndRowBounded()

決定範圍的邊界是否與特定列重疊。舉例來說,範圍 A1:B103:7 的範圍 (範圍在列結尾時) 會回傳 true;對於 B:BA1:C 的範圍,如果範圍限定在範圍結尾的特定資料欄,此方法會傳回 false

回攻

Boolean — 如果範圍結尾是特定資料列,則為 true;否則傳回 false

授權

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

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

isPartOfMerge()

如果目前範圍內的儲存格與任何合併的儲存格重疊,則傳回 true

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

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

// True if any of the cells in A1:B3 is included in a merge.
var isPartOfMerge = range.isPartOfMerge();

回攻

Boolean — 如果範圍與任何合併的儲存格重疊,則會傳回 true,否則會傳回 false


isStartColumnBounded()

決定範圍的起始結果是否與特定欄連結。舉例來說,範圍 A1:B10B:B 的範圍 (在範圍起點時) 會繫結至欄數,因此會傳回 true;範圍:3:7 的範圍,僅限於範圍開頭的列。false

回攻

Boolean — 如果範圍的開頭是特定欄,則為 true;否則傳回 false

授權

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

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

isStartRowBounded()

決定範圍的起始結果是否與特定列重複。舉例來說,針對 A1:B103:7 範圍 (在範圍開頭時建立的資料列),此方法會傳回 true;針對 B:B 範圍,只會繫結至範圍開頭的特定資料欄。false

回攻

Boolean — 如果範圍的開頭是特定資料列,則為 true;否則傳回 false

授權

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

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

merge()

將範圍內的儲存格合併成一個區塊。

var sheet = SpreadsheetApp.getActiveSheet();

// The code below 2-dimensionally merges the cells in A1 to B3
sheet.getRange('A1:B3').merge();

回攻

Range — 這個範圍代表鏈結。

授權

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

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

mergeAcross()

將範圍內儲存格中的儲存格合併。

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

// The code below merges cells C5:E5 into one cell
var range1 = sheet.getRange("C5:E5");
range1.mergeAcross();

// The code below creates 2 horizontal cells, F5:H5 and F6:H6
var range2 = sheet.getRange("F5:H6");
range2.mergeAcross();

回攻

Range — 這個範圍代表鏈結。

授權

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

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

mergeVertically()

合併範圍內的儲存格。

var sheet = SpreadsheetApp.getActiveSheet();

// The code below vertically merges the cells in A1 to A10
sheet.getRange('A1:A10').mergeVertically();

// The code below creates 3 merged columns: B1 to B10, C1 to C10, and D1 to D10
sheet.getRange('B1:D10').mergeVertically();

回攻

Range — 這個範圍代表鏈結。

授權

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

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

moveTo(target)

將選取範圍中的值 (包括格式和值) 剪下並貼入目標範圍。

// The code below moves the first 5 columns over to the 6th column
var sheet = SpreadsheetApp.getActiveSheet()
sheet.getRange("A1:E").moveTo(sheet.getRange("F1"));

參數

姓名類型說明
targetRange要複製這個範圍的目標範圍;只有左上方儲存格的位置才相關。

授權

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

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

offset(rowOffset, columnOffset)

傳回以特定列數和欄為負數的新範圍 (可以是負數)。新範圍的大小與原始範圍相同。

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

var cell = sheet.getRange("A1");

// newCell references B2
var newCell = cell.offset(1, 1);

參數

姓名類型說明
rowOffsetInteger範圍由左至左的儲存格數;負值代表範圍從左上方儲存格開始計算的數值。
columnOffsetInteger範圍從左上方儲存格的欄數;負值代表範圍從左上方儲存格的欄。

回攻

Range — 這個範圍代表鏈結。


offset(rowOffset, columnOffset, numRows)

傳回與目前範圍相關的新範圍。現有範圍是從指定列和欄的目前範圍與目前範圍的偏移,以及儲存格中的指定高度。

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

var cell = sheet.getRange("A1");

// newCell references B2:B3
var newRange = cell.offset(1, 1, 2);

參數

姓名類型說明
rowOffsetInteger範圍由左至左的儲存格數;負值代表範圍從左上方儲存格開始計算的數值。
columnOffsetInteger範圍從左上方儲存格的欄數;負值代表範圍從左上方儲存格的欄。
numRowsInteger新範圍的資料列高度。

回攻

Range — 這個範圍代表鏈結。


offset(rowOffset, columnOffset, numRows, numColumns)

傳回與目前範圍相關的新範圍。現有範圍是從指定列和欄的目前範圍與當前範圍的偏移,以及儲存格中的指定高度和寬度。

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

var cell = sheet.getRange("A1");

// newCell references B2:C3
var newRange = cell.offset(1, 1, 2, 2);

參數

姓名類型說明
rowOffsetInteger範圍由左至左的儲存格數;負值代表範圍從左上方儲存格開始計算的數值。
columnOffsetInteger範圍從左上方儲存格的欄數;負值代表範圍從左上方儲存格的欄。
numRowsInteger新範圍的資料列高度。
numColumnsInteger新範圍欄的寬度。

回攻

Range — 這個範圍代表鏈結。


protect()

建立可保護範圍的編輯權限物件 (但具備權限的使用者除外)。除非指令碼實際變更範圍的編輯者清單 (透過呼叫 Protection.removeEditor(emailAddress)Protection.removeEditor(user)Protection.removeEditors(emailAddresses)Protection.addEditor(emailAddress)Protection.addEditor(user)Protection.addEditors(emailAddresses),或為 Protection.setDomainEdit(editable) 設定新值),否則權限會反映試算表本身的權限,但這意味著該範圍不會受到保護。如果範圍已受到保護,這個方法會建立一個與現有範圍重疊的新受保護範圍。如果儲存格受到多個保護範圍保護,只要任一範圍禁止特定使用者編輯儲存格,該使用者就無法編輯儲存格。

// Protect range A1:B10, then remove all other users from the list of editors.
var ss = SpreadsheetApp.getActive();
var range = ss.getRange('A1:B10');
var protection = range.protect().setDescription('Sample protected range');

// 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

randomize()

隨機排序指定範圍內的資料列順序。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A1:C7");

// Randomizes the range
range.randomize();

回攻

Range — 這個範圍代表鏈結。

授權

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

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

removeCheckboxes()

從範圍中移除所有核取方塊。清除每個儲存格的資料驗證,如果儲存格包含已勾選或未勾選的值,即會清除其值。

var range = SpreadsheetApp.getActive().getRange('A1:B10');

// Inserts checkboxes and sets each cell value to 'no' in the range A1:B10.
range.insertCheckboxes('yes', 'no');

var range1 = SpreadsheetApp.getActive().getRange('A1');
range1.setValue('yes');
// Removes the checkbox data validation in cell A1 and clears its value.
range1.removeCheckboxes();

var range2 = SpreadsheetApp.getActive().getRange('A2');
range2.setValue('random');
// Removes the checkbox data validation in cell A2 but does not clear its value.
range2.removeCheckboxes();

回攻

Range — 這個範圍代表鏈結。

授權

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

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

removeDuplicates()

移除這個範圍內的資料列,其中包含與前一個資料列中值為重複的值。含有相同值但字母大小寫、格式或公式的資料列都視為重複。這個方法也會移除檢視畫面中隱藏的重複資料列 (例如因篩選器而出現)。系統不會移除超出這個範圍的內容。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B1:D7");

// Remove duplicate rows in the range.
range.removeDuplicates();

回攻

Range:移除重複項目後產生的範圍。範圍移除後,每個資料列都會減少 1 的列大小。

授權

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

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

removeDuplicates(columnsToCompare)

移除這個範圍內的資料列,其中包含的指定值與先前資料列重複的值。含有相同值但字母大小寫、格式或公式的資料列都屬於重複資料列。這個方法也會移除檢視畫面中隱藏的重複資料列 (例如因篩選器而未顯示)。系統不會移除超出這個範圍的內容。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("B1:D7");

// Remove rows which have duplicate values in column B.
range.removeDuplicates([2]);

// Remove rows which have duplicate values in both columns B and D.
range.removeDuplicates([2,4]);

參數

姓名類型說明
columnsToCompareInteger[]用於分析重複值的資料欄。如未提供資料欄,系統就會分析所有資料欄來檢查是否有重複。

回攻

Range:移除重複項目後產生的範圍。範圍移除後,每個資料列都會減少 1 的列大小。

授權

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

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

setBackground(color)

設定 CSS 標記法中所有範圍的背景顏色 (例如 '#ffffff''white')。

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

var range = sheet.getRange("B2:D5");
range.setBackground("red");

參數

姓名類型說明
colorStringCSS 標記法中的顏色代碼 (例如 '#ffffff''white');null 值會重設顏色。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setBackgroundObject(color)

設定範圍內所有儲存格的背景顏色。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var bgColor = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.BACKGROUND)
    .build();

var range = sheet.getRange("B2:D5");
range.setBackgroundObject(bgColor);

參數

姓名類型說明
colorColor要設定的背景顏色;null 值會重設背景顏色。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setBackgroundObjects(color)

設定背景顏色的矩形格線 (必須符合此範圍的維度)。

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

var colorAccent1 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT1)
    .build();
var colorAccent2 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT2)
    .build();
var colorAccent3 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT3)
    .build();
var colorAccent4 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT4)
    .build();

var colors = [
  [colorAccent1, colorAccent2],
  [colorAccent3, colorAccent4]
];

var cell = sheet.getRange("B5:C6");
cell.setBackgroundObjects(colors);

參數

姓名類型說明
colorColor[][]兩維顏色的陣列;null 值會重設顏色。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setBackgroundRGB(red, green, blue)

使用 RGB 值 (包括 0 到 255 (含) 之間的整數) 將背景設為指定顏色。

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

var cell = sheet.getRange("B2");

// Sets the background to white
cell.setBackgroundRGB(255, 255, 255);

// Sets the background to red
cell.setBackgroundRGB(255, 0, 0);

參數

姓名類型說明
redIntegerRGB 標記法中的紅色值。
greenIntegerRGB 標記法中的綠色值。
blueIntegerRGB 標記法中的藍色值。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setBackgrounds(color)

設定背景顏色的矩形格線 (必須符合此範圍的維度)。顏色會以 CSS 標記法 (例如 '#ffffff''white') 表示。

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

var colors = [
  ["red", "white", "blue"],
  ["#FF0000", "#FFFFFF", "#0000FF"] // These are the hex equivalents
];

var cell = sheet.getRange("B5:D6");
cell.setBackgrounds(colors);

參數

姓名類型說明
colorString[][]CSS 標記法的 2D 陣列陣列 (例如 '#ffffff''white');null 值會重設顏色。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setBorder(top, left, bottom, right, vertical, horizontal)

設定邊框屬性。有效值為 true (開啟)、false (關閉) 和 null (不變更)。

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

var cell = sheet.getRange("B2");
// Sets borders on the top and bottom, but leaves the left and right unchanged
cell.setBorder(true, null, true, null, false, false);

參數

姓名類型說明
topBooleantrue 代錶框線,false 代表無,null 代表不變。
leftBooleantrue 代錶框線,false 代表無,null 代表不變。
bottomBooleantrue 代錶框線,false 代表無,null 代表不變。
rightBooleantrue 代錶框線,false 代表無,null 代表不變。
verticalBooleantrue 適用於內部垂直邊框,false 代表無,null 則不會變更。
horizontalBooleantrue 適用於內部水平框線,false 代表無,null 代表不變。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setBorder(top, left, bottom, right, vertical, horizontal, color, style)

設定顏色和/或樣式的邊框屬性。有效值為 true (開啟)、false (關閉) 和 null (不變)。如要設定顏色,請在 CSS 標記法中使用顏色 (例如 '#ffffff''white')。

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

var cell = sheet.getRange("B2");
// Sets borders on the top and bottom, but leaves the left and right unchanged
// Also sets the color to "red", and the border to "DASHED".
cell.setBorder(true, null, true, null, false, false, "red", SpreadsheetApp.BorderStyle.DASHED);

參數

姓名類型說明
topBooleantrue 代錶框線,false 代表無,null 代表不變。
leftBooleantrue 代錶框線,false 代表無,null 代表不變。
bottomBooleantrue 代錶框線,false 代表無,null 代表不變。
rightBooleantrue 代錶框線,false 代表無,null 代表不變。
verticalBooleantrue 適用於內部垂直邊框,false 代表無,null 則不會變更。
horizontalBooleantrue 適用於內部水平框線,false 代表無,null 代表不變。
colorStringCSS 標記法中的顏色 (例如 '#ffffff''white'),null 代表預設顏色 (黑色)。
styleBorderStyle框線的樣式,null 為預設樣式 (單體)。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setDataValidation(rule)

為範圍內的所有儲存格設定一個資料驗證規則。

// Set the data validation rule for cell A1 to require a value from B1:B10.
var cell = SpreadsheetApp.getActive().getRange('A1');
var range = SpreadsheetApp.getActive().getRange('B1:B10');
var rule = SpreadsheetApp.newDataValidation().requireValueInRange(range).build();
cell.setDataValidation(rule);

參數

姓名類型說明
ruleDataValidation設定的資料驗證規則,或點選 null 移除資料驗證。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setDataValidations(rules)

設定範圍內所有儲存格的資料驗證規則。這個方法採用資料維度的一維陣列,並依列和欄建立索引。陣列尺寸必須對應範圍維度。

// Set the data validation rules for Sheet1!A1:B5 to require a value from Sheet2!A1:A10.
var destinationRange = SpreadsheetApp.getActive().getSheetByName('Sheet1').getRange('A1:B5');
var sourceRange = SpreadsheetApp.getActive().getSheetByName('Sheet2').getRange('A1:A10');
var rule = SpreadsheetApp.newDataValidation().requireValueInRange(sourceRange).build();
var rules = destinationRange.getDataValidations();
for (var i = 0; i < rules.length; i++) {
  for (var j = 0; j < rules[i].length; j++) {
    rules[i][j] = rule;
  }
}
destinationRange.setDataValidations(rules);

參數

姓名類型說明
rulesDataValidation[][]設定資料維度的 2 維陣列;null 值會移除資料驗證。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFontColor(color)

設定 CSS 標記法中的字型顏色 (例如 '#ffffff''white')。

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

var cell = sheet.getRange("B2");
cell.setFontColor("red");

參數

姓名類型說明
colorStringCSS 標記法中的字型顏色 (例如 '#ffffff''white');null 值會重設顏色。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFontColorObject(color)

設定指定範圍的字型顏色。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var color = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.TEXT)
    .build();

var cell = sheet.getRange("B2");
cell.setFontColor(color);

參數

姓名類型說明
colorColor設定的字型顏色;null 值會重設顏色。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFontColorObjects(colors)

設定字型顏色的矩形格線 (必須符合此範圍的維度)。

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

var colorAccent1 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT1)
    .build();
var colorAccent2 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT2)
    .build();
var colorAccent3 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT3)
    .build();
var colorAccent4 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT4)
    .build();

var colors = [
  [colorAccent1, colorAccent2],
  [colorAccent3, colorAccent4]
];

var cell = sheet.getRange("B5:C6");
cell.setFontColorObjects(colors);

參數

姓名類型說明
colorsColor[][]兩維顏色的陣列;null 值會重設字型顏色。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFontColors(colors)

設定字型顏色的矩形格線 (必須符合此範圍的維度)。顏色會以 CSS 標記法 (例如 '#ffffff''white') 表示。

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

var colors = [
  ["red", "white", "blue"],
  ["#FF0000", "#FFFFFF", "#0000FF"] // These are the hex equivalents
];

var cell = sheet.getRange("B5:D6");
cell.setFontColors(colors);

參數

姓名類型說明
colorsObject[][]CSS 標記法的 2D 陣列陣列 (例如 '#ffffff''white');null 值會重設顏色。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFontFamilies(fontFamilies)

設定字型系列的矩形格線 (必須符合此範圍的維度)。字型系列範例為「Њ」或「Helvetica」。

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

var fonts = [
  ["Arial", "Helvetica", "Verdana"],
  ["Courier New", "Arial", "Helvetica]
];

var cell = sheet.getRange("B2:D3");
cell.setFontFamilies(fonts);

參數

姓名類型說明
fontFamiliesObject[][]字型系列的 2 維陣列;null 值會重設字型系列。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFontFamily(fontFamily)

設定字型系列,例如「ELL」或「Helvetica」。

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

var cell = sheet.getRange("B2");
cell.setFontFamily("Helvetica");

參數

姓名類型說明
fontFamilyString設定的字型系列;null 值會重設字型系列。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFontLine(fontLine)

設定指定範圍的字型行樣式 ('underline''line-through''none')。

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

var cell = sheet.getRange("B2");
cell.setFontLine("line-through");

參數

姓名類型說明
fontLineString字型行樣式:'underline''line-through''none'null 值會重設字型行樣式。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFontLines(fontLines)

設定線條樣式的矩形格線 (必須符合此範圍的維度)。

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

// The size of the two-dimensional array must match the size of the range.
var fontLines = [
  ["underline", "line-through", "none"]
];

var range = sheet.getRange("B2:D2");
range.setFontLines(fontLines);

參數

姓名類型說明
fontLinesObject[][]字型行樣式的 2 維陣列 ('underline''line-through''none');null 值會重設字型行樣式。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFontSize(size)

設定字型大小,以及要使用的點大小。

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

var cell = sheet.getRange("B2");
cell.setFontSize(20);

參數

姓名類型說明
sizeInteger字型大小的字型大小。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFontSizes(sizes)

設定字型大小的矩形格線 (必須符合這個範圍的尺寸)。尺寸也在某個點。

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

// The size of the two-dimensional array must match the size of the range.
var fontSizes = [
  [16, 20, 24]
];

var range = sheet.getRange("B2:D2");
range.setFontSizes(fontSizes);

參數

姓名類型說明
sizesObject[][]二維大小陣列。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFontStyle(fontStyle)

設定指定範圍的字型樣式 ('italic''normal')。

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

var cell = sheet.getRange("B2");
cell.setFontStyle("italic");

參數

姓名類型說明
fontStyleString字型樣式:'italic''normal'null 值會重設字型樣式。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFontStyles(fontStyles)

設定字型樣式的矩形格線 (必須符合此範圍的維度)。

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

// The size of the two-dimensional array must match the size of the range.
var fontStyles = [
  ["italic", "normal"]
];

var range = sheet.getRange("B2:C2");
range.setFontStyles(fontStyles);

參數

姓名類型說明
fontStylesObject[][]字型樣式的 2 維陣列,'italic''normal'null 值會重設字型樣式。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFontWeight(fontWeight)

設定指定範圍的字型粗細 (正常/粗體)。

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

var cell = sheet.getRange("B2");
cell.setFontWeight("bold");

參數

姓名類型說明
fontWeightString字型粗細 'bold''normal'null 值會重設字型粗細。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFontWeights(fontWeights)

設定字型粗細的矩形格線 (必須符合這個範圍的維度)。字型粗細為「粗體」。

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

// The size of the two-dimensional array must match the size of the range.
var fontStyles = [
  [ "bold", "bold", "normal" ]
];

var range = sheet.getRange("B2:D2");
range.setFontWeights(fontStyles);

參數

姓名類型說明
fontWeightsObject[][]字型粗細的 2 維陣列,'bold''normal'null 值會重設字型粗細。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFormula(formula)

更新這個範圍的公式。給定的公式必須採用 A1 標記法。

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

var cell = sheet.getRange("B5");
cell.setFormula("=SUM(B3:B4)");

參數

姓名類型說明
formulaString字串,代表要為儲存格設定的公式。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFormulaR1C1(formula)

更新這個範圍的公式。指定公式必須採用 R1C1 標記法。

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

var cell = sheet.getRange("B5");
// This sets the formula to be the sum of the 3 rows above B5
cell.setFormulaR1C1("=SUM(R[-3]C[0]:R[-1]C[0])");

參數

姓名類型說明
formulaString字串公式。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFormulas(formulas)

設定公式的矩形格線 (必須符合此範圍的維度)。給定公式必須採用 A1 標記法。這個方法會使用 2D 公式陣列,依列排序,然後是欄。陣列尺寸必須對應範圍維度。

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

// This sets the formulas to be a row of sums, followed by a row of averages right below.
// The size of the two-dimensional array must match the size of the range.
var formulas = [
  ["=SUM(B2:B4)", "=SUM(C2:C4)", "=SUM(D2:D4)"],
  ["=AVERAGE(B2:B4)", "=AVERAGE(C2:C4)", "=AVERAGE(D2:D4)"]
];

var cell = sheet.getRange("B5:D6");
cell.setFormulas(formulas);

參數

姓名類型說明
formulasString[][]公式的二維字串陣列。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setFormulasR1C1(formulas)

設定公式的矩形格線 (必須符合此範圍的維度)。給定公式必須採用 R1C1 標記法。

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

// This creates formulas for a row of sums, followed by a row of averages.
var sumOfRowsAbove = "=SUM(R[-3]C[0]:R[-1]C[0])";
var averageOfRowsAbove = "=AVERAGE(R[-4]C[0]:R[-2]C[0])";

// The size of the two-dimensional array must match the size of the range.
var formulas = [
  [sumOfRowsAbove, sumOfRowsAbove, sumOfRowsAbove],
  [averageOfRowsAbove, averageOfRowsAbove, averageOfRowsAbove]
];

var cell = sheet.getRange("B5:D6");
// This sets the formula to be the sum of the 3 rows above B5.
cell.setFormulasR1C1(formulas);

參數

姓名類型說明
formulasString[][]R1C1 格式的 2D 公式陣列。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setHorizontalAlignment(alignment)

設定指定範圍 (左/中/左) 的水平 (由左至右) 對齊。

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

var cell = sheet.getRange("B2");
cell.setHorizontalAlignment("center");

參數

姓名類型說明
alignmentString對齊方式:'left''center''normal'null 值會重設對齊方式。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setHorizontalAlignments(alignments)

設定水平對齊的矩形格線。詳情請參閱 setHorizontalAlignment(alignment)

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

// The size of the two-dimensional array must match the size of the range.
var horizontalAlignments = [
  [ "left", "right", "center" ]
];

var range = sheet.getRange("B2:D2");
range.setHorizontalAlignments(horizontalAlignments);

參數

姓名類型說明
alignmentsObject[][]對齊的 2 維陣列,可選擇 'left''center''normal'null 值則會重設對齊方式。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

另請參閱


setNote(note)

將附註設為指定值。

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

var cell = sheet.getRange("B2");
cell.setNote("This is a note");

參數

姓名類型說明
noteString用來設定範圍的附註值;null 值會移除附註。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setNotes(notes)

設定記事的矩形格線 (必須符合此範圍的維度)。

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

// The size of the two-dimensional array must match the size of the range.
var notes = [
  ["it goes", "like this", "the fourth, the fifth"],
  ["the minor fall", "and the", "major lift"]
];

var cell = sheet.getRange("B2:D3");
cell.setNotes(notes)

參數

姓名類型說明
notesObject[][]兩維註解結構;null 值會移除附註。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

另請參閱


setNumberFormat(numberFormat)

將數字或日期格式設為指定格式字串。如需可接受的格式模式,請參閱 Sheets API 說明文件

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

var cell = sheet.getRange("B2");
// Always show 3 decimal points
cell.setNumberFormat("0.000");

參數

姓名類型說明
numberFormatString數字格式字串。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setNumberFormats(numberFormats)

設定數字或日期格式的矩形格線 (必須符合此範圍的維度)。這些值均符合 Sheets API 說明文件中的格式模式字串。

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

// The size of the two-dimensional array must match the size of the range.
var formats = [
  [ "0.000", "0,000,000", "$0.00" ]
];

var range = sheet.getRange("B2:D2");
range.setNumberFormats(formats);

參數

姓名類型說明
numberFormatsObject[][]二維數字格式陣列。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setRichTextValue(value)

設定範圍內儲存格的 RTF 格式值。

// Sets all cells in range B2:D4 to have the text "Hello world", with "Hello" bolded.
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("B2:D4");
var bold = SpreadsheetApp.newTextStyle()
    .setBold(true)
    .build();
var richText = SpreadsheetApp.newRichTextValue()
    .setText("Hello world")
    .setTextStyle(0, 5, bold)
    .build();
range.setRichTextValue(richText);

參數

姓名類型說明
valueRichTextValue所需的 RTF 格式值。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setRichTextValues(values)

設定 RTF 格式的矩形格線。

// Sets the cells in range A1:A2 to have Rich Text values.
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:A2");
var bold = SpreadsheetApp.newTextStyle()
    .setBold(true)
    .build();
var italic = SpreadsheetApp.newTextStyle()
    .setItalic(true)
    .build();
var richTextA1 = SpreadsheetApp.newRichTextValue()
    .setText("This cell is bold")
    .setTextStyle(bold)
    .build();
var richTextA2 = SpreadsheetApp.newRichTextValue()
    .setText("bold words, italic words")
    .setTextStyle(0, 11, bold)
    .setTextStyle(12, 24, italic)
    .build();
range.setRichTextValues([[richTextA1], [richTextA2]]);

參數

姓名類型說明
valuesRichTextValue[][]所需的 RTF 格式值。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

設定範圍是否應顯示超連結。

參數

姓名類型說明
showHyperlinkBoolean是否顯示超連結。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setTextDirection(direction)

設定範圍內的儲存格文字方向。如果指定方向為 null,系統會推論方向然後設定。

// Sets right-to-left text direction for the range.
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("B5:C6");
range.setTextDirection(SpreadsheetApp.TextDirection.RIGHT_TO_LEFT);

參數

姓名類型說明
directionTextDirection所需的文字方向;如果 null,則會在設定前推測方向。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setTextDirections(directions)

設定文字方向的矩形格線。如果指定方向為 null,系統會推斷該方向,然後進行設定。

// Copies all of the text directions from range A1:B2 over to range C5:D6.
var sheet = SpreadsheetApp.getActiveSheet();
var range1 = sheet.getRange("A1:B2");
var range2 = sheet.getRange("C5:D6");

range2.setTextRotations(range1.getTextDirections());

參數

姓名類型說明
directionsTextDirection[][]所需的文字方向;如果指定方向為 null,則會在設定前推論。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setTextRotation(degrees)

設定範圍內的儲存格文字旋轉設定,輸入內容對應標準文字方向和所需方向的角度。輸入 0 表示文字已設為標準方向。

從左到右文字方向時,正面角度會逆時針移動,而從右到左的方向則是順時針方向。

// Sets all cell's in range B2:D4 to have text rotated up 45 degrees.
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("B2:D4");

range.setTextRotation(45);

參數

姓名類型說明
degreesInteger標準螢幕方向和所需螢幕方向之間的所需角度。文字從左到右時,正角會以逆時針方向移動。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setTextRotation(rotation)

設定範圍內的儲存格文字旋轉設定。

// Sets all cell's in range B2:D4 to have the same text rotation settings as cell A1.
var sheet = SpreadsheetApp.getActiveSheet();

var rotation = sheet.getRange("A1").getTextRotation();

sheet.getRange("B2:D4").setTextRotation(rotation);

參數

姓名類型說明
rotationTextRotation所需文字旋轉設定。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setTextRotations(rotations)

設定文字旋轉的矩形格線。

// Copies all of the text rotations from range A1:B2 over to range C5:D6.
var sheet = SpreadsheetApp.getActiveSheet();
var range1 = sheet.getRange("A1:B2");
var range2 = sheet.getRange("C5:D6");

range2.setTextRotations(range1.getTextRotations());

參數

姓名類型說明
rotationsTextRotation[][]所需文字旋轉設定。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setTextStyle(style)

設定範圍內的儲存格文字樣式。

// Sets the cells in range C5:D6 to have underlined size 15 font.
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("C5:D6");
var style = SpreadsheetApp.newTextStyle()
    .setFontSize(15)
    .setUnderline(true)
    .build();
range.setTextStyle(style);

參數

姓名類型說明
styleTextStyle所需文字樣式。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setTextStyles(styles)

設定文字樣式的矩形格線。

// Sets text styles for cells in range A1:B2
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B2");
var bold = SpreadsheetApp.newTextStyle()
    .setBold(true)
    .build();
var otherStyle = SpreadsheetApp.newTextStyle()
    .setBold(true)
    .setUnderline(true)
    .setItalic(true)
    .setForegroundColor("#335522")
    .setFontSize(44)
    .build();
range.setTextStyles([[bold, otherStyle], [otherStyle, bold]]);

參數

姓名類型說明
stylesTextStyle[][]所需文字樣式。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setValue(value)

設定範圍的值。值可以是數字、字串、布林值或日期。如果開頭為 '=',則會解讀為公式。

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

var cell = sheet.getRange("B2");
cell.setValue(100);

參數

姓名類型說明
valueObject範圍的值。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setValues(values)

設定值的矩形格線 (必須符合這個範圍的維度)。

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

// The size of the two-dimensional array must match the size of the range.
var values = [
  [ "2.000", "1,000,000", "$2.99" ]
];

var range = sheet.getRange("B2:D2");
range.setValues(values);

參數

姓名類型說明
valuesObject[][]二維數值的陣列。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setVerticalAlignment(alignment)

設定指定範圍 (頂端/中間/底部) 的指定範圍 (上方/中間/中間)。

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

var cell = sheet.getRange("B2");
cell.setVerticalAlignment("middle");

參數

姓名類型說明
alignmentString對齊方式:'top''middle''bottom'null 值會重設對齊方式。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setVerticalAlignments(alignments)

設定垂直對齊的矩形格線 (必須符合這個範圍的維度)。

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

// The size of the two-dimensional array must match the size of the range.
var alignments = [
  [ "top", "middle", "bottom" ]
];

var range = sheet.getRange("B2:D2");
range.setVerticalAlignments(alignments);

參數

姓名類型說明
alignmentsObject[][]'top''middle''bottom' 的雙維對齊陣列;null 值會重設對齊方式。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

另請參閱


setVerticalText(isVertical)

設定是否要設定範圍內儲存格的文字文字。如果文字垂直堆疊,系統會忽略度數文字旋轉設定。

// Sets all cell's in range B2:D4 to have vertically stacked text.
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("B2:D4");

range.setVerticalText(true);

參數

姓名類型說明
isVerticalBoolean是否要堆疊文字。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setWrap(isWrapEnabled)

設定指定範圍的儲存格換行碼。

已啟用換行功能 (預設) 的儲存格會調整大小,以便顯示完整內容。含有換行字元的儲存格會在儲存格中盡可能顯示,不會調整大小或分為多行。

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

var cell = sheet.getRange("B2");
cell.setWrap(true);

參數

姓名類型說明
isWrapEnabledBoolean是否要換行。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setWrapStrategies(strategies)

設定包裝策略的矩形格線。

// Copies all of the wrap strategies from range A1:B2 over to range C5:D6.
var sheet = SpreadsheetApp.getActiveSheet();
var range1 = sheet.getRange("A1:B2");
var range2 = sheet.getRange("C5:D6");

range2.setWrapStrategies(range1.getWrapStrategies());

參數

姓名類型說明
strategiesWrapStrategy[][]想要的包裝策略。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setWrapStrategy(strategy)

設定範圍內儲存格的文字換行策略。

// Sets all cells in range B2:D4 to use the clip wrap strategy.
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("B2:D4");

range.setWrapStrategy(SpreadsheetApp.WrapStrategy.CLIP);

參數

姓名類型說明
strategyWrapStrategy想要的包裝策略。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

setWraps(isWrapEnabled)

設定文字換行政策的矩形格線 (必須符合這個範圍的維度)。已啟用換行的儲存格 (預設) 會調整大小,以便顯示完整內容。如果儲存格已停用,自動換行功能會盡可能在儲存格中顯示,但不會調整大小或執行多行。

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

// The size of the two-dimensional array must match the size of the range.
var wraps = [
  [ true, true, false ]
];

var range = sheet.getRange("B2:D2");
range.setWraps(wraps);

參數

姓名類型說明
isWrapEnabledObject[][]二維包裝陣列,用於判斷是否要將文字包裝成儲存格。

回攻

Range — 這個範圍代表鏈結。

授權

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

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

另請參閱


shiftColumnGroupDepth(delta)

依指定金額變更範圍的欄深度。

這會影響建立、修改或刪除與範圍重疊的群組。偽陽性時,系統會建立和/或修改群組;如果是負數,群組就會遭到刪除和/或修改。

將群組深度降低至低於 8 或以上時,不會產生任何影響。

如果 column group control positionBEFORE,在嘗試移動第一個資料列的深度時,系統會擲回錯誤。

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

// The column grouping depth is increased by 1.
range.shiftColumnGroupDepth(1);

// The column grouping depth is decreased by 1.
range.shiftColumnGroupDepth(-1);

參數

姓名類型說明
deltaInteger變更這個範圍欄群組深度的金額。

回攻

Range — 這個範圍代表鏈結。

擲回

Error — 嘗試控制控制項位置為 GroupControlTogglePosition.BEFORE 時,移動第一欄的深度

授權

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

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

shiftRowGroupDepth(delta)

依指定金額變更範圍的列高深度。

這會影響建立、修改或刪除與範圍重疊的群組。偽陽性時,系統會建立和/或修改群組;如果是負數,群組就會遭到刪除和/或修改。

將群組深度降低至低於 8 或以上時,不會產生任何影響。

如果 row group control positionBEFORE,在嘗試移動第一個資料列的深度時,系統會擲回錯誤。

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

// The row grouping depth is increased by 1.
range.shiftRowGroupDepth(1);

// The row grouping depth is decreased by 1.
range.shiftRowGroupDepth(-1);

參數

姓名類型說明
deltaInteger變更這個範圍列群組深度深度的金額。

回攻

Range — 這個範圍代表鏈結。

擲回

Error — 嘗試控制控制項位置為 GroupControlTogglePosition.BEFORE 時,移動第一列的深度

授權

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

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

sort(sortSpecObj)

依欄和指定順序排序指定範圍中的儲存格。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A1:C7");

// Sorts by the values in the first column (A)
range.sort(1);

// Sorts by the values in the second column (B)
range.sort(2);

// Sorts descending by column B
range.sort({column: 2, ascending: false});

// Sorts descending by column B, then ascending by column A
// Note the use of an array
range.sort([{column: 2, ascending: false}, {column: 1, ascending: true}]);

// For rows that are sorted in ascending order, the "ascending" parameter is
// optional, and just an integer with the column can be used instead. Note that
// in general, keeping the sort specification consistent results in more readable
// code. You can express the earlier sort as:
range.sort([{column: 2, ascending: false}, 1]);

// Alternatively, if you want all columns to be in ascending order, you can use
// the following (this makes column 2 ascending)
range.sort([2, 1]);
// ... which is equivalent to
range.sort([{column: 2, ascending: true}, {column: 1, ascending: true}]);

參數

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

回攻

Range — 這個範圍代表鏈結。

授權

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

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

splitTextToColumns()

使用自動偵測的分隔符號將一段文字分割成多個欄。

// A1:A3 has the following values:
//           A                  B                 C
// 1 |one,one,one      |                 |                 |
// 2 |two,two,two      |                 |                 |
// 3 |three,three,three|                 |                 |

var range = SpreadsheetApp.getActiveSheet().getRange("A1:A3");
range.splitTextToColumns();

// Result after spliting the text to columns:
//           A                  B                 C
// 1 |one              |one              |one              |
// 2 |two              |two              |two              |
// 3 |three            |three            |three            |

授權

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

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

splitTextToColumns(delimiter)

使用指定字串做為自訂分隔符號,將單一文字欄分割為多個資料欄。

// A1:A3 has the following values:
//           A                  B                 C
// 1 |one#one#one      |                 |                 |
// 2 |two#two#two      |                 |                 |
// 3 |three#three#three|                 |                 |

var range = SpreadsheetApp.getActiveSheet().getRange("A1:A3");
range.splitTextToColumns('#');

// Result after spliting the text to columns:
//           A                  B                 C
// 1 |one              |one              |one              |
// 2 |two              |two              |two              |
// 3 |three            |three            |three            |

參數

姓名類型說明
delimiterString做為分割目標的自訂分隔符號。

授權

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

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

splitTextToColumns(delimiter)

依據指定的分隔符號,將一欄分割為多個資料欄。

// A1:A3 has the following values:
//           A                  B                 C
// 1 |one;one;one      |                 |                 |
// 2 |two;two;two      |                 |                 |
// 3 |three;three;three|                 |                 |

var range = SpreadsheetApp.getActiveSheet().getRange("A1:A3");
range.splitTextToColumns(SpreadsheetApp.TextToColumnsDelimiter.SEMICOLON);

// Result after spliting the text to columns:
//           A                  B                 C
// 1 |one              |one              |one              |
// 2 |two              |two              |two              |
// 3 |three            |three            |three            |

參數

姓名類型說明
delimiterTextToColumnsDelimiter做為分割目標的預設分隔符號。

授權

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

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

trimWhitespace()

剪輯這個範圍內每個儲存格中的空白字元 (例如空格、分頁或換行),移除每個儲存格文字開頭和結尾的所有空白字元,並將剩餘空白字元的所有子元素縮減成單一空格。

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var range = sheet.getRange('A1:A4');
range.activate();
range.setValues(
    [' preceding space', 'following space ', 'two  middle  spaces', '   =SUM(1,2)'])

range.trimWhitespace();

var values = range.getValues();
// Values are ['preceding space', 'following space', 'two middle spaces', '=SUM(1,2)']

回攻

Range — 這個範圍代表鏈結。

授權

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

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

uncheck()

將範圍中的核取方塊狀態變更為「未勾選」狀態。忽略該範圍中目前未設定或未勾選值的儲存格。

// Changes the state of cells which currently contain either the checked or unchecked value
// configured in the range A1:B10 to 'unchecked'.
var range = SpreadsheetApp.getActive().getRange('A1:B10');
range.uncheck();

回攻

Range — 這個範圍代表鏈結。

授權

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

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

已淘汰的方法