Excel.Workbook class

ブックは、ワークシート、テーブル、範囲などの関連するブック オブジェクトを含む最上位レベルのオブジェクトです。 ブック オブジェクト モデルの詳細については、「 Excel JavaScript API を使用してブックを操作する」を参照してください。

Extends

注釈

API セット: ExcelApi 1.1

使用元

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/50-workbook/workbook-get-active-cell.yaml

await Excel.run(async (context) => {

    let myWorkbook = context.workbook;
    let activeCell = myWorkbook.getActiveCell();
    activeCell.load("address");

    await context.sync();
    
    console.log("The active cell is " + activeCell.address);
});

プロパティ

application

このブックを含む Excel アプリケーション インスタンスを表します。

bindings

ブックの一部であるバインドのコレクションを表します。

context

オブジェクトに関連付けられている要求コンテキスト。 これにより、アドインのプロセスが Office ホスト アプリケーションのプロセスに接続されます。

customXmlParts

このブックに含まれるカスタム XML パーツのコレクションを表します。

dataConnections

ブック内のすべてのデータ接続を表します。

functions

計算に使用できるワークシート関数のコレクションを表します。

name

ブックの名前を取得します。

names

ブック スコープの名前付き項目 (名前付き範囲と定数) のコレクションを表します。

pivotTables

ブックに関連付けられているピボットテーブルのコレクションを表します。

properties

ブックのプロパティを取得します。

protection

ブックの保護オブジェクトを返します。

settings

ブックに関連付けられている設定のコレクションを表します。

styles

ブックに関連付けられているスタイルのコレクションを表します。

tables

ブックに関連付けられているテーブルのコレクションを表します。

worksheets

ブックに関連付けられているワークシートのコレクションを表します。

メソッド

getActiveCell()

ブックで現在アクティブなセルを取得します。

getSelectedRange()

現在選択されている単一の範囲をブックから取得します。 複数の範囲が選択されている場合、このメソッドはエラーをスローします。

load(options)

オブジェクトの指定されたプロパティを読み込むコマンドを待ち行列に入れます。 プロパティを読み取る前に、context.sync() を呼び出す必要があります。

load(propertyNames)

オブジェクトの指定されたプロパティを読み込むコマンドを待ち行列に入れます。 プロパティを読み取る前に、context.sync() を呼び出す必要があります。

load(propertyNamesAndPaths)

オブジェクトの指定されたプロパティを読み込むコマンドを待ち行列に入れます。 プロパティを読み取る前に、context.sync() を呼び出す必要があります。

set(properties, options)

オブジェクトの複数のプロパティを同時に設定します。 適切なプロパティを持つプレーン オブジェクト、または同じ型の別の API オブジェクトを渡すことができます。

set(properties)

既存の読み込まれたオブジェクトに基づいて、オブジェクトに複数のプロパティを同時に設定します。

toJSON()

API オブジェクトが JSON.stringify() に渡されるときに、より有用な出力を提供するために、JavaScript toJSON() メソッドをオーバーライドします。 (次に、JSON.stringify渡されたオブジェクトの toJSON メソッドを呼び出します)。元の Excel.Workbook オブジェクトが API オブジェクトであるのに対し、 toJSON メソッドは、元のオブジェクトから読み込まれた子プロパティの浅いコピーを含むプレーンな JavaScript オブジェクト ( Excel.Interfaces.WorkbookData と型指定) を返します。

イベント

onSelectionChanged

ドキュメント内で選択が変更されるときに発生します。

プロパティの詳細

application

このブックを含む Excel アプリケーション インスタンスを表します。

readonly application: Excel.Application;

プロパティ値

注釈

API セット: ExcelApi 1.1

bindings

ブックの一部であるバインドのコレクションを表します。

readonly bindings: Excel.BindingCollection;

プロパティ値

注釈

API セット: ExcelApi 1.1

context

オブジェクトに関連付けられている要求コンテキスト。 これにより、アドインのプロセスが Office ホスト アプリケーションのプロセスに接続されます。

context: RequestContext;

プロパティ値

customXmlParts

このブックに含まれるカスタム XML パーツのコレクションを表します。

readonly customXmlParts: Excel.CustomXmlPartCollection;

プロパティ値

注釈

API セット: ExcelApi 1.5

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/18-custom-xml-parts/test-xml-for-unique-namespace.yaml

await Excel.run(async (context) => {
    document.getElementById("display-xml").textContent = "";
    const customXmlParts = context.workbook.customXmlParts;
    customXmlParts.load("items");

    await context.sync();

    for (let i = 0; i < customXmlParts.items.length; i++) {
        customXmlParts.items[i].delete();
    }

    await context.sync();
});

dataConnections

ブック内のすべてのデータ接続を表します。

readonly dataConnections: Excel.DataConnectionCollection;

プロパティ値

注釈

API セット: ExcelApi 1.7

functions

計算に使用できるワークシート関数のコレクションを表します。

readonly functions: Excel.Functions;

プロパティ値

注釈

API セット: ExcelApi 1.2

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/50-workbook/workbook-built-in-functions.yaml

await Excel.run(async (context) => {
  // This function uses VLOOKUP to find data in the "Wrench" row
  // on the worksheet, and then it uses SUM to combine the values.
  let range = context.workbook.worksheets.getItem("Sample").getRange("A1:D4");

  // Get the values in the second, third, and fourth columns in the "Wrench" row,
  // and combine those values with SUM.
  let sumOfTwoLookups = context.workbook.functions.sum(
    context.workbook.functions.vlookup("Wrench", range, 2, false),
    context.workbook.functions.vlookup("Wrench", range, 3, false),
    context.workbook.functions.vlookup("Wrench", range, 4, false)
  );
  sumOfTwoLookups.load("value");

  await context.sync();
  console.log(" Number of wrenches sold in November, December, and January = " + sumOfTwoLookups.value);
});

name

ブックの名前を取得します。

readonly name: string;

プロパティ値

string

注釈

API セット: ExcelApi 1.7

names

ブック スコープの名前付き項目 (名前付き範囲と定数) のコレクションを表します。

readonly names: Excel.NamedItemCollection;

プロパティ値

注釈

API セット: ExcelApi 1.1

pivotTables

ブックに関連付けられているピボットテーブルのコレクションを表します。

readonly pivotTables: Excel.PivotTableCollection;

プロパティ値

注釈

API セット: ExcelApi 1.3

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/38-pivottable/pivottable-get-pivottables.yaml

await Excel.run(async (context) => {
  // Get the names of all the PivotTables in the workbook.
  const pivotTables = context.workbook.pivotTables;
  pivotTables.load("name");
  await context.sync();

  // Display the names in the console.
  console.log("PivotTables in the workbook:")
  pivotTables.items.forEach((pivotTable) => {
    console.log(`\t${pivotTable.name}`);
  });
});

properties

ブックのプロパティを取得します。

readonly properties: Excel.DocumentProperties;

プロパティ値

注釈

API セット: ExcelApi 1.7

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/26-document/properties.yaml

await Excel.run(async (context) => {
    let titleValue = "Excel document properties API";
    let subjectValue = "Set and get document properties";
    let keywordsValue = "Set and get operations";
    let commentsValue = "This is an Excel document properties API code sample";
    let categoryValue = "Office Add-ins";
    let managerValue = "John";
    let companyValue = "Microsoft";

    let docProperties = context.workbook.properties;

    // Set the writeable document properties.
    docProperties.title = titleValue;
    docProperties.subject = subjectValue;
    docProperties.keywords = keywordsValue;
    docProperties.comments = commentsValue;
    docProperties.category = categoryValue;
    docProperties.manager = managerValue;
    docProperties.company = companyValue;

    await context.sync();

    console.log("Set the following document properties: title, subject, keywords, comments, category, manager, company.");
});

protection

ブックの保護オブジェクトを返します。

readonly protection: Excel.WorkbookProtection;

プロパティ値

注釈

API セット: ExcelApi 1.7

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/50-workbook/data-protection.yaml

await Excel.run(async (context) => {
    const workbook: Excel.Workbook = context.workbook;
    workbook.protection.unprotect();
});

settings

ブックに関連付けられている設定のコレクションを表します。

readonly settings: Excel.SettingCollection;

プロパティ値

注釈

API セット: ExcelApi 1.4

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/30-events/events-workbook-and-worksheet-collection.yaml

await Excel.run(async (context) => {
    const settings: Excel.SettingCollection = context.workbook.settings;
    settings.onSettingsChanged.add(onChangedSetting);

    await context.sync();
    console.log("Settings changed handler registered.");
});

styles

ブックに関連付けられているスタイルのコレクションを表します。

readonly styles: Excel.StyleCollection;

プロパティ値

注釈

API セット: ExcelApi 1.7

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/style.yaml

await Excel.run(async (context) => {
    let styles = context.workbook.styles;

    // Add a new style to the style collection.
    // Styles is in the Home tab ribbon.
    styles.add("Diagonal Orientation Style");
          
    let newStyle = styles.getItem("Diagonal Orientation Style");
    
    // The "Diagonal Orientation Style" properties.
    newStyle.textOrientation = 38;
    newStyle.autoIndent = true;
    newStyle.includeProtection = true;
    newStyle.shrinkToFit = true;
    newStyle.locked = false;
    
    await context.sync();

    console.log("Successfully added a new style with diagonal orientation to the Home tab ribbon.");
});

tables

ブックに関連付けられているテーブルのコレクションを表します。

readonly tables: Excel.TableCollection;

プロパティ値

注釈

API セット: ExcelApi 1.1

worksheets

ブックに関連付けられているワークシートのコレクションを表します。

readonly worksheets: Excel.WorksheetCollection;

プロパティ値

注釈

API セット: ExcelApi 1.1

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/54-worksheet/list-worksheets.yaml

await Excel.run(async (context) => {
    const sheets: Excel.WorksheetCollection = context.workbook.worksheets;
    sheets.load("items/name");
    
    await context.sync();

    if (sheets.items.length > 1) {
        console.log(`There are ${sheets.items.length} worksheets in the workbook:`);
    } else {
        console.log(`There is one worksheet in the workbook:`);
    }
    for (let i in sheets.items) {
        console.log(sheets.items[i].name);
    } 
});

メソッドの詳細

getActiveCell()

ブックで現在アクティブなセルを取得します。

getActiveCell(): Excel.Range;

返品

注釈

API セット: ExcelApi 1.7

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/50-workbook/workbook-get-active-cell.yaml

await Excel.run(async (context) => {

    let myWorkbook = context.workbook;
    let activeCell = myWorkbook.getActiveCell();
    activeCell.load("address");

    await context.sync();
    
    console.log("The active cell is " + activeCell.address);
});

getSelectedRange()

現在選択されている単一の範囲をブックから取得します。 複数の範囲が選択されている場合、このメソッドはエラーをスローします。

getSelectedRange(): Excel.Range;

返品

注釈

API セット: ExcelApi 1.1

await Excel.run(async (context) => { 
    const selectedRange = context.workbook.getSelectedRange();
    selectedRange.load('address');
    await context.sync();
    console.log(selectedRange.address);
});

load(options)

オブジェクトの指定されたプロパティを読み込むコマンドを待ち行列に入れます。 プロパティを読み取る前に、context.sync() を呼び出す必要があります。

load(options?: Excel.Interfaces.WorkbookLoadOptions): Excel.Workbook;

パラメーター

options
Excel.Interfaces.WorkbookLoadOptions

読み込むオブジェクトのプロパティのオプションを指定します。

返品

load(propertyNames)

オブジェクトの指定されたプロパティを読み込むコマンドを待ち行列に入れます。 プロパティを読み取る前に、context.sync() を呼び出す必要があります。

load(propertyNames?: string | string[]): Excel.Workbook;

パラメーター

propertyNames

string | string[]

読み込むプロパティを指定するコンマ区切りの文字列または文字列の配列。

返品

load(propertyNamesAndPaths)

オブジェクトの指定されたプロパティを読み込むコマンドを待ち行列に入れます。 プロパティを読み取る前に、context.sync() を呼び出す必要があります。

load(propertyNamesAndPaths?: {
            select?: string;
            expand?: string;
        }): Excel.Workbook;

パラメーター

propertyNamesAndPaths

{ select?: string; expand?: string; }

propertyNamesAndPaths.select は読み込むプロパティを指定するコンマ区切りの文字列であり、 propertyNamesAndPaths.expand は読み込むナビゲーションのプロパティを指定するコンマ区切りの文字列です。

返品

set(properties, options)

オブジェクトの複数のプロパティを同時に設定します。 適切なプロパティを持つプレーン オブジェクト、または同じ型の別の API オブジェクトを渡すことができます。

set(properties: Interfaces.WorkbookUpdateData, options?: OfficeExtension.UpdateOptions): void;

パラメーター

properties
Excel.Interfaces.WorkbookUpdateData

メソッドが呼び出されたオブジェクトのプロパティと同型構造のプロパティを持つ JavaScript オブジェクト。

options
OfficeExtension.UpdateOptions

プロパティ オブジェクトが読み取り専用プロパティを設定しようとした場合にエラーを抑制するオプションを提供します。

返品

void

set(properties)

既存の読み込まれたオブジェクトに基づいて、オブジェクトに複数のプロパティを同時に設定します。

set(properties: Excel.Workbook): void;

パラメーター

properties
Excel.Workbook

返品

void

toJSON()

API オブジェクトが JSON.stringify() に渡されるときに、より有用な出力を提供するために、JavaScript toJSON() メソッドをオーバーライドします。 (次に、JSON.stringify渡されたオブジェクトの toJSON メソッドを呼び出します)。元の Excel.Workbook オブジェクトが API オブジェクトであるのに対し、 toJSON メソッドは、元のオブジェクトから読み込まれた子プロパティの浅いコピーを含むプレーンな JavaScript オブジェクト ( Excel.Interfaces.WorkbookData と型指定) を返します。

toJSON(): Excel.Interfaces.WorkbookData;

返品

イベントの詳細

onSelectionChanged

ドキュメント内で選択が変更されるときに発生します。

readonly onSelectionChanged: OfficeExtension.EventHandlers<Excel.SelectionChangedEventArgs>;

イベントの種類

注釈

API セット: ExcelApi 1.2