#csv->cells(row?: number, rows?: bool, column?: any, columns?: bool, header?: bool, as?: type) → any
它的单元格。单独使用,或与
rows 一起使用时,返回每一行;与 columns 一起使用时,返回每一列。row: 选取一行,从 0 开始计数(-1 表示最后一行);column: 选取一列,可用表头名称或从 0 开始的序号指定。同时指定行和列时,返回二者交叉处的单个单元格;rows 与 column: 一起使用时,返回每一行中该列的单元格。行是以表头行为键的映射,若没有表头则为单元格列表;列是以表头行为键的列表映射,若没有表头则为列表的列表。没有表头时,各行长度可以不同,列操作会跳过长度不够的行。第一行是表头,除非其中每个单元格都是数字;可以用 header 或 not header 来指定。写成数字的单元格会返回为数字。单元格可以用逗号、制表符、分号或数字之间的空格分隔。指定 as 类型时,结果会被检查并转换为该类型。| 参数 | 接受 |
|---|---|
row 可选 | number |
rows 可选 | 标志:rows 或 not rows |
column 可选 | any |
columns 可选 | 标志:columns 或 not columns |
header 可选 | 一个标志:header 或 not header |
as 可选 | type |
参数可以使用命名方式:->cells(…, rows: …, column: …)。
示例
<let name="prices" value="```csv
fruit,price
apple,3
plum,5
```" />
<!-- 'apple': the header line names each row's keys -->
<echo>{ $prices->cells(row: 0).fruit }</echo>
<!-- [3, 5]: the cells under the price header, as numbers -->
<echo>{ $prices->cells(column: 'price') }</echo>
<!-- 5: row 1 is plum, and its price is 5 -->
<echo>{ $prices->cells(row: 1, column: 'price') }</echo>
<!-- { fruit: ['apple', 'plum'], price: [3, 5] }: every column, keyed by its header -->
<echo>{ $prices->cells(columns) }</echo>
<let name="readings" value="```csv
387
942
945
```" />
<!-- 2274: every line is a number, so there's no header and every line is a row -->
<echo>{ $readings->cells(column: 0)->sum() }</echo>
<let name="puzzle" value="```csv
-903 -642 446
200 818
```" />
<!-- [-903, -642, 446]: spaces split the numbers on each line -->
<echo>{ $puzzle->cells(row: 0) }</echo>
<!-- [[-903, 200], [-642, 818], [446]]: the second row is too short for the third column -->
<echo>{ $puzzle->cells(columns) }</echo>$prices = ```csv
fruit,price
apple,3
plum,5
```
// 'apple': the header line names each row's keys
echo $prices->cells(row: 0).fruit
// [3, 5]: the cells under the price header, as numbers
echo $prices->cells(column: 'price')
// 5: row 1 is plum, and its price is 5
echo $prices->cells(row: 1, column: 'price')
// { fruit: ['apple', 'plum'], price: [3, 5] }: every column, keyed by its header
echo $prices->cells(columns)
$readings = ```csv
387
942
945
```
// 2274: every line is a number, so there's no header and every line is a row
echo $readings->cells(column: 0)->sum()
$puzzle = ```csv
-903 -642 446
200 818
```
// [-903, -642, 446]: spaces split the numbers on each line
echo $puzzle->cells(row: 0)
// [[-903, 200], [-642, 818], [446]]: the second row is too short for the third column
echo $puzzle->cells(columns)