• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPOffice / PhpSpreadsheet / 30320556871

28 Jul 2026 01:32AM UTC coverage: 97.162% (-0.007%) from 97.169%
30320556871

Pull #4942

github

web-flow
Merge 27f689c77 into 540dfb463
Pull Request #4942: Feature/pivot table read

809 of 836 new or added lines in 12 files covered. (96.77%)

49198 of 50635 relevant lines covered (97.16%)

382.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.64
/src/PhpSpreadsheet/Worksheet/PivotTable/PivotTable.php
1
<?php
2

3
namespace PhpOffice\PhpSpreadsheet\Worksheet\PivotTable;
4

5
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
6
use Stringable;
7

8
/**
9
 * Read-only representation of a pivot table.
10
 *
11
 * This models the metadata of a pivot table that already exists in a loaded
12
 * spreadsheet: its name, where it is placed on the sheet, the cache it draws
13
 * its data from, and the fields on each axis. It does not (yet) recalculate or
14
 * render the pivot, and it does not modify the underlying file - existing pivot
15
 * tables are still written back verbatim from the preserved source XML.
16
 *
17
 * @see PivotCacheDefinition
18
 * @see PivotField
19
 */
20
class PivotTable implements Stringable
21
{
22
    /**
23
     * Pivot table name (the "name" attribute of the pivotTableDefinition).
24
     */
25
    private string $name;
26

27
    /**
28
     * The worksheet this pivot table is placed on.
29
     */
30
    private ?Worksheet $worksheet = null;
31

32
    /**
33
     * Target range on the worksheet occupied by the rendered pivot table,
34
     * taken from location@ref, e.g. "A3:D20".
35
     */
36
    private string $location = '';
37

38
    /**
39
     * The cache definition this pivot table draws its data from.
40
     */
41
    private ?PivotCacheDefinition $cacheDefinition = null;
42

43
    /**
44
     * The pivot fields, in field-index order.
45
     *
46
     * @var PivotField[]
47
     */
48
    private array $fields = [];
49

50
    /**
51
     * True when this pivot table was built in memory (rather than loaded from a
52
     * file) and therefore must have its OOXML parts generated on save. Loaded
53
     * pivot tables keep their original XML and are written back verbatim.
54
     */
55
    private bool $generated = false;
56

57
    public function __construct(string $name = '')
24✔
58
    {
59
        $this->name = $name;
24✔
60
    }
61

62
    public function isGenerated(): bool
17✔
63
    {
64
        return $this->generated;
17✔
65
    }
66

67
    public function setGenerated(bool $generated): self
14✔
68
    {
69
        $this->generated = $generated;
14✔
70

71
        return $this;
14✔
72
    }
73

74
    /**
75
     * Code to execute when this pivot table is unset().
76
     */
77
    public function __destruct()
3✔
78
    {
79
        $this->worksheet = null;
3✔
80
    }
81

82
    public function getName(): string
20✔
83
    {
84
        return $this->name;
20✔
85
    }
86

NEW
87
    public function setName(string $name): self
×
88
    {
NEW
89
        $this->name = $name;
×
90

NEW
91
        return $this;
×
92
    }
93

94
    public function getWorksheet(): ?Worksheet
2✔
95
    {
96
        return $this->worksheet;
2✔
97
    }
98

99
    public function setWorksheet(?Worksheet $worksheet): self
23✔
100
    {
101
        $this->worksheet = $worksheet;
23✔
102

103
        return $this;
23✔
104
    }
105

106
    public function getLocation(): string
15✔
107
    {
108
        return $this->location;
15✔
109
    }
110

111
    public function setLocation(string $location): self
22✔
112
    {
113
        $this->location = $location;
22✔
114

115
        return $this;
22✔
116
    }
117

118
    public function getCacheDefinition(): ?PivotCacheDefinition
16✔
119
    {
120
        return $this->cacheDefinition;
16✔
121
    }
122

123
    public function setCacheDefinition(?PivotCacheDefinition $cacheDefinition): self
22✔
124
    {
125
        $this->cacheDefinition = $cacheDefinition;
22✔
126

127
        return $this;
22✔
128
    }
129

130
    /**
131
     * @return PivotField[]
132
     */
133
    public function getFields(): array
14✔
134
    {
135
        return $this->fields;
14✔
136
    }
137

138
    public function addField(PivotField $field): self
22✔
139
    {
140
        $this->fields[] = $field;
22✔
141

142
        return $this;
22✔
143
    }
144

145
    /**
146
     * Return only the fields placed on the row axis, in order.
147
     *
148
     * @return PivotField[]
149
     */
150
    public function getRowFields(): array
15✔
151
    {
152
        return $this->getFieldsOnAxis(PivotField::AXIS_ROW);
15✔
153
    }
154

155
    /**
156
     * Return only the fields placed on the column axis, in order.
157
     *
158
     * @return PivotField[]
159
     */
160
    public function getColumnFields(): array
15✔
161
    {
162
        return $this->getFieldsOnAxis(PivotField::AXIS_COLUMN);
15✔
163
    }
164

165
    /**
166
     * Return only the fields placed on the page/filter axis, in order.
167
     *
168
     * @return PivotField[]
169
     */
170
    public function getPageFields(): array
14✔
171
    {
172
        return $this->getFieldsOnAxis(PivotField::AXIS_PAGE);
14✔
173
    }
174

175
    /**
176
     * Return only the value/data fields, in order.
177
     *
178
     * @return PivotField[]
179
     */
180
    public function getDataFields(): array
15✔
181
    {
182
        return array_values(
15✔
183
            array_filter(
15✔
184
                $this->fields,
15✔
185
                static fn (PivotField $field): bool => $field->isDataField()
15✔
186
            )
15✔
187
        );
15✔
188
    }
189

190
    /**
191
     * @return PivotField[]
192
     */
193
    private function getFieldsOnAxis(string $axis): array
15✔
194
    {
195
        return array_values(
15✔
196
            array_filter(
15✔
197
                $this->fields,
15✔
198
                static fn (PivotField $field): bool => $field->getAxis() === $axis
15✔
199
            )
15✔
200
        );
15✔
201
    }
202

203
    public function __toString(): string
1✔
204
    {
205
        return $this->name;
1✔
206
    }
207
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc