CORE

值与验证

OOXML 规范值类型的运行时验证和类型转换

OOXML 为度量值、颜色等数据定义了严格的值类型。@office-open/core 提供验证函数来规范化和验证这些值。

度量值验证器

TWIP 度量值

TWIP(二十分之一磅)是 WordprocessingML 的标准单位。

import { twipsMeasureValue, signedTwipsMeasureValue } from "@office-open/core";

twipsMeasureValue(1440); // "1440"(1 英寸)
signedTwipsMeasureValue(-720); // "-720"

磅度量值

import { hpsMeasureValue, pointMeasureValue, eighthPointMeasureValue } from "@office-open/core";

hpsMeasureValue(24); // 半磅单位的字号
pointMeasureValue(12); // 12 磅
eighthPointMeasureValue(96); // 96 八分之一磅 = 12 磅

通用度量值

接受带单位后缀的值:mmcminptpcpi

import { universalMeasureValue, positiveUniversalMeasureValue } from "@office-open/core";

universalMeasureValue("25.4mm"); // "25.4mm"
universalMeasureValue("1in"); // "1in"
positiveUniversalMeasureValue("72pt"); // "72pt"

数值验证器

函数输入输出
decimalNumber(val)number整数(向下取整)
unsignedDecimalNumber(val)number正整数

十六进制验证器

函数输入说明
hexColorValue(val)string验证十六进制颜色,支持 "auto"
hexBinary(val, length)string, number固定长度的十六进制二进制
longHexNumber(val)string4 字节十六进制(8 字符)
shortHexNumber(val)string2 字节十六进制(4 字符)
uCharHexNumber(val)string1 字节十六进制(2 字符)
import { hexColorValue, shortHexNumber } from "@office-open/core";

hexColorValue("FF0000"); // "#FF0000"
hexColorValue("auto"); // "auto"
shortHexNumber("0013"); // "0013"

百分比

import { percentageValue, measurementOrPercentValue } from "@office-open/core";

percentageValue("50%"); // "50%"
measurementOrPercentValue("25.4mm"); // "25.4mm"
measurementOrPercentValue("100%"); // "100%"

日期和时间

import { dateTimeValue } from "@office-open/core";

dateTimeValue(new Date("2025-01-01")); // "2025-01-01T00:00:00.000Z"

主题常量

ThemeColor

预定义的主题颜色索引,用于颜色引用:

import { ThemeColor } from "@office-open/core";

// ThemeColor.DARK1       — 深色主色
// ThemeColor.LIGHT1      — 浅色主色
// ThemeColor.DARK2       — 深色辅色
// ThemeColor.LIGHT2      — 浅色辅色
// ThemeColor.ACCENT1-6   — 强调色
// ThemeColor.HYPERLINK   — 超链接颜色
// ThemeColor.FOLLOWED_HYPERLINK

ThemeFont

预定义的主题字体索引:

import { ThemeFont } from "@office-open/core";

// ThemeFont.MAJOR_EAST_ASIA
// ThemeFont.MAJOR_ASCII
// ThemeFont.MAJOR_HIGH_ANSI
// ThemeFont.MINOR_EAST_ASIA
// ThemeFont.MINOR_ASCII
// ThemeFont.MINOR_HIGH_ANSI
Copyright © 2026