Word.Style class

表示 Word 文档中的样式。

扩展

注解

API 集:WordApi 1.3

使用方

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/28-formatting/manage-styles.yaml

// Applies the specified style to a paragraph.
await Word.run(async (context) => {
  const styleName = (document.getElementById("style-name-to-use") as HTMLInputElement).value;
  if (styleName == "") {
    console.warn("Enter a style name to apply.");
    return;
  }

  const style: Word.Style = context.document.getStyles().getByNameOrNullObject(styleName);
  style.load("isNullObject, nameLocal, type");
  await context.sync();

  if (style.isNullObject) {
    console.warn(`There's no existing style with the name '${styleName}'.`);
  } else if (style.type != Word.StyleType.paragraph) {
    console.log(`The '${styleName}' style isn't a paragraph style.`);
  } else {
    const body: Word.Body = context.document.body;
    body.clear();
    const paragraph: Word.Paragraph = body.insertParagraph(
      "This paragraph uses a custom style.",
      Word.InsertLocation.start
    );
    paragraph.style = style.nameLocal;
    await context.sync();

    console.log(`Applied '${styleName}' to the sample paragraph.`);
  }
});

属性

automaticallyUpdate

指定是否根据所选内容自动重新定义样式。

baseStyle

指定要用作另一个样式的基本格式的现有样式的名称。

borders

指定表示 BorderCollection 样式的所有边框的对象。

builtIn

获取样式是否为内置样式。

context

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

description

获取样式的说明。

font

获取表示 Font 样式的字符格式的对象。

frame

返回表示 Frame 样式的框架格式的对象。

hasProofing

指定拼写和语法检查是否忽略采用此样式格式的文本。

inUse

获取样式是在文档中已修改或应用的内置样式,还是在文档中创建的新样式。

languageId

指定表示 LanguageId 样式语言的值。

languageIdFarEast

为样式指定东亚语言。

linked

获取样式是否为可用于段落和字符格式的链接样式。

linkStyle

指定段落和字符样式之间的链接。

listLevelNumber

返回样式的列表级别。

listTemplate

获取表示 ListTemplate 样式的列表格式的对象。

locked

指定是否无法更改或编辑样式。

nameLocal

获取采用用户语言的样式名称。

nextParagraphStyle

指定要自动应用于在使用样式设置格式的段落之后插入的新段落的样式名称。

noSpaceBetweenParagraphsOfSameStyle

指定是否删除使用相同样式设置格式的段落之间的间距。

paragraphFormat

获取表示 ParagraphFormat 样式的段落设置的对象。

priority

指定优先级。

quickStyle

指定样式是否对应于可用的快速样式。

shading

获取 Shading 表示样式底纹的对象。 不适用于字体样 List 式。

tableStyle

获取 TableStyle 表示可应用于表格的样式属性的对象。

type

获取样式类型。

unhideWhenUsed

指定在文档中使用样式后,是否在“样式”库和 Microsoft Word 的“样式”任务窗格中显示为推荐样式。

visibility

指定样式是否作为推荐样式在“样式”库和“样式”任务窗格中可见。

方法

delete()

删除样式。

linkToListTemplate(listTemplate)

将此样式链接到列表模板,以便样式的格式可以应用于列表。

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 传递给它的对象的方法。) 虽然原始 Word.Style 对象是 API 对象, toJSON 但该方法返回一个纯 JavaScript 对象, (类型化为 Word.Interfaces.StyleData) ,其中包含从原始对象加载的任何子属性的浅层副本。

track()

根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果在调用中 .sync 使用此对象,并且在“.run”批处理的顺序执行之外使用此对象,并且在设置属性或对对象调用方法时收到“InvalidObjectPath”错误,则需要在首次创建对象时将对象添加到跟踪对象集合。 如果此对象是集合的一部分,您还应跟踪父集合。

untrack()

释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 需要在内存释放生效之前调用 context.sync() 。

属性详细信息

automaticallyUpdate

指定是否根据所选内容自动重新定义样式。

automaticallyUpdate: boolean;

属性值

boolean

注解

API 集:WordApiDesktop 1.3

baseStyle

指定要用作另一个样式的基本格式的现有样式的名称。

baseStyle: string;

属性值

string

注解

API 集:WordApi 1.5

注意:WordApi 1.6 中引入了设置 baseStyle 功能。

borders

指定表示 BorderCollection 样式的所有边框的对象。

readonly borders: Word.BorderCollection;

属性值

注解

API 集:WordApiDesktop 1.1

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/28-formatting/manage-styles.yaml

// Gets the outside border properties of the sample style.
await Word.run(async (context) => {
  const sampleStyle: Word.Style = context.document.getStyles().getByName("Sample Style");
  const borders: Word.BorderCollection = sampleStyle.borders;
  borders.load("outsideBorderColor, outsideBorderType, outsideBorderWidth");
  await context.sync();

  console.log("Outside border color:", borders.outsideBorderColor);
  console.log("Outside border type:", borders.outsideBorderType);
  console.log("Outside border width:", borders.outsideBorderWidth);
});

builtIn

获取样式是否为内置样式。

readonly builtIn: boolean;

属性值

boolean

注解

API 集:WordApi 1.5

context

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

context: RequestContext;

属性值

description

注意

此 API 以预览状态提供给开发者,可能根据我们收到的反馈更改。 请勿在生产环境中使用此 API。

获取样式的说明。

readonly description: string;

属性值

string

注解

API 集:WordApi BETA (仅预览版)

font

获取表示 Font 样式的字符格式的对象。

readonly font: Word.Font;

属性值

注解

API 集:WordApi 1.5

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/28-formatting/manage-styles.yaml

// Sets the font color and size of the sample style.
await Word.run(async (context) => {
  const sampleStyle: Word.Style = context.document.getStyles().getByName("Sample Style");
  const font: Word.Font = sampleStyle.font;
  font.color = "#C00000";
  font.size = 20;
  await context.sync();

  console.log("Set the Sample Style font color to red and size to 20 points.");
});

frame

返回表示 Frame 样式的框架格式的对象。

readonly frame: Word.Frame;

属性值

注解

API 集:WordApiDesktop 1.3

hasProofing

指定拼写和语法检查是否忽略采用此样式格式的文本。

hasProofing: boolean;

属性值

boolean

注解

API 集:WordApiDesktop 1.3

inUse

获取样式是在文档中已修改或应用的内置样式,还是在文档中创建的新样式。

readonly inUse: boolean;

属性值

boolean

注解

API 集:WordApi 1.5

languageId

指定表示 LanguageId 样式语言的值。

languageId: Word.LanguageId | "Afrikaans" | "Albanian" | "Amharic" | "Arabic" | "ArabicAlgeria" | "ArabicBahrain" | "ArabicEgypt" | "ArabicIraq" | "ArabicJordan" | "ArabicKuwait" | "ArabicLebanon" | "ArabicLibya" | "ArabicMorocco" | "ArabicOman" | "ArabicQatar" | "ArabicSyria" | "ArabicTunisia" | "ArabicUAE" | "ArabicYemen" | "Armenian" | "Assamese" | "AzeriCyrillic" | "AzeriLatin" | "Basque" | "BelgianDutch" | "BelgianFrench" | "Bengali" | "Bulgarian" | "Burmese" | "Belarusian" | "Catalan" | "Cherokee" | "ChineseHongKongSAR" | "ChineseMacaoSAR" | "ChineseSingapore" | "Croatian" | "Czech" | "Danish" | "Divehi" | "Dutch" | "Edo" | "EnglishAUS" | "EnglishBelize" | "EnglishCanadian" | "EnglishCaribbean" | "EnglishIndonesia" | "EnglishIreland" | "EnglishJamaica" | "EnglishNewZealand" | "EnglishPhilippines" | "EnglishSouthAfrica" | "EnglishTrinidadTobago" | "EnglishUK" | "EnglishUS" | "EnglishZimbabwe" | "Estonian" | "Faeroese" | "Filipino" | "Finnish" | "French" | "FrenchCameroon" | "FrenchCanadian" | "FrenchCongoDRC" | "FrenchCotedIvoire" | "FrenchHaiti" | "FrenchLuxembourg" | "FrenchMali" | "FrenchMonaco" | "FrenchMorocco" | "FrenchReunion" | "FrenchSenegal" | "FrenchWestIndies" | "FrisianNetherlands" | "Fulfulde" | "GaelicIreland" | "GaelicScotland" | "Galician" | "Georgian" | "German" | "GermanAustria" | "GermanLiechtenstein" | "GermanLuxembourg" | "Greek" | "Guarani" | "Gujarati" | "Hausa" | "Hawaiian" | "Hebrew" | "Hindi" | "Hungarian" | "Ibibio" | "Icelandic" | "Igbo" | "Indonesian" | "Inuktitut" | "Italian" | "Japanese" | "Kannada" | "Kanuri" | "Kashmiri" | "Kazakh" | "Khmer" | "Kirghiz" | "Konkani" | "Korean" | "Kyrgyz" | "LanguageNone" | "Lao" | "Latin" | "Latvian" | "Lithuanian" | "MacedonianFYROM" | "Malayalam" | "MalayBruneiDarussalam" | "Malaysian" | "Maltese" | "Manipuri" | "Marathi" | "MexicanSpanish" | "Mongolian" | "Nepali" | "NoProofing" | "NorwegianBokmol" | "NorwegianNynorsk" | "Oriya" | "Oromo" | "Pashto" | "Persian" | "Polish" | "Portuguese" | "PortugueseBrazil" | "Punjabi" | "RhaetoRomanic" | "Romanian" | "RomanianMoldova" | "Russian" | "RussianMoldova" | "SamiLappish" | "Sanskrit" | "SerbianCyrillic" | "SerbianLatin" | "Sesotho" | "SimplifiedChinese" | "Sindhi" | "SindhiPakistan" | "Sinhalese" | "Slovak" | "Slovenian" | "Somali" | "Sorbian" | "Spanish" | "SpanishArgentina" | "SpanishBolivia" | "SpanishChile" | "SpanishColombia" | "SpanishCostaRica" | "SpanishDominicanRepublic" | "SpanishEcuador" | "SpanishElSalvador" | "SpanishGuatemala" | "SpanishHonduras" | "SpanishModernSort" | "SpanishNicaragua" | "SpanishPanama" | "SpanishParaguay" | "SpanishPeru" | "SpanishPuertoRico" | "SpanishUruguay" | "SpanishVenezuela" | "Sutu" | "Swahili" | "Swedish" | "SwedishFinland" | "SwissFrench" | "SwissGerman" | "SwissItalian" | "Syriac" | "Tajik" | "Tamazight" | "TamazightLatin" | "Tamil" | "Tatar" | "Telugu" | "Thai" | "Tibetan" | "TigrignaEritrea" | "TigrignaEthiopic" | "TraditionalChinese" | "Tsonga" | "Tswana" | "Turkish" | "Turkmen" | "Ukrainian" | "Urdu" | "UzbekCyrillic" | "UzbekLatin" | "Venda" | "Vietnamese" | "Welsh" | "Xhosa" | "Yi" | "Yiddish" | "Yoruba" | "Zulu";

属性值

Word.LanguageId | "Afrikaans" | "Albanian" | "Amharic" | "Arabic" | "ArabicAlgeria" | "ArabicBahrain" | "ArabicEgypt" | "ArabicIraq" | "ArabicJordan" | "ArabicKuwait" | "ArabicLebanon" | "ArabicLibya" | "ArabicMorocco" | "ArabicOman" | "ArabicQatar" | "ArabicSyria" | "ArabicTunisia" | "ArabicUAE" | "ArabicYemen" | "Armenian" | "Assamese" | "AzeriCyrillic" | "AzeriLatin" | "Basque" | "BelgianDutch" | "BelgianFrench" | "Bengali" | "Bulgarian" | "Burmese" | "Belarusian" | "Catalan" | "Cherokee" | "ChineseHongKongSAR" | "ChineseMacaoSAR" | "ChineseSingapore" | "Croatian" | "Czech" | "Danish" | "Divehi" | "Dutch" | "Edo" | "EnglishAUS" | "EnglishBelize" | "EnglishCanadian" | "EnglishCaribbean" | "EnglishIndonesia" | "EnglishIreland" | "EnglishJamaica" | "EnglishNewZealand" | "EnglishPhilippines" | "EnglishSouthAfrica" | "EnglishTrinidadTobago" | "EnglishUK" | "EnglishUS" | "EnglishZimbabwe" | "Estonian" | "Faeroese" | "Filipino" | "Finnish" | "French" | "FrenchCameroon" | "FrenchCanadian" | "FrenchCongoDRC" | "FrenchCotedIvoire" | "FrenchHaiti" | "FrenchLuxembourg" | "FrenchMali" | "FrenchMonaco" | "FrenchMorocco" | "FrenchReunion" | "FrenchSenegal" | "FrenchWestIndies" | "FrisianNetherlands" | "Fulfulde" | "GaelicIreland" | "GaelicScotland" | "Galician" | "Georgian" | "German" | "GermanAustria" | "GermanLiechtenstein" | "GermanLuxembourg" | "Greek" | "Guarani" | "Gujarati" | "Hausa" | "Hawaiian" | "Hebrew" | "Hindi" | "Hungarian" | "Ibibio" | "Icelandic" | "Igbo" | "Indonesian" | "Inuktitut" | "Italian" | "Japanese" | "Kannada" | "Kanuri" | "Kashmiri" | "Kazakh" | "Khmer" | "Kirghiz" | "Konkani" | "Korean" | "Kyrgyz" | "LanguageNone" | "Lao" | "Latin" | "Latvian" | "Lithuanian" | "MacedonianFYROM" | "Malayalam" | "MalayBruneiDarussalam" | "Malaysian" | "Maltese" | "Manipuri" | "Marathi" | "MexicanSpanish" | "Mongolian" | "Nepali" | "NoProofing" | "NorwegianBokmol" | "NorwegianNynorsk" | "Oriya" | "Oromo" | "Pashto" | "Persian" | "Polish" | "Portuguese" | "PortugueseBrazil" | "Punjabi" | "RhaetoRomanic" | "Romanian" | "RomanianMoldova" | "Russian" | "RussianMoldova" | "SamiLappish" | "Sanskrit" | "SerbianCyrillic" | "SerbianLatin" | "Sesotho" | "SimplifiedChinese" | "Sindhi" | "SindhiPakistan" | "Sinhalese" | "Slovak" | "Slovenian" | "Somali" | "Sorbian" | "Spanish" | "SpanishArgentina" | "SpanishBolivia" | "SpanishChile" | "SpanishColombia" | "SpanishCostaRica" | "SpanishDominicanRepublic" | "SpanishEcuador" | "SpanishElSalvador" | "SpanishGuatemala" | "SpanishHonduras" | "SpanishModernSort" | "SpanishNicaragua" | "SpanishPanama" | "SpanishParaguay" | "SpanishPeru" | "SpanishPuertoRico" | "SpanishUruguay" | "SpanishVenezuela" | "Sutu" | "Swahili" | "Swedish" | "SwedishFinland" | "SwissFrench" | "SwissGerman" | "SwissItalian" | "Syriac" | "Tajik" | "Tamazight" | "TamazightLatin" | "Tamil" | "Tatar" | "Telugu" | "Thai" | "Tibetan" | "TigrignaEritrea" | "TigrignaEthiopic" | "TraditionalChinese" | "Tsonga" | "Tswana" | "Turkish" | "Turkmen" | "Ukrainian" | "Urdu" | "UzbekCyrillic" | "UzbekLatin" | "Venda" | "Vietnamese" | "Welsh" | "Xhosa" | "Yi" | "Yiddish" | "Yoruba" | "Zulu"

注解

API 集:WordApiDesktop 1.3

languageIdFarEast

为样式指定东亚语言。

languageIdFarEast: Word.LanguageId | "Afrikaans" | "Albanian" | "Amharic" | "Arabic" | "ArabicAlgeria" | "ArabicBahrain" | "ArabicEgypt" | "ArabicIraq" | "ArabicJordan" | "ArabicKuwait" | "ArabicLebanon" | "ArabicLibya" | "ArabicMorocco" | "ArabicOman" | "ArabicQatar" | "ArabicSyria" | "ArabicTunisia" | "ArabicUAE" | "ArabicYemen" | "Armenian" | "Assamese" | "AzeriCyrillic" | "AzeriLatin" | "Basque" | "BelgianDutch" | "BelgianFrench" | "Bengali" | "Bulgarian" | "Burmese" | "Belarusian" | "Catalan" | "Cherokee" | "ChineseHongKongSAR" | "ChineseMacaoSAR" | "ChineseSingapore" | "Croatian" | "Czech" | "Danish" | "Divehi" | "Dutch" | "Edo" | "EnglishAUS" | "EnglishBelize" | "EnglishCanadian" | "EnglishCaribbean" | "EnglishIndonesia" | "EnglishIreland" | "EnglishJamaica" | "EnglishNewZealand" | "EnglishPhilippines" | "EnglishSouthAfrica" | "EnglishTrinidadTobago" | "EnglishUK" | "EnglishUS" | "EnglishZimbabwe" | "Estonian" | "Faeroese" | "Filipino" | "Finnish" | "French" | "FrenchCameroon" | "FrenchCanadian" | "FrenchCongoDRC" | "FrenchCotedIvoire" | "FrenchHaiti" | "FrenchLuxembourg" | "FrenchMali" | "FrenchMonaco" | "FrenchMorocco" | "FrenchReunion" | "FrenchSenegal" | "FrenchWestIndies" | "FrisianNetherlands" | "Fulfulde" | "GaelicIreland" | "GaelicScotland" | "Galician" | "Georgian" | "German" | "GermanAustria" | "GermanLiechtenstein" | "GermanLuxembourg" | "Greek" | "Guarani" | "Gujarati" | "Hausa" | "Hawaiian" | "Hebrew" | "Hindi" | "Hungarian" | "Ibibio" | "Icelandic" | "Igbo" | "Indonesian" | "Inuktitut" | "Italian" | "Japanese" | "Kannada" | "Kanuri" | "Kashmiri" | "Kazakh" | "Khmer" | "Kirghiz" | "Konkani" | "Korean" | "Kyrgyz" | "LanguageNone" | "Lao" | "Latin" | "Latvian" | "Lithuanian" | "MacedonianFYROM" | "Malayalam" | "MalayBruneiDarussalam" | "Malaysian" | "Maltese" | "Manipuri" | "Marathi" | "MexicanSpanish" | "Mongolian" | "Nepali" | "NoProofing" | "NorwegianBokmol" | "NorwegianNynorsk" | "Oriya" | "Oromo" | "Pashto" | "Persian" | "Polish" | "Portuguese" | "PortugueseBrazil" | "Punjabi" | "RhaetoRomanic" | "Romanian" | "RomanianMoldova" | "Russian" | "RussianMoldova" | "SamiLappish" | "Sanskrit" | "SerbianCyrillic" | "SerbianLatin" | "Sesotho" | "SimplifiedChinese" | "Sindhi" | "SindhiPakistan" | "Sinhalese" | "Slovak" | "Slovenian" | "Somali" | "Sorbian" | "Spanish" | "SpanishArgentina" | "SpanishBolivia" | "SpanishChile" | "SpanishColombia" | "SpanishCostaRica" | "SpanishDominicanRepublic" | "SpanishEcuador" | "SpanishElSalvador" | "SpanishGuatemala" | "SpanishHonduras" | "SpanishModernSort" | "SpanishNicaragua" | "SpanishPanama" | "SpanishParaguay" | "SpanishPeru" | "SpanishPuertoRico" | "SpanishUruguay" | "SpanishVenezuela" | "Sutu" | "Swahili" | "Swedish" | "SwedishFinland" | "SwissFrench" | "SwissGerman" | "SwissItalian" | "Syriac" | "Tajik" | "Tamazight" | "TamazightLatin" | "Tamil" | "Tatar" | "Telugu" | "Thai" | "Tibetan" | "TigrignaEritrea" | "TigrignaEthiopic" | "TraditionalChinese" | "Tsonga" | "Tswana" | "Turkish" | "Turkmen" | "Ukrainian" | "Urdu" | "UzbekCyrillic" | "UzbekLatin" | "Venda" | "Vietnamese" | "Welsh" | "Xhosa" | "Yi" | "Yiddish" | "Yoruba" | "Zulu";

属性值

Word.LanguageId | "Afrikaans" | "Albanian" | "Amharic" | "Arabic" | "ArabicAlgeria" | "ArabicBahrain" | "ArabicEgypt" | "ArabicIraq" | "ArabicJordan" | "ArabicKuwait" | "ArabicLebanon" | "ArabicLibya" | "ArabicMorocco" | "ArabicOman" | "ArabicQatar" | "ArabicSyria" | "ArabicTunisia" | "ArabicUAE" | "ArabicYemen" | "Armenian" | "Assamese" | "AzeriCyrillic" | "AzeriLatin" | "Basque" | "BelgianDutch" | "BelgianFrench" | "Bengali" | "Bulgarian" | "Burmese" | "Belarusian" | "Catalan" | "Cherokee" | "ChineseHongKongSAR" | "ChineseMacaoSAR" | "ChineseSingapore" | "Croatian" | "Czech" | "Danish" | "Divehi" | "Dutch" | "Edo" | "EnglishAUS" | "EnglishBelize" | "EnglishCanadian" | "EnglishCaribbean" | "EnglishIndonesia" | "EnglishIreland" | "EnglishJamaica" | "EnglishNewZealand" | "EnglishPhilippines" | "EnglishSouthAfrica" | "EnglishTrinidadTobago" | "EnglishUK" | "EnglishUS" | "EnglishZimbabwe" | "Estonian" | "Faeroese" | "Filipino" | "Finnish" | "French" | "FrenchCameroon" | "FrenchCanadian" | "FrenchCongoDRC" | "FrenchCotedIvoire" | "FrenchHaiti" | "FrenchLuxembourg" | "FrenchMali" | "FrenchMonaco" | "FrenchMorocco" | "FrenchReunion" | "FrenchSenegal" | "FrenchWestIndies" | "FrisianNetherlands" | "Fulfulde" | "GaelicIreland" | "GaelicScotland" | "Galician" | "Georgian" | "German" | "GermanAustria" | "GermanLiechtenstein" | "GermanLuxembourg" | "Greek" | "Guarani" | "Gujarati" | "Hausa" | "Hawaiian" | "Hebrew" | "Hindi" | "Hungarian" | "Ibibio" | "Icelandic" | "Igbo" | "Indonesian" | "Inuktitut" | "Italian" | "Japanese" | "Kannada" | "Kanuri" | "Kashmiri" | "Kazakh" | "Khmer" | "Kirghiz" | "Konkani" | "Korean" | "Kyrgyz" | "LanguageNone" | "Lao" | "Latin" | "Latvian" | "Lithuanian" | "MacedonianFYROM" | "Malayalam" | "MalayBruneiDarussalam" | "Malaysian" | "Maltese" | "Manipuri" | "Marathi" | "MexicanSpanish" | "Mongolian" | "Nepali" | "NoProofing" | "NorwegianBokmol" | "NorwegianNynorsk" | "Oriya" | "Oromo" | "Pashto" | "Persian" | "Polish" | "Portuguese" | "PortugueseBrazil" | "Punjabi" | "RhaetoRomanic" | "Romanian" | "RomanianMoldova" | "Russian" | "RussianMoldova" | "SamiLappish" | "Sanskrit" | "SerbianCyrillic" | "SerbianLatin" | "Sesotho" | "SimplifiedChinese" | "Sindhi" | "SindhiPakistan" | "Sinhalese" | "Slovak" | "Slovenian" | "Somali" | "Sorbian" | "Spanish" | "SpanishArgentina" | "SpanishBolivia" | "SpanishChile" | "SpanishColombia" | "SpanishCostaRica" | "SpanishDominicanRepublic" | "SpanishEcuador" | "SpanishElSalvador" | "SpanishGuatemala" | "SpanishHonduras" | "SpanishModernSort" | "SpanishNicaragua" | "SpanishPanama" | "SpanishParaguay" | "SpanishPeru" | "SpanishPuertoRico" | "SpanishUruguay" | "SpanishVenezuela" | "Sutu" | "Swahili" | "Swedish" | "SwedishFinland" | "SwissFrench" | "SwissGerman" | "SwissItalian" | "Syriac" | "Tajik" | "Tamazight" | "TamazightLatin" | "Tamil" | "Tatar" | "Telugu" | "Thai" | "Tibetan" | "TigrignaEritrea" | "TigrignaEthiopic" | "TraditionalChinese" | "Tsonga" | "Tswana" | "Turkish" | "Turkmen" | "Ukrainian" | "Urdu" | "UzbekCyrillic" | "UzbekLatin" | "Venda" | "Vietnamese" | "Welsh" | "Xhosa" | "Yi" | "Yiddish" | "Yoruba" | "Zulu"

注解

API 集:WordApiDesktop 1.3

linked

获取样式是否为可用于段落和字符格式的链接样式。

readonly linked: boolean;

属性值

boolean

注解

API 集:WordApi 1.5

linkStyle

指定段落和字符样式之间的链接。

linkStyle: Word.Style;

属性值

注解

API 集:WordApiDesktop 1.3

listLevelNumber

返回样式的列表级别。

readonly listLevelNumber: number;

属性值

number

注解

API 集:WordApiDesktop 1.3

listTemplate

获取表示 ListTemplate 样式的列表格式的对象。

readonly listTemplate: Word.ListTemplate;

属性值

注解

API 集:WordApiDesktop 1.1

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/20-lists/manage-list-styles.yaml

// Gets the properties of the specified style.
await Word.run(async (context) => {
  const styleName = (document.getElementById("style-name-to-use") as HTMLInputElement).value;
  if (styleName == "") {
    console.warn("Enter a style name to get properties.");
    return;
  }

  const style: Word.Style = context.document.getStyles().getByNameOrNullObject(styleName);
  style.load("type");
  await context.sync();

  if (style.isNullObject || style.type != Word.StyleType.list) {
    console.warn(`There's no existing style with the name '${styleName}'. Or this isn't a list style.`);
  } else {
    // Load objects to log properties and their values in the console.
    style.load();
    style.listTemplate.load();
    await context.sync();

    console.log(`Properties of the '${styleName}' style:`, style);

    const listLevels = style.listTemplate.listLevels;
    listLevels.load("items");
    await context.sync();

    console.log(`List levels of the '${styleName}' style:`, listLevels);
  }
});

locked

指定是否无法更改或编辑样式。

locked: boolean;

属性值

boolean

注解

API 集:WordApiDesktop 1.3

nameLocal

获取采用用户语言的样式名称。

readonly nameLocal: string;

属性值

string

注解

API 集:WordApi 1.5

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/28-formatting/manage-styles.yaml

// Applies the specified style to a paragraph.
await Word.run(async (context) => {
  const styleName = (document.getElementById("style-name-to-use") as HTMLInputElement).value;
  if (styleName == "") {
    console.warn("Enter a style name to apply.");
    return;
  }

  const style: Word.Style = context.document.getStyles().getByNameOrNullObject(styleName);
  style.load("isNullObject, nameLocal, type");
  await context.sync();

  if (style.isNullObject) {
    console.warn(`There's no existing style with the name '${styleName}'.`);
  } else if (style.type != Word.StyleType.paragraph) {
    console.log(`The '${styleName}' style isn't a paragraph style.`);
  } else {
    const body: Word.Body = context.document.body;
    body.clear();
    const paragraph: Word.Paragraph = body.insertParagraph(
      "This paragraph uses a custom style.",
      Word.InsertLocation.start
    );
    paragraph.style = style.nameLocal;
    await context.sync();

    console.log(`Applied '${styleName}' to the sample paragraph.`);
  }
});

nextParagraphStyle

指定要自动应用于在使用样式设置格式的段落之后插入的新段落的样式名称。

nextParagraphStyle: string;

属性值

string

注解

API 集:WordApi 1.5

注意:WordApi 1.6 中引入了设置 nextParagraphStyle 功能。

noSpaceBetweenParagraphsOfSameStyle

指定是否删除使用相同样式设置格式的段落之间的间距。

noSpaceBetweenParagraphsOfSameStyle: boolean;

属性值

boolean

注解

API 集:WordApiDesktop 1.3

paragraphFormat

获取表示 ParagraphFormat 样式的段落设置的对象。

readonly paragraphFormat: Word.ParagraphFormat;

属性值

注解

API 集:WordApi 1.5

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/28-formatting/paragraph-format.yaml

// Gets the paragraph format properties of a custom paragraph style.
await Word.run(async (context) => {
  const styleName = "SampleParagraphFormat";
  const sampleStyle: Word.Style = context.document.getStyles().getByName(styleName);
  const paragraphFormat: Word.ParagraphFormat = sampleStyle.paragraphFormat;
  const loadOptions: Word.Interfaces.ParagraphFormatLoadOptions = {
    alignment: true,
    firstLineIndent: true,
    keepTogether: true,
    keepWithNext: true,
    leftIndent: true,
    lineSpacing: true,
    rightIndent: true,
    spaceAfter: true,
    spaceBefore: true,
    widowControl: true
  };
  paragraphFormat.load(loadOptions);
  await context.sync();

  const formatData: Word.Interfaces.ParagraphFormatData = paragraphFormat.toJSON();
  console.log("SampleParagraphFormat properties:", formatData);
});

priority

指定优先级。

priority: number;

属性值

number

注解

API 集:WordApi 1.5

quickStyle

指定样式是否对应于可用的快速样式。

quickStyle: boolean;

属性值

boolean

注解

API 集:WordApi 1.5

shading

获取 Shading 表示样式底纹的对象。 不适用于字体样 List 式。

readonly shading: Word.Shading;

属性值

注解

API 集:WordApi 1.6

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/28-formatting/manage-styles.yaml

// Gets the pattern colors and texture of the sample style.
await Word.run(async (context) => {
  const sampleStyle: Word.Style = context.document.getStyles().getByName("Sample Style");
  const shading: Word.Shading = sampleStyle.shading;
  shading.load("backgroundPatternColor, foregroundPatternColor, texture");
  await context.sync();

  console.log("Background pattern color:", shading.backgroundPatternColor);
  console.log("Foreground pattern color:", shading.foregroundPatternColor);
  console.log("Texture:", shading.texture);
});

tableStyle

获取 TableStyle 表示可应用于表格的样式属性的对象。

readonly tableStyle: Word.TableStyle;

属性值

注解

API 集:WordApi 1.6

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/40-tables/manage-custom-style.yaml

// Gets the table style properties and displays them in the form.
const styleName = (document.getElementById("style-name") as HTMLInputElement).value;
if (styleName == "") {
  console.warn("Please input a table style name.");
  return;
}

await Word.run(async (context) => {
  const tableStyle: Word.TableStyle = context.document.getStyles().getByName(styleName).tableStyle;
  tableStyle.load();
  await context.sync();

  if (tableStyle.isNullObject) {
    console.warn(`There's no existing table style with the name '${styleName}'.`);
    return;
  }

  console.log(tableStyle);
  (document.getElementById("alignment") as HTMLInputElement).value = tableStyle.alignment;
  (document.getElementById("allow-break-across-page") as HTMLInputElement).value = tableStyle.allowBreakAcrossPage.toString();
  (document.getElementById("top-cell-margin") as HTMLInputElement).value = `${tableStyle.topCellMargin}`;
  (document.getElementById("bottom-cell-margin") as HTMLInputElement).value = `${tableStyle.bottomCellMargin}`;
  (document.getElementById("left-cell-margin") as HTMLInputElement).value = `${tableStyle.leftCellMargin}`;
  (document.getElementById("right-cell-margin") as HTMLInputElement).value = `${tableStyle.rightCellMargin}`;
  (document.getElementById("cell-spacing") as HTMLInputElement).value = `${tableStyle.cellSpacing}`;
});

type

获取样式类型。

readonly type: Word.StyleType | "Character" | "List" | "Paragraph" | "Table";

属性值

Word.StyleType | "Character" | "List" | "Paragraph" | "Table"

注解

API 集:WordApi 1.5

unhideWhenUsed

指定在文档中使用样式后,是否在“样式”库和 Microsoft Word 的“样式”任务窗格中显示为推荐样式。

unhideWhenUsed: boolean;

属性值

boolean

注解

API 集:WordApi 1.5

visibility

指定样式是否作为推荐样式在“样式”库和“样式”任务窗格中可见。

visibility: boolean;

属性值

boolean

注解

API 集:WordApi 1.5

方法详细信息

delete()

删除样式。

delete(): void;

返回

void

注解

API 集:WordApi 1.5

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/word/28-formatting/manage-styles.yaml

// Deletes the specified custom style.
await Word.run(async (context) => {
  const styleName = (document.getElementById("style-name-to-delete") as HTMLInputElement).value;
  if (styleName == "") {
    console.warn("Enter a style name to delete.");
    return;
  }

  const style: Word.Style = context.document.getStyles().getByNameOrNullObject(styleName);
  style.load("isNullObject");
  await context.sync();

  if (style.isNullObject) {
    console.warn(`There's no existing style with the name '${styleName}'.`);
  } else {
    style.delete();
    await context.sync();

    console.log(`Deleted the custom style '${styleName}'.`);
  }
});

linkToListTemplate(listTemplate)

将此样式链接到列表模板,以便样式的格式可以应用于列表。

linkToListTemplate(listTemplate: Word.ListTemplate): void;

参数

listTemplate
Word.ListTemplate

A ListTemplate 链接到样式。

返回

void

注解

API 集:WordApiDesktop 1.3

load(options)

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

load(options?: Word.Interfaces.StyleLoadOptions): Word.Style;

参数

options
Word.Interfaces.StyleLoadOptions

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

返回

load(propertyNames)

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

load(propertyNames?: string | string[]): Word.Style;

参数

propertyNames

string | string[]

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

返回

load(propertyNamesAndPaths)

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

load(propertyNamesAndPaths?: {
            select?: string;
            expand?: string;
        }): Word.Style;

参数

propertyNamesAndPaths

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

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

返回

set(properties, options)

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

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

参数

properties
Word.Interfaces.StyleUpdateData

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

options
OfficeExtension.UpdateOptions

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

返回

void

set(properties)

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

set(properties: Word.Style): void;

参数

properties
Word.Style

返回

void

toJSON()

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

toJSON(): Word.Interfaces.StyleData;

返回

track()

根据文档中的相应更改来跟踪对象,以便进行自动调整。 此调用是 context.trackedObjects.add (thisObject) 的简写。 如果在调用中 .sync 使用此对象,并且在“.run”批处理的顺序执行之外使用此对象,并且在设置属性或对对象调用方法时收到“InvalidObjectPath”错误,则需要在首次创建对象时将对象添加到跟踪对象集合。 如果此对象是集合的一部分,您还应跟踪父集合。

track(): Word.Style;

返回

untrack()

释放与此对象关联的内存(如果先前已跟踪过)。 此调用是 context.trackedObjects.remove (thisObject) 的简写。 拥有许多跟踪对象会降低主机应用程序的速度,因此请在使用完毕后释放所添加的任何对象。 需要在内存释放生效之前调用 context.sync() 。

untrack(): Word.Style;

返回