Class SpreadsheetApp

SpreadsheetApp

Google スプレッドシート ファイルにアクセスして作成する。このクラスは、スプレッドシート サービスの親クラスです。

プロパティ

プロパティ種類説明
AutoFillSeriesAutoFillSeries自動入力される値の計算に使用される系列を列挙します。
BandingThemeBandingTheme考えられるバンディングテーマを列挙します。
BooleanCriteriaBooleanCriteria条件付き書式のブール値条件の列挙です。
BorderStyleBorderStyleRange に枠線を設定するための有効なスタイルの列挙です。
ColorTypeColorType使用可能な色タイプの列挙です。
CopyPasteTypeCopyPasteType可能な貼り付けタイプの列挙です。
DataExecutionErrorCodeDataExecutionErrorCodeデータ実行の可能性のあるエラーコードを列挙します。
DataExecutionStateDataExecutionState可能なデータ実行状態の列挙。
DataSourceParameterTypeDataSourceParameterType有効なデータソース パラメータの型の列挙型。
DataSourceRefreshScopeDataSourceRefreshScopeデータソースの更新スコープを列挙します。
DataSourceTypeDataSourceType有効なデータソース タイプの列挙です。
DataValidationCriteriaDataValidationCriteria範囲に対して設定できるデータ検証基準を表す列挙型。
DateTimeGroupingRuleTypeDateTimeGroupingRuleType日時のグループ化ルールの列挙。
DeveloperMetadataLocationTypeDeveloperMetadataLocationTypeデベロッパー メタデータの位置情報タイプを列挙します。
DeveloperMetadataVisibilityDeveloperMetadataVisibilityデベロッパー メタデータの公開設定を列挙します。
DimensionDimensionスプレッドシートの可能なディメンションを列挙します。
DirectionDirection矢印キーを使用してスプレッドシート内で移動できる方向を列挙したもの。
FrequencyTypeFrequencyType有効な頻度の型の列挙です。
GroupControlTogglePositionGroupControlTogglePositionグループ コントロールの切り替えの位置を列挙します。
InterpolationTypeInterpolationType条件付き書式のグラデーション補間タイプの列挙。
PivotTableSummarizeFunctionPivotTableSummarizeFunctionピボット テーブル内の値の集計に使用できる関数の列挙です。
PivotValueDisplayTypePivotValueDisplayTypeピボット値の表示方法を列挙したものです。
ProtectionTypeProtectionType編集から保護できるスプレッドシートの部分を表す列挙型。
RecalculationIntervalRecalculationIntervalスプレッドシートの再計算に使用できる間隔を列挙します。
RelativeDateRelativeDate日付ベースの BooleanCriteria で使用する値を計算するための相対日付オプションの列挙。
SheetTypeSheetTypeスプレッドシートに存在することができるさまざまな種類のシートを列挙したものです。
SortOrderSortOrder並べ替え順の列挙。
TextDirectionTextDirection有効なテキスト方向の列挙。
TextToColumnsDelimiterTextToColumnsDelimiterテキストを列に分割するためにプリセットされた区切り文字の列挙。
ThemeColorTypeThemeColorType有効なテーマのカラータイプを列挙します。
ValueTypeValueTypeスプレッドシート サービスの Range クラスから Range.getValue()Range.getValues() によって返される値の型の列挙型です。NumberBooleanDateString に加えて、以下の列挙値が使用されます。
WrapStrategyWrapStrategyセルをラップするために使用される戦略を列挙します。

Methods

メソッド戻り値の型概要
create(name)Spreadsheet指定した名前で新しいスプレッドシートを作成します。
create(name, rows, columns)Spreadsheet指定した名前と指定した行数と列数で、新しいスプレッドシートを作成します。
enableAllDataSourcesExecution()voidすべての種類のデータソースに対してデータ実行を有効にします。
enableBigQueryExecution()voidBigQuery データソースのデータ実行をオンにします。
flush()voidスプレッドシートの保留中の変更をすべて適用します。
getActive()Spreadsheet現在アクティブなスプレッドシートを返します。スプレッドシートがない場合は null を返します。
getActiveRange()Rangeアクティブ シート内の選択した範囲を返します。アクティブ範囲がない場合は null を返します。
getActiveRangeList()RangeListアクティブ シート内のアクティブ範囲のリストを返します。範囲が選択されていない場合は null を返します。
getActiveSheet()Sheetスプレッドシート内のアクティブ シートを取得します。
getActiveSpreadsheet()Spreadsheet現在アクティブなスプレッドシートを返します。スプレッドシートがない場合は null を返します。
getCurrentCell()Rangeアクティブ シート内のアクティブ範囲のいずれかで選択された現在の(ハイライト表示された)セルを返します。現在のセルがない場合は null を返します。
getSelection()Selectionスプレッドシートの現在の Selection を返します。
getUi()Uiスプレッドシートのユーザー インターフェース環境のインスタンスを返します。この環境では、スクリプトがメニュー、ダイアログ、サイドバーなどの機能を追加できます。
newCellImage()CellImageBuilderCellImage のビルダーを作成します。
newColor()ColorBuilderColor のビルダーを作成します。
newConditionalFormatRule()ConditionalFormatRuleBuilder条件付き書式ルールのビルダーを作成します。
newDataSourceSpec()DataSourceSpecBuilderDataSourceSpec のビルダーを作成します。
newDataValidation()DataValidationBuilderデータ入力規則のビルダーを作成します。
newFilterCriteria()FilterCriteriaBuilderFilterCriteria のビルダーを作成します。
newRichTextValue()RichTextValueBuilderリッチテキスト値のビルダーを作成します。
newTextStyle()TextStyleBuilderテキスト スタイルのビルダーを作成します。
open(file)Spreadsheet指定した File オブジェクトに対応するスプレッドシートを開きます。
openById(id)Spreadsheet指定された ID のスプレッドシートを開きます。
openByUrl(url)Spreadsheet指定した URL のスプレッドシートを開きます。
setActiveRange(range)Range指定された範囲を active range として設定し、範囲内の左上のセルを current cell として設定します。
setActiveRangeList(rangeList)RangeList指定された範囲リストを active ranges として設定します。
setActiveSheet(sheet)Sheetスプレッドシートにアクティブ シートを設定します。
setActiveSheet(sheet, restoreSelection)Sheetスプレッドシート内のアクティブなシートを設定し、そのシート内の最新の選択を復元するオプションを提供します。
setActiveSpreadsheet(newActiveSpreadsheet)voidアクティブなスプレッドシートを設定します。
setCurrentCell(cell)Range指定されたセルを current cell として設定します。

詳細なドキュメント

create(name)

指定した名前で新しいスプレッドシートを作成します。

// The code below creates a new spreadsheet "Finances" and logs the URL for it
var ssNew = SpreadsheetApp.create("Finances");
Logger.log(ssNew.getUrl());

パラメータ

名前説明
nameStringスプレッドシートの名前。

リターン

Spreadsheet - 新しいスプレッドシート

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

create(name, rows, columns)

指定した名前と指定した行数と列数で、新しいスプレッドシートを作成します。

// The code below creates a new spreadsheet "Finances" with 50 rows and 5 columns and logs the
// URL for it
var ssNew = SpreadsheetApp.create("Finances", 50, 5);
Logger.log(ssNew.getUrl());

パラメータ

名前説明
nameStringスプレッドシートの名前。
rowsIntegerスプレッドシートの行数。
columnsIntegerスプレッドシートの列数。

リターン

Spreadsheet - 新しいスプレッドシート

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

enableAllDataSourcesExecution()

すべての種類のデータソースに対してデータ実行を有効にします。

データソースのタイプが有効になっていない場合、データの実行で例外がスローされます。すべてのデータソース タイプに対してデータ実行を有効にするには、このメソッドを使用します。

// Turns data execution on for all types of data sources.
SpreadsheetApp.enableAllDataSourcesExecution();

// Opens the spreadsheet file by its ID. If you created your script from a Google Sheets file,
// use SpreadsheetApp.getActiveSpreadsheet().
// TODO(developer): Replace the ID with your own.
const ss = SpreadsheetApp.openById('abc123456');

// Gets the first data source sheet in the spreadsheet and refreshes the data.
ss.getDataSourceSheets()[0].refreshData();

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • https://www.googleapis.com/auth/bigquery.readonly

enableBigQueryExecution()

BigQuery データソースのデータ実行をオンにします。

オンにしないと、BigQuery データソースのデータ実行で例外がスローされます。

// Turns data execution on for BigQuery data sources.
SpreadsheetApp.enableBigQueryExecution();

// Opens the spreadsheet file by its ID. If you created your script from a Google Sheets file,
// use SpreadsheetApp.getActiveSpreadsheet().
// TODO(developer): Replace the ID with your own.
const ss = SpreadsheetApp.openById('abc123456');

// Gets the first data source sheet in the spreadsheet and refreshes the BigQuery data.
ss.getDataSourceSheets()[0].refreshData();

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • https://www.googleapis.com/auth/bigquery.readonly

flush()

スプレッドシートに対する保留中の変更をすべて適用します。

スプレッドシートのオペレーションは、Range.getValue() を複数回呼び出す場合など、パフォーマンスを向上させるためにバンドルされる場合があります。ただし、スクリプトの実行中にユーザーデータを表示したい場合など、保留中の変更をすべて直ちに適用したい場合もあります。

// The code below changes the background color of cells A1 and B1 twenty times. You should be
// able to see the updates live in the spreadsheet. If flush() is not called, the updates may
// be applied live or may all be applied at once when the script completes.
function colors() {
  var sheet = SpreadsheetApp.getActiveSheet();
  for (var i = 0; i < 20; i++) {
    if ((i % 2) == 0) {
      sheet.getRange('A1').setBackground('green');
      sheet.getRange('B1').setBackground('red');
    } else {
      sheet.getRange('A1').setBackground('red');
      sheet.getRange('B1').setBackground('green');
    }
    SpreadsheetApp.flush();
  }
}

getActive()

現在アクティブなスプレッドシートを返します。スプレッドシートが存在しない場合は null を返します。

スプレッドシートのコンテキストで実行される関数は、この関数を呼び出すことで、対応するスプレッドシート オブジェクトへの参照を取得できます。

// The code below logs the URL for the active spreadsheet.
Logger.log(SpreadsheetApp.getActive().getUrl());

リターン

Spreadsheet - アクティブなスプレッドシート オブジェクト

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

getActiveRange()

アクティブ シート内の選択した範囲を返します。アクティブ範囲がない場合は null を返します。複数の範囲を選択した場合、このメソッドは最後に選択された範囲のみを返します。

これは通常、ユーザーがアクティブ シートで選択した範囲を意味しますが、カスタム関数ではアクティブに再計算されているセルを参照します。

// The code below logs the background color for the active range.
var colorObject = SpreadsheetApp.getActiveRange().getBackgroundObject();
// Assume the color has ColorType.RGB.
Logger.log(colorObject.asRgbColor().asHexString());

リターン

Range - アクティブ範囲。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

getActiveRangeList()

アクティブ シート内のアクティブ範囲のリストを返します。範囲が選択されていない場合は null を返します。現在ハイライト表示されているセルを含むアクティブ範囲が、リストの最後に配置されます。

範囲が 1 つ選択された場合は、getActiveRange() 呼び出しとして機能します。

// Returns the list of active ranges.
var rangeList = SpreadsheetApp.getActiveRangeList();

リターン

RangeList - アクティブ範囲のリスト

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

getActiveSheet()

スプレッドシート内のアクティブ シートを取得します。

スプレッドシート内のアクティブ シートは、スプレッドシートの UI に表示されているシートです。

// The code below logs the name of the active sheet.
Logger.log(SpreadsheetApp.getActiveSheet().getName());

リターン

Sheet - アクティブなスプレッドシート オブジェクト

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

getActiveSpreadsheet()

現在アクティブなスプレッドシートを返します。スプレッドシートが存在しない場合は null を返します。

スプレッドシートのコンテキストで実行される関数は、この関数を呼び出すことで、対応する Spreadsheet オブジェクトへの参照を取得できます。

// The code below logs the URL for the active spreadsheet.
Logger.log(SpreadsheetApp.getActiveSpreadsheet().getUrl());

リターン

Spreadsheet - アクティブなスプレッドシート オブジェクト

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

getCurrentCell()

アクティブ シート内のアクティブ範囲のいずれかで選択された現在の(ハイライト表示された)セルを返します。現在のセルがない場合は null を返します。

// Returns the current highlighted cell in the one of the active ranges.
var currentCell = SpreadsheetApp.getCurrentCell();

リターン

Range - 現在のセル

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

getSelection()

スプレッドシートの現在の Selection を返します。

var selection = SpreadsheetApp.getSelection();
var currentCell = selection.getCurrentCell();

リターン

Selection - 現在の選択

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

getUi()

スプレッドシートのユーザー インターフェース環境のインスタンスを返します。この環境では、スクリプトがメニュー、ダイアログ、サイドバーなどの機能を追加できます。スクリプトがスプレッドシートにバインドされている場合にのみ、開いているスプレッドシートの現在のインスタンスについて、UI を操作できる。詳しくは、メニューダイアログとサイドバーのガイドをご覧ください。

// Add a custom menu to the active spreadsheet, including a separator and a sub-menu.
function onOpen(e) {
  SpreadsheetApp.getUi()
      .createMenu('My Menu')
      .addItem('My menu item', 'myFunction')
      .addSeparator()
      .addSubMenu(SpreadsheetApp.getUi().createMenu('My sub-menu')
          .addItem('One sub-menu item', 'mySecondFunction')
          .addItem('Another sub-menu item', 'myThirdFunction'))
      .addToUi();
}

リターン

Ui - このスプレッドシートのユーザー インターフェース環境のインスタンス


newCellImage()

CellImage のビルダーを作成します。

// 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 on Sheet1.
const range = sheet.getRange('A1');

// Builds an image using a source URL.
const cellImage = SpreadsheetApp.newCellImage()
  .setSourceUrl('https://www.gstatic.com/images/branding/productlogos/apps_script/v10/web-64dp/logo_apps_script_color_1x_web_64dp.png')
  .build();

// Sets the image in cell A1.
range.setValue(cellImage);

リターン

CellImageBuilder - 新しいビルダー。


newColor()

Color のビルダーを作成します。

var rgbColor = SpreadsheetApp.newColor().setRgbColor("#FF0000").build();

リターン

ColorBuilder - 新しいビルダー。


newConditionalFormatRule()

条件付き書式ルールのビルダーを作成します。

// Adds a conditional format rule to a sheet that causes all cells in range A1:B3 to turn red
// if they contain a number between 1 and 10.
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var range = sheet.getRange('A1:B3');
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenNumberBetween(1, 10)
    .setBackground("#FF0000")
    .setRanges([range])
    .build()
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

リターン

ConditionalFormatRuleBuilder - 新しいビルダー。


newDataSourceSpec()

DataSourceSpec のビルダーを作成します。

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

// Enables BigQuery.
SpreadsheetApp.enableBigQueryExecution();

// Builds a data source specification.
// TODO (developer): Update the project ID to your own Google Cloud project ID.
const dataSourceSpec = SpreadsheetApp.newDataSourceSpec()
  .asBigQuery()
  .setProjectId('project-id-1')
  .setTableProjectId('bigquery-public-data')
  .setDatasetId('ncaa_basketball')
  .setTableId('mbb_historical_teams_games')
  .build();

// Adds the data source and its data to the spreadsheet.
ss.insertDataSourceSheet(dataSourceSpec);

リターン

DataSourceSpecBuilder - 新しいビルダー。


newDataValidation()

データ入力規則のビルダーを作成します。

var cell = SpreadsheetApp.getActive().getRange('A1');
var rule = SpreadsheetApp.newDataValidation()
    .requireNumberBetween(1, 100)
    .setAllowInvalid(false)
    .setHelpText('Number must be between 1 and 100.')
    .build();
cell.setDataValidation(rule);

リターン

DataValidationBuilder - 新しいビルダー。


newFilterCriteria()

FilterCriteria のビルダーを作成します。

// 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 the range to A1:D20.
const range = sheet.getRange('A1:D20');

// Creates a filter and applies it to the specified range.
range.createFilter();

// Gets the current filter for the range and creates filter criteria that only shows cells
// that aren't empty.
const filter = range.getFilter();
const criteria = SpreadsheetApp.newFilterCriteria().whenCellNotEmpty().build();

// Sets the criteria to  column C.
filter.setColumnFilterCriteria(3, criteria);

リターン

FilterCriteriaBuilder - 新しいビルダー


newRichTextValue()

リッチテキスト値のビルダーを作成します。

// Sets cell A1 to have the text "Hello world", with "Hello" bolded.
var cell = SpreadsheetApp.getActive().getRange('A1');
var bold = SpreadsheetApp.newTextStyle().setBold(true).build();
var value = SpreadsheetApp.newRichTextValue()
    .setText("Hello world")
    .setTextStyle(0, 5, bold)
    .build();
cell.setRichTextValue(value);

リターン

RichTextValueBuilder - 新しいビルダー。


newTextStyle()

テキスト スタイルのビルダーを作成します。

// Sets range A1:B3 to have red, size 22, bolded, underlined text.
var range = SpreadsheetApp.getActive().getRange('A1:B3');
var style = SpreadsheetApp.newTextStyle()
    .setForegroundColor("red")
    .setFontSize(22)
    .setBold(true)
    .setUnderline(true)
    .build();
range.setTextStyle(style);

リターン

TextStyleBuilder - 新しいビルダー。


open(file)

指定された File オブジェクトに対応するスプレッドシートを開きます。

// Get any starred spreadsheets from Google Drive, then open the spreadsheets and log the name
// of the first sheet within each spreadsheet.
var files = DriveApp.searchFiles(
    'starred = true and mimeType = "' + MimeType.GOOGLE_SHEETS + '"');
while (files.hasNext()) {
  var spreadsheet = SpreadsheetApp.open(files.next());
  var sheet = spreadsheet.getSheets()[0];
  Logger.log(sheet.getName());
}

パラメータ

名前説明
fileFile開くファイル。

リターン

Spreadsheet - スプレッドシート

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

openById(id)

指定された ID のスプレッドシートを開きます。スプレッドシート ID は URL から抽出できます。たとえば、URL https://docs.google.com/spreadsheets/d/abc1234567/edit#gid=0 のスプレッドシート ID は「abc1234567」です。

// The code below opens a spreadsheet using its ID and logs the name for it.
// Note that the spreadsheet is NOT physically opened on the client side.
// It is opened on the server only (for modification by the script).
var ss = SpreadsheetApp.openById("abc1234567");
Logger.log(ss.getName());

パラメータ

名前説明
idStringスプレッドシートの一意の識別子。

リターン

Spreadsheet - 指定された ID を持つスプレッドシート オブジェクト。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

openByUrl(url)

指定した URL のスプレッドシートを開きます。URL が存在しない場合、またはユーザーが URL へのアクセス権限を持っていない場合は、スクリプト例外をスローします。

// Opens a spreadsheet by its URL and logs its name.
// Note that the spreadsheet doesn't physically open on the client side.
// It opens on the server only (for modification by the script).
var ss = SpreadsheetApp.openByUrl(
    'https://docs.google.com/spreadsheets/d/abc1234567/edit');
console.log(ss.getName());

パラメータ

名前説明
urlStringスプレッドシートの URL。

リターン

Spreadsheet - 指定された URL を持つスプレッドシート オブジェクト。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

setActiveRange(range)

指定された範囲を active range として設定し、範囲内の左上のセルを current cell として設定します。

スプレッドシートの UI に、選択した範囲を含むシートが表示され、選択した範囲で定義されているセルが選択されます。

// The code below sets range C1:D4 in the first sheet as the active range.
var range = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange('C1:D4');
SpreadsheetApp.setActiveRange(range);

var selection = SpreadsheetApp.getSelection();
// Current cell: C1
var currentCell = selection.getCurrentCell();
// Active Range: C1:D4
var activeRange = selection.getActiveRange();

パラメータ

名前説明
rangeRangeアクティブ範囲にする範囲。

リターン

Range - 新しいアクティブ範囲


setActiveRangeList(rangeList)

指定された範囲リストを active ranges として設定します。リストの最後の範囲が active range として設定されます。

// The code below sets ranges [D4, B2:C4] in the active sheet as the active ranges.
var rangeList = SpreadsheetApp.getActiveSheet().getRanges(['D4', 'B2:C4']);
SpreadsheetApp.setActiveRangeList(rangeList);

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

パラメータ

名前説明
rangeListRangeList選択する範囲のリスト。

リターン

RangeList - 新しく選択された範囲のリスト

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

setActiveSheet(sheet)

スプレッドシートにアクティブ シートを設定します。Google スプレッドシートの UI には、別のスプレッドシートに属している場合を除き、選択されたシートが表示されます。

// The code below makes the 2nd sheet active in the active spreadsheet.
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[1]);

パラメータ

名前説明
sheetSheet新しいアクティブ シート。

リターン

Sheet - 新しいアクティブ シートにされたシート


setActiveSheet(sheet, restoreSelection)

スプレッドシート内のアクティブなシートを設定し、そのシート内の最新の選択を復元するオプションを提供します。Google スプレッドシートの UI には、別のスプレッドシートに属している場合を除き、選択されたシートが表示されます。

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var firstSheet = spreadsheet.getSheets()[0];
var secondSheet = spreadsheet.getSheets()[1];
// Set the first sheet as the active sheet and select the range D4:F4.
spreadsheet.setActiveSheet(firstSheet).getRange('D4:F4').activate();

// Switch to the second sheet to do some work.
spreadsheet.setActiveSheet(secondSheet);
// Switch back to first sheet, and restore its selection.
spreadsheet.setActiveSheet(firstSheet, true);

// The selection of first sheet is restored, and it logs D4:F4
var range = spreadsheet.getActiveSheet().getSelection().getActiveRange();
Logger.log(range.getA1Notation());

パラメータ

名前説明
sheetSheet新しいアクティブ シート。
restoreSelectionBooleantrue の場合、新しいシートがアクティブになると、新しいアクティブ シートの最新の選択が再び選択されます。false の場合、現在の選択を変更せずに新しいシートがアクティブになります。

リターン

Sheet - 新しいアクティブ シート

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

setActiveSpreadsheet(newActiveSpreadsheet)

アクティブなスプレッドシートを設定します。

// The code below makes the spreadsheet with key "1234567890" the active spreadsheet
var ss = SpreadsheetApp.openById("1234567890");
SpreadsheetApp.setActiveSpreadsheet(ss);

パラメータ

名前説明
newActiveSpreadsheetSpreadsheetアクティブなスプレッドシートにするスプレッドシート。

setCurrentCell(cell)

指定したセルを current cell として設定します。

指定したセルがすでに選択されている範囲に存在する場合は、その範囲がアクティブ範囲になり、セルは現在のセルとなります。

指定したセルが選択した範囲に存在しない場合は、既存の選択が削除され、そのセルが現在のセルおよびアクティブ範囲になります。

注: 指定する Range は 1 つのセルで構成する必要があります。そうでない場合、このメソッドは例外をスローします。

// The code below sets the cell B5 in the first sheet as the current cell.
var cell = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange('B5');
SpreadsheetApp.setCurrentCell(cell);

var selection = SpreadsheetApp.getSelection();
// Current cell: B5
var currentCell = selection.getCurrentCell();

パラメータ

名前説明
cellRange現在のセルとして設定するセルです。

リターン

Range - 新しく設定された現在のセル

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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