Excel.RangeFormat class

一个格式对象,其中封装了区域的字体、填充、边框、对齐方式和其他属性。

扩展

属性

borders

应用于整个区域的 Border 对象的集合。

columnWidth

指定范围内所有列的宽度。 如果列宽不一致, null 则返回。

context

与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。

fill

返回在整个区域内定义的 fill 对象。

font

返回在整个区域内定义的 Font 对象。

horizontalAlignment

表示指定对象的水平对齐方式。 有关详细信息,请参阅 Excel.HorizontalAlignment。

protection

返回某一区域的格式 protection 对象。

rowHeight

区域中所有行的高度。 如果行高不一致, null 则返回。

verticalAlignment

表示指定对象的垂直对齐方式。 有关详细信息,请参阅 Excel.VerticalAlignment。

wrapText

指定 Excel 是否将文本换行到对象中。 一个 null 值指示整个区域没有统一的换行设置

方法

autofitColumns()

根据列中的当前数据更改当前范围的列宽,以达到最佳宽度。

autofitRows()

根据列中的当前数据,更改当前范围的行高以达到最佳高度。

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。

set(properties, options)

同时设置对象的多个属性。 可以传递具有相应属性的纯文本对象,也可以传递另一个相同类型的 API 对象。

set(properties)

基于现有加载的对象,同时在对象上设置多个属性。

toJSON()

覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Excel.RangeFormat 对象是 API 对象, toJSON 但该方法返回一个纯 JavaScript 对象, (类型化为 Excel.Interfaces.RangeFormatData) ,其中包含从原始对象加载的任何子属性的浅层副本。

属性详细信息

borders

应用于整个区域的 Border 对象的集合。

readonly borders: Excel.RangeBorderCollection;

属性值

注解

API 集:ExcelApi 1.1

columnWidth

指定范围内所有列的宽度。 如果列宽不一致, null 则返回。

columnWidth: number;

属性值

number

注解

API 集:ExcelApi 1.2

context

与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。

context: RequestContext;

属性值

fill

返回在整个区域内定义的 fill 对象。

readonly fill: Excel.RangeFill;

属性值

注解

API 集:ExcelApi 1.1

示例

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

// Set the fill color for the header range.
await Excel.run(async (context) => {
    const sheet: Excel.Worksheet = context.workbook.worksheets.getItem("Sample");
    const headerRange: Excel.Range = sheet.getRange("B2:E2");

    headerRange.format.fill.color = "#D9EAF7";

    await context.sync();
    console.log("The header fill color was updated.");
});

font

返回在整个区域内定义的 Font 对象。

readonly font: Excel.RangeFont;

属性值

注解

API 集:ExcelApi 1.1

horizontalAlignment

表示指定对象的水平对齐方式。 有关详细信息,请参阅 Excel.HorizontalAlignment。

horizontalAlignment: Excel.HorizontalAlignment | "General" | "Left" | "Center" | "Right" | "Fill" | "Justify" | "CenterAcrossSelection" | "Distributed";

属性值

Excel.HorizontalAlignment | "General" | "Left" | "Center" | "Right" | "Fill" | "Justify" | "CenterAcrossSelection" | "Distributed"

注解

API 集:ExcelApi 1.1

示例

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

await Excel.run(async (context) => {
  // Set a default value for an empty cell in the PivotTable. This doesn't include cells left blank by the layout.
  const pivotTable = context.workbook.pivotTables.getItem("Farm Sales");
  const pivotLayout = pivotTable.layout;

  pivotLayout.emptyCellText = "--";

  // Set the text alignment to match the rest of the PivotTable.
  pivotLayout.getDataBodyRange().format.horizontalAlignment = Excel.HorizontalAlignment.right;
  await context.sync();
});

protection

返回某一区域的格式 protection 对象。

readonly protection: Excel.FormatProtection;

属性值

注解

API 集:ExcelApi 1.2

rowHeight

区域中所有行的高度。 如果行高不一致, null 则返回。

rowHeight: number;

属性值

number

注解

API 集:ExcelApi 1.2

verticalAlignment

表示指定对象的垂直对齐方式。 有关详细信息,请参阅 Excel.VerticalAlignment。

verticalAlignment: Excel.VerticalAlignment | "Top" | "Center" | "Bottom" | "Justify" | "Distributed";

属性值

Excel.VerticalAlignment | "Top" | "Center" | "Bottom" | "Justify" | "Distributed"

注解

API 集:ExcelApi 1.1

示例

// 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 worksheet = context.workbook.worksheets.getItem("Sample");
    let range = worksheet.getRange("A1:E1");
    // Apply new style.
    range.style = ("Diagonal Orientation Style");
    range.format.verticalAlignment = "Justify";

    await context.sync();
});

wrapText

指定 Excel 是否将文本换行到对象中。 一个 null 值指示整个区域没有统一的换行设置

wrapText: boolean;

属性值

boolean

注解

API 集:ExcelApi 1.1

方法详细信息

autofitColumns()

根据列中的当前数据更改当前范围的列宽,以达到最佳宽度。

autofitColumns(): void;

返回

void

注解

API 集:ExcelApi 1.2

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/30-events/events-table-changed.yaml

await Excel.run(async (context) => {
    let sheet = context.workbook.worksheets.getItem("Sample");
    let range = sheet.getRange("B7");
    range.values = [[900]];
    range.format.autofitColumns();

    await context.sync();
    console.log("B7 value has been changed.");
});

autofitRows()

根据列中的当前数据,更改当前范围的行高以达到最佳高度。

autofitRows(): void;

返回

void

注解

API 集:ExcelApi 1.2

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/14-conditional-formatting/conditional-formatting-advanced.yaml

function queueCommandsToCreateTemperatureTable(sheet: Excel.Worksheet) {
    let temperatureTable = sheet.tables.add('A1:M1', true);
    temperatureTable.name = "TemperatureTable";
    temperatureTable.getHeaderRowRange().values = [["Category", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]];
    temperatureTable.rows.add(null, [
        ["Avg High", 40, 38, 44, 45, 51, 56, 67, 72, 79, 59, 45, 41],
        ["Avg Low", 34, 33, 38, 41, 45, 48, 51, 55, 54, 45, 41, 38],
        ["Record High", 61, 69, 79, 83, 95, 97, 100, 101, 94, 87, 72, 66],
        ["Record Low", -1, 2, 9, 24, 28, 32, 36, 39, 35, 21, 12, 4]
    ]);
    temperatureTable.getRange().format.autofitColumns();
    temperatureTable.getRange().format.autofitRows();
}

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。

load(options?: Excel.Interfaces.RangeFormatLoadOptions): Excel.RangeFormat;

参数

options
Excel.Interfaces.RangeFormatLoadOptions

为要加载的对象属性提供选项。

返回

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。

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

参数

propertyNames

string | string[]

指定要加载的属性的逗号分隔的字符串或字符串数组。

返回

示例

// Select all of the range's format properties.
await Excel.run(async (context) => { 
    const sheetName = "Sheet1";
    const rangeAddress = "F:G";
    const worksheet = context.workbook.worksheets.getItem(sheetName);
    const range = worksheet.getRange(rangeAddress);
    range.load(["format/*", "format/fill", "format/borders", "format/font"]);
    await context.sync();
    
    console.log(range.format.wrapText);
    console.log(range.format.fill.color);
    console.log(range.format.font.name);
});

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()。

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

参数

propertyNamesAndPaths

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

propertyNamesAndPaths.select 是指定要加载的属性的逗号分隔字符串,以及 propertyNamesAndPaths.expand 指定要加载的导航属性的逗号分隔字符串。

返回

set(properties, options)

同时设置对象的多个属性。 可以传递具有相应属性的纯文本对象,也可以传递另一个相同类型的 API 对象。

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

参数

properties
Excel.Interfaces.RangeFormatUpdateData

一个 JavaScript 对象,其属性与调用该方法的对象的属性同构结构。

options
OfficeExtension.UpdateOptions

提供一个选项,用于在 properties 对象尝试设置任何只读属性时禁止错误。

返回

void

set(properties)

基于现有加载的对象,同时在对象上设置多个属性。

set(properties: Excel.RangeFormat): void;

参数

properties
Excel.RangeFormat

返回

void

toJSON()

覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Excel.RangeFormat 对象是 API 对象, toJSON 但该方法返回一个纯 JavaScript 对象, (类型化为 Excel.Interfaces.RangeFormatData) ,其中包含从原始对象加载的任何子属性的浅层副本。

toJSON(): Excel.Interfaces.RangeFormatData;

返回