Excel.CustomXmlPart class
表示工作簿中的自定义 XML 部件对象。
- 扩展
注解
属性
context | 与 对象关联的请求上下文。 这会将加载项的进程连接到 Office 主机应用程序的进程。 |
id | 自定义 XML 部件的 ID。 |
namespace |
自定义 XML 部件的命名空间 URI。 |
方法
delete() | 删除自定义 XML 部件。 |
get |
获取自定义 XML 部件的完整 XML 内容。 |
load(options) | 将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
load(property |
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 |
set |
设置自定义 XML 部件的完整 XML 内容。 |
toJSON() | 重写 JavaScript |
属性详细信息
context
id
自定义 XML 部件的 ID。
readonly id: string;
属性值
string
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/18-custom-xml-parts/create-set-get-and-delete-custom-xml-parts.yaml
await Excel.run(async (context) => {
// You must have the xmlns attribute to populate the
// CustomXml.namespaceUri property.
const originalXml = "<Reviewers xmlns='http://schemas.contoso.com/review/1.0'><Reviewer>Juan</Reviewer><Reviewer>Hong</Reviewer><Reviewer>Sally</Reviewer></Reviewers>";
const customXmlPart = context.workbook.customXmlParts.add(originalXml);
customXmlPart.load("id");
const xmlBlob = customXmlPart.getXml();
await context.sync();
const readableXml = addLineBreaksToXML(xmlBlob.value);
$("#display-xml").text(readableXml);
// Store the XML part's ID in a setting.
const settings = context.workbook.settings;
settings.add("ContosoReviewXmlPartId", customXmlPart.id);
await context.sync();
});
namespaceUri
方法详细信息
delete()
删除自定义 XML 部件。
delete(): void;
返回
void
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/18-custom-xml-parts/create-set-get-and-delete-custom-xml-parts.yaml
await Excel.run(async (context) => {
const settings = context.workbook.settings;
const xmlPartIDSetting = settings.getItemOrNullObject("ContosoReviewXmlPartId").load("value");
await context.sync();
if (xmlPartIDSetting.value) {
let customXmlPart = context.workbook.customXmlParts.getItem(xmlPartIDSetting.value);
const xmlBlob = customXmlPart.getXml();
customXmlPart.delete();
customXmlPart = context.workbook.customXmlParts.getItemOrNullObject(xmlPartIDSetting.value);
await context.sync();
if (customXmlPart.isNullObject) {
$("#display-xml").text(`The XML part with the id ${xmlPartIDSetting.value} has been deleted.`);
// Delete the unneeded setting too.
xmlPartIDSetting.delete();
} else {
const readableXml = addLineBreaksToXML(xmlBlob.value);
const strangeMessage = `This is strange. The XML part with the id ${xmlPartIDSetting.value} has not been deleted:\n${readableXml}`
$("#display-xml").text(strangeMessage);
}
await context.sync();
}
});
getXml()
获取自定义 XML 部件的完整 XML 内容。
getXml(): OfficeExtension.ClientResult<string>;
返回
OfficeExtension.ClientResult<string>
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/18-custom-xml-parts/create-set-get-and-delete-custom-xml-parts.yaml
await Excel.run(async (context) => {
// You must have the xmlns attribute to populate the
// CustomXml.namespaceUri property.
const originalXml = "<Reviewers xmlns='http://schemas.contoso.com/review/1.0'><Reviewer>Juan</Reviewer><Reviewer>Hong</Reviewer><Reviewer>Sally</Reviewer></Reviewers>";
const customXmlPart = context.workbook.customXmlParts.add(originalXml);
customXmlPart.load("id");
const xmlBlob = customXmlPart.getXml();
await context.sync();
const readableXml = addLineBreaksToXML(xmlBlob.value);
$("#display-xml").text(readableXml);
// Store the XML part's ID in a setting.
const settings = context.workbook.settings;
settings.add("ContosoReviewXmlPartId", customXmlPart.id);
await context.sync();
});
load(options)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()
。
load(options?: Excel.Interfaces.CustomXmlPartLoadOptions): Excel.CustomXmlPart;
参数
提供要加载对象的属性的选项。
返回
load(propertyNames)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()
。
load(propertyNames?: string | string[]): Excel.CustomXmlPart;
参数
- propertyNames
-
string | string[]
逗号分隔的字符串或指定要加载的属性的字符串数组。
返回
load(propertyNamesAndPaths)
将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()
。
load(propertyNamesAndPaths?: {
select?: string;
expand?: string;
}): Excel.CustomXmlPart;
参数
- propertyNamesAndPaths
-
{ select?: string; expand?: string; }
propertyNamesAndPaths.select
是一个逗号分隔的字符串,指定要加载的属性,是 propertyNamesAndPaths.expand
一个逗号分隔的字符串,指定要加载的导航属性。
返回
setXml(xml)
设置自定义 XML 部件的完整 XML 内容。
setXml(xml: string): void;
参数
- xml
-
string
部件的 XML 内容。
返回
void
注解
示例
// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/18-custom-xml-parts/create-set-get-and-delete-custom-xml-parts.yaml
await Excel.run(async (context) => {
const settings = context.workbook.settings;
const xmlPartIDSetting = settings.getItemOrNullObject("ContosoReviewXmlPartId").load("value");
await context.sync();
if (xmlPartIDSetting.value) {
const customXmlPart = context.workbook.customXmlParts.getItem(xmlPartIDSetting.value);
// The setXml method does a whole-for-whole replacement
// of the entire XML.
customXmlPart.setXml("<Reviewers xmlns='http://schemas.contoso.com/review/1.0'><Reviewer>John</Reviewer><Reviewer>Hitomi</Reviewer></Reviewers>");
const xmlBlob = customXmlPart.getXml();
await context.sync();
const readableXml = addLineBreaksToXML(xmlBlob.value);
$("#display-xml").text(readableXml);
await context.sync();
}
});
toJSON()
重写 JavaScript toJSON()
方法,以便在将 API 对象传递给 JSON.stringify()
时提供更有用的输出。
JSON.stringify
(,反过来又调用toJSON
传递给它的 对象的 方法。) 而原始Excel.CustomXmlPart
对象是 API 对象,toJSON
该方法返回一个纯 JavaScript 对象, (类型为 Excel.Interfaces.CustomXmlPartData
) ,其中包含原始对象中任何已加载子属性的浅表副本。
toJSON(): Excel.Interfaces.CustomXmlPartData;