需要 Scripting PRO
Health.queryStatisticsCollection() 方法用于按时间区间查询指定 HealthQuantityType 类型的聚合统计数据,例如每天、每周或每月的步数、心率等。它返回一个 HealthStatisticsCollection 实例,其中包含多个按时间间隔对齐的 HealthStatistics 对象。
此方法非常适合:
| 参数名称 | 类型 | 必填 | 说明 |
|---|---|---|---|
quantityType |
HealthQuantityType |
Yes | 要查询的健康数据类型,如 "stepCount"、"heartRate" 等 |
options.startDate |
Date |
No | 查询范围的起始日期,查询结果不包含此日期之前的数据 |
options.endDate |
Date |
No | 查询范围的结束日期,查询结果不包含此日期之后的数据 |
options.strictStartDate |
boolean |
No | 若为 true,仅包含精确从 startDate 开始的区间 |
options.strictEndDate |
boolean |
No | 若为 true,仅包含精确在 endDate 结束的区间 |
options.statisticsOptions |
HealthStatisticsOptions[] 或单个选项 |
No | 指定要计算的统计类型,可包含: "cumulativeSum", "discreteAverage", "discreteMin", "discreteMax", "mostRecent", "duration", "separateBySource" |
options.anchorDate |
Date |
Yes | 用于对齐时间间隔的锚点日期,通常设为当天零点 |
options.intervalComponents |
DateComponents |
Yes | 定义时间间隔,例如每日、每周等。通过 new DateComponents({ day: 1 })、new DateComponents({ weekOfYear: 1 }) 等方式创建 |
返回一个 Promise,解析为 HealthStatisticsCollection 对象。该集合按时间间隔组织,每个区间包含一个 HealthStatistics 实例。
HealthStatistics 对象可能会返回 null。anchorDate 对齐,区间由 intervalComponents 定义。Health.queryStatistics() 方法代替。