Google スプレッドシート ファイルにアクセスして作成する。このクラスは、スプレッドシート サービスの親クラスです。
プロパティ
| プロパティ | タイプ | 説明 |
|---|---|---|
Auto | Auto | 自動入力された値の計算に使用される系列のタイプを列挙します。 |
Banding | Banding | 可能なバンディング テーマの列挙。 |
Boolean | Boolean | 条件付き書式のブール値条件の列挙。 |
Border | Border | Range の枠線を設定するための有効なスタイルの列挙型。 |
Color | Color | 使用可能な色の種類の列挙。 |
Copy | Copy | 可能な貼り付けタイプの列挙。 |
Data | Data | データ実行エラーコードの列挙。 |
Data | Data | 可能性のあるデータ実行状態の列挙。 |
Data | Data | データソース パラメータの可能なタイプを列挙します。 |
Data | Data | データソースの更新範囲の列挙型。 |
Data | Data | 使用可能なデータソースのタイプの列挙。 |
Data | Data | 範囲に設定できるデータ入力規則の条件を表す列挙型。 |
Date | Date | 日時のグループ化ルールの列挙型。 |
Developer | Developer | デベロッパー メタデータの場所のタイプとして考えられるものの列挙。 |
Developer | Developer | デベロッパー メタデータの公開設定の列挙型。 |
Dimension | Dimension | スプレッドシートの可能なディメンションの列挙。 |
Direction | Direction | 矢印キーを使用してスプレッドシート内を移動できる方向の列挙型。 |
Frequency | Frequency | 可能な頻度タイプの列挙。 |
Group | Group | グループ コントロール トグルが取り得る位置の列挙。 |
Interpolation | Interpolation | 条件付き書式のグラデーション補間タイプの列挙。 |
Pivot | Pivot | ピボット テーブルの値の集計に使用できる関数の列挙。 |
Pivot | Pivot | ピボット値を表示する方法の列挙。 |
Protection | Protection | 編集から保護できるスプレッドシートのパーツを表す列挙型。 |
Recalculation | Recalculation | スプレッドシートの再計算で使用できる間隔の列挙。 |
Relative | Relative | 日付ベースの Boolean で使用する値を計算するための相対日付オプションの列挙。 |
Sheet | Sheet | スプレッドシートに存在する可能性のあるさまざまなシートの列挙型。 |
Sort | Sort | 並べ替え順の列挙。 |
Text | Text | 有効なテキスト方向の列挙。 |
Text | Text | テキストを列に分割するためのプリセット区切り文字の列挙型。 |
Theme | Theme | 使用可能なテーマのカラータイプの列挙。 |
Value | Value | スプレッドシート サービスの Range クラスの Range.getValue() と Range.getValues() から返される値の型の列挙型。以下の列挙値は、Number、Boolean、Date、String に加えて使用できます。 |
Wrap | Wrap | セルの折り返しに使用される戦略の列挙型。 |
メソッド
| メソッド | 戻り値の型 | 概要 |
|---|---|---|
create(name) | Spreadsheet | 指定された名前で新しいスプレッドシートを作成します。 |
create(name, rows, columns) | Spreadsheet | 指定された名前と行数、列数で新しいスプレッドシートを作成します。 |
enable | void | すべてのタイプのデータソースでデータ実行をオンにします。 |
enable | void | BigQuery データソースのデータ実行をオンにします。 |
enable | void | Looker データソースのデータ実行を有効にします。 |
flush() | void | 保留中のスプレッドシートの変更をすべて適用します。 |
get | Spreadsheet | 現在アクティブなスプレッドシートを返します。アクティブなスプレッドシートがない場合は null を返します。 |
get | Range | アクティブなシートで選択された範囲を返します。アクティブな範囲がない場合は null を返します。 |
get | Range | アクティブなシートのアクティブな範囲のリストを返します。範囲が選択されていない場合は null を返します。 |
get | Sheet | スプレッドシートのアクティブなシートを取得します。 |
get | Spreadsheet | 現在アクティブなスプレッドシートを返します。アクティブなスプレッドシートがない場合は null を返します。 |
get | Range|null | アクティブなシートのアクティブな範囲のいずれかで選択されている現在の(ハイライト表示されている)セルを返します。現在のセルがない場合は null を返します。 |
get | Selection | スプレッドシートの現在の Selection を返します。 |
get | Ui | スクリプトでメニュー、ダイアログ、サイドバーなどの機能を追加できるようにする、スプレッドシートのユーザー インターフェース環境のインスタンスを返します。 |
new | Cell | Cell のビルダーを作成します。 |
new | Color | Color のビルダーを作成します。 |
new | Conditional | 条件付き書式ルールのビルダーを作成します。 |
new | Data | Data のビルダーを作成します。 |
new | Data | データ入力規則のビルダーを作成します。 |
new | Filter | Filter のビルダーを作成します。 |
new | Rich | リッチテキスト値のビルダーを作成します。 |
new | Text | テキスト スタイルのビルダーを作成します。 |
open(file) | Spreadsheet | 指定された File オブジェクトに対応するスプレッドシートを開きます。 |
open | Spreadsheet | 指定された ID のスプレッドシートを開きます。 |
open | Spreadsheet | 指定された URL のスプレッドシートを開きます。 |
set | Range | 指定された範囲を active range に設定し、範囲の左上のセルを current cell に設定します。 |
set | Range | 指定された範囲のリストを active ranges として設定します。 |
set | Sheet | スプレッドシートのアクティブなシートを設定します。 |
set | Sheet | スプレッドシートのアクティブなシートを設定します。そのシート内の最新の選択を復元するオプションがあります。 |
set | void | アクティブなスプレッドシートを設定します。 |
set | Range | 指定したセルを current cell に設定します。 |
詳細なドキュメント
create(name)
指定された名前で新しいスプレッドシートを作成します。
// The code below creates a new spreadsheet "Finances" and logs the URL for it const ssNew = SpreadsheetApp.create('Finances'); Logger.log(ssNew.getUrl());
パラメータ
| 名前 | 型 | 説明 |
|---|---|---|
name | String | スプレッドシートの名前。 |
戻る
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 const ssNew = SpreadsheetApp.create('Finances', 50, 5); Logger.log(ssNew.getUrl());
パラメータ
| 名前 | 型 | 説明 |
|---|---|---|
name | String | スプレッドシートの名前。 |
rows | Integer | スプレッドシートの行数。 |
columns | Integer | スプレッドシートの列数。 |
戻る
Spreadsheet - 新しいスプレッドシート
承認
このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/spreadsheets
enable All Data Sources Execution()
すべてのタイプのデータソースでデータ実行をオンにします。
データソースのタイプが有効になっていない場合、データ実行で例外がスローされます。このメソッドを使用して、すべてのデータソース タイプでデータの実行を有効にします。
// 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
enable Big Query Execution()
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
enable Looker Execution()
Looker データソースのデータ実行を有効にします。
Looker データソースのデータ実行は、有効になっていない場合、例外をスローします。
// Turns data execution on for Looker data sources. SpreadsheetApp.enableLookerExecution(); // 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 // associated Looker data. ss.getDataSourceSheets()[0].refreshData();
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() { const sheet = SpreadsheetApp.getActiveSheet(); for (let 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(); } }
get Active()
現在アクティブなスプレッドシートを返します。存在しない場合は null を返します。
スプレッドシートのコンテキストで実行される関数は、この関数を呼び出すことで、対応する Spreadsheet オブジェクトへの参照を取得できます。
// The code below logs the URL for the active spreadsheet. Logger.log(SpreadsheetApp.getActive().getUrl());
戻る
Spreadsheet - アクティブな Spreadsheet オブジェクト
承認
このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Active Range()
アクティブなシートで選択された範囲を返します。アクティブな範囲がない場合は null を返します。複数の範囲が選択されている場合、このメソッドは最後に選択された範囲のみを返します。
通常、これはアクティブなシートでユーザーが選択した範囲を意味しますが、カスタム関数では、アクティブに再計算されているセルを指します。
// The code below logs the background color for the active range. const 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
get Active Range List()
アクティブなシートのアクティブな範囲のリストを返します。範囲が選択されていない場合は null を返します。現在ハイライト表示されているセルを含むアクティブな範囲は、リストの最後に配置されます。
範囲が 1 つ選択されている場合は、get 呼び出しとして動作します。
// Returns the list of active ranges. const rangeList = SpreadsheetApp.getActiveRangeList();
戻る
Range - アクティブな範囲のリスト
承認
このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Active Sheet()
スプレッドシートのアクティブなシートを取得します。
スプレッドシートのアクティブなシートとは、スプレッドシートの UI に表示されているシートのことです。
// The code below logs the name of the active sheet. Logger.log(SpreadsheetApp.getActiveSheet().getName());
戻る
Sheet - アクティブな Sheet オブジェクト
承認
このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Active Spreadsheet()
現在アクティブなスプレッドシートを返します。存在しない場合は null を返します。
スプレッドシートのコンテキストで実行される関数は、この関数を呼び出すことで、対応する Spreadsheet オブジェクトへの参照を取得できます。
// The code below logs the URL for the active spreadsheet. Logger.log(SpreadsheetApp.getActiveSpreadsheet().getUrl());
戻る
Spreadsheet - アクティブな Spreadsheet オブジェクト
承認
このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Current Cell()
アクティブなシートのアクティブな範囲のいずれかで選択されている現在の(ハイライト表示されている)セルを返します。現在のセルがない場合は null を返します。
// Returns the current highlighted cell in the one of the active ranges. const currentCell = SpreadsheetApp.getCurrentCell();
戻る
Range|null - 現在のセル
承認
このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Selection()
get Ui()
スクリプトでメニュー、ダイアログ、サイドバーなどの機能を追加できるようにする、スプレッドシートのユーザー インターフェース環境のインスタンスを返します。スクリプトが操作できるのは、開いているスプレッドシートの現在のインスタンスの 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 - このスプレッドシートのユーザー インターフェース環境のインスタンス
new Cell Image()
Cell のビルダーを作成します。
// 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);
戻る
Cell - 新しいビルダー。
new Color()
Color のビルダーを作成します。
const rgbColor = SpreadsheetApp.newColor().setRgbColor('#FF0000').build();
戻る
Color - 新しいビルダー。
new Conditional Format Rule()
条件付き書式ルールのビルダーを作成します。
// 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. const sheet = SpreadsheetApp.getActive().getActiveSheet(); const range = sheet.getRange('A1:B3'); const rule = SpreadsheetApp.newConditionalFormatRule() .whenNumberBetween(1, 10) .setBackground('#FF0000') .setRanges([range]) .build(); const rules = sheet.getConditionalFormatRules(); rules.push(rule); sheet.setConditionalFormatRules(rules);
戻る
Conditional - 新しいビルダー。
new Data Source Spec()
Data のビルダーを作成します。
// 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);
戻る
Data - 新しいビルダー。
new Data Validation()
データ入力規則のビルダーを作成します。
const cell = SpreadsheetApp.getActive().getRange('A1'); const rule = SpreadsheetApp.newDataValidation() .requireNumberBetween(1, 100) .setAllowInvalid(false) .setHelpText('Number must be between 1 and 100.') .build(); cell.setDataValidation(rule);
戻る
Data - 新しいビルダー。
new Filter Criteria()
Filter のビルダーを作成します。
// 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);
戻る
Filter - 新しいビルダー
new Rich Text Value()
リッチテキスト値のビルダーを作成します。
// Sets cell A1 to have the text "Hello world", with "Hello" bolded. const cell = SpreadsheetApp.getActive().getRange('A1'); const bold = SpreadsheetApp.newTextStyle().setBold(true).build(); const value = SpreadsheetApp.newRichTextValue() .setText('Hello world') .setTextStyle(0, 5, bold) .build(); cell.setRichTextValue(value);
戻る
Rich - 新しいビルダー。
new Text Style()
テキスト スタイルのビルダーを作成します。
// Sets range A1:B3 to have red, size 22, bolded, underlined text. const range = SpreadsheetApp.getActive().getRange('A1:B3'); const style = SpreadsheetApp.newTextStyle() .setForegroundColor('red') .setFontSize(22) .setBold(true) .setUnderline(true) .build(); range.setTextStyle(style);
戻る
Text - 新しいビルダー。
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. const files = DriveApp.searchFiles( `starred = true and mimeType = "${MimeType.GOOGLE_SHEETS}"`, ); while (files.hasNext()) { const spreadsheet = SpreadsheetApp.open(files.next()); const sheet = spreadsheet.getSheets()[0]; Logger.log(sheet.getName()); }
パラメータ
| 名前 | 型 | 説明 |
|---|---|---|
file | File | 開くファイル。 |
戻る
Spreadsheet - スプレッドシート
承認
このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/spreadsheets
open By Id(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). const ss = SpreadsheetApp.openById('abc1234567'); Logger.log(ss.getName());
パラメータ
| 名前 | 型 | 説明 |
|---|---|---|
id | String | スプレッドシートの一意の識別子。 |
戻る
Spreadsheet - 指定された ID のスプレッドシート オブジェクト
承認
このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/spreadsheets
open By 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). const ss = SpreadsheetApp.openByUrl( 'https://docs.google.com/spreadsheets/d/abc1234567/edit', ); console.log(ss.getName());
パラメータ
| 名前 | 型 | 説明 |
|---|---|---|
url | String | スプレッドシートの URL。 |
戻る
Spreadsheet - 指定された URL のスプレッドシート オブジェクト。
承認
このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/spreadsheets
set Active Range(range)
指定された範囲を active range に設定し、範囲の左上のセルを current cell に設定します。
スプレッドシートの UI に、選択した範囲を含むシートが表示され、選択した範囲で定義されたセルが選択されます。
// The code below sets range C1:D4 in the first sheet as the active range. const range = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange('C1:D4'); SpreadsheetApp.setActiveRange(range); const selection = SpreadsheetApp.getSelection(); // Current cell: C1 const currentCell = selection.getCurrentCell(); // Active Range: C1:D4 const activeRange = selection.getActiveRange();
パラメータ
| 名前 | 型 | 説明 |
|---|---|---|
range | Range | アクティブな範囲にする範囲。 |
戻る
Range - 新しいアクティブな範囲
set Active Range List(rangeList)
指定された範囲のリストを active ranges として設定します。リストの最後の範囲が active range として設定されます。
// The code below sets ranges [D4, B2:C4] in the active sheet as the active // ranges. const rangeList = SpreadsheetApp.getActiveSheet().getRanges(['D4', 'B2:C4']); SpreadsheetApp.setActiveRangeList(rangeList); const selection = SpreadsheetApp.getSelection(); // Current cell: B2 const currentCell = selection.getCurrentCell(); // Active range: B2:C4 const activeRange = selection.getActiveRange(); // Active range list: [D4, B2:C4] const activeRangeList = selection.getActiveRangeList();
パラメータ
| 名前 | 型 | 説明 |
|---|---|---|
range | Range | 選択する範囲のリスト。 |
戻る
Range - 新しく選択された範囲のリスト
承認
このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Active Sheet(sheet)
スプレッドシートのアクティブなシートを設定します。シートが別のスプレッドシートに属していない限り、選択したシートが Google スプレッドシートの UI に表示されます。
// The code below makes the 2nd sheet active in the active spreadsheet. const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[1]);
パラメータ
| 名前 | 型 | 説明 |
|---|---|---|
sheet | Sheet | 新しいアクティブ シート。 |
戻る
Sheet - 新しいアクティブ シートになったシート
set Active Sheet(sheet, restoreSelection)
スプレッドシートのアクティブなシートを設定します。そのシート内で最新の選択を復元するオプションがあります。シートが別のスプレッドシートに属していない限り、選択したシートが Google スプレッドシートの UI に表示されます。
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); const firstSheet = spreadsheet.getSheets()[0]; const 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 const range = spreadsheet.getActiveSheet().getSelection().getActiveRange(); Logger.log(range.getA1Notation());
パラメータ
| 名前 | 型 | 説明 |
|---|---|---|
sheet | Sheet | 新しいアクティブ シート。 |
restore | Boolean | true の場合、新しいシートがアクティブになると、新しいアクティブ シートの直近の選択が再び選択されます。false の場合、現在の選択を変更せずに新しいシートがアクティブになります。 |
戻る
Sheet - 新しいアクティブ シート
承認
このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Active Spreadsheet(newActiveSpreadsheet)
アクティブなスプレッドシートを設定します。
// The code below makes the spreadsheet with key "1234567890" the active // spreadsheet const ss = SpreadsheetApp.openById('1234567890'); SpreadsheetApp.setActiveSpreadsheet(ss);
パラメータ
| 名前 | 型 | 説明 |
|---|---|---|
new | Spreadsheet | アクティブなスプレッドシートにするスプレッドシート。 |
set Current Cell(cell)
指定したセルを current cell に設定します。
指定したセルがすでに選択されている範囲に含まれている場合、その範囲がアクティブな範囲になり、そのセルが現在のセルになります。
指定されたセルが選択された範囲に存在しない場合、既存の選択が削除され、セルが現在のセルとアクティブな範囲になります。
注: 指定された Range は 1 つのセルで構成されている必要があります。そうでない場合、このメソッドは例外をスローします。
// The code below sets the cell B5 in the first sheet as the current cell. const cell = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange('B5'); SpreadsheetApp.setCurrentCell(cell); const selection = SpreadsheetApp.getSelection(); // Current cell: B5 const currentCell = selection.getCurrentCell();
パラメータ
| 名前 | 型 | 説明 |
|---|---|---|
cell | Range | 現在のセルとして設定するセル。 |
戻る
Range - 新しく設定された現在のセル
承認
このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets