Phpspreadsheet 中文文档(三) 计算引擎

PhpOffice/PhpSpreadsheet读取和写入Excel PhpSpreadsheet读取和写入Excel 阅读详情

使用PhpSpreadsheet计算引擎

执行公式计算

由于PhpSpreadsheet表示内存中的电子表格,因此它还提供公式计算功能。单元格可以是值类型(包含数字或文本),也可以是公式类型(包含可以求值的公式)。例如,该公式=SUM(A1:A10) 计算得出A1,A2,...,A10中的值之和。

要计算公式,可以调用包含公式方法的单元格getCalculatedValue(),例如:

$spreadsheet->getActiveSheet()->getCell('E11')->getCalculatedValue();

如果您在PhpSpreadsheet随附的发票演示中编写了以下代码行,则其评估值为“ 64”:

PhpSpreadsheet公式解析器的另一个不错的功能是,它可以在插入/删除行/列时自动调整公式。这是一个例子:

您会看到单元格E11中包含的公式是“ SUM(E4:E9)”。现在,当我编写以下代码行时,添加了两个新的产品线:

$spreadsheet->getActiveSheet()->insertNewRowBefore(7, 2);

你注意到了吗?以前的单元格E11中的公式(当我插入2个新行时为E13)更改为“ SUM(E4:E11)”。同样,插入的单元格将复制前一个单元格的样式信息,就像Excel的行为一样。请注意,您可以插入行和列。

计算缓存

一旦计算引擎评估了单元格中的公式后,结果将被缓存,因此,如果您getCalculatedValue()再次调用同一单元格,则结果将从缓存中返回,而不是第二次评估公式。这有助于提高性能,因为就性能和速度而言,评估公式是一项昂贵的操作。

但是,有时您可能不希望这样,也许您已经更改了基础数据,并且需要使用该新数据重新评估相同的公式。

Calculation::getInstance($spreadsheet)->disableCalculationCache();

将禁用计算缓存,并刷新当前计算缓存。

如果您只想刷新缓存,则可以调用

Calculation::getInstance($spreadsheet)->clearCalculationCache();

已知限制

PhpSpreadsheet计算引擎有一些已知的限制。它们中的大多数是由于在执行之前将Excel公式转换为PHP代码的事实。这意味着Excel公式的计算取决于PHP的语言特性。

Xls不支持的功能

并非所有功能都受支持,要获取完整列表,请按名称阅读 功能列表。

运算符优先级

在Excel中+胜过&,就像在普通代数中*胜过一样+。前一条规则不是使用PhpSpreadsheet附带的计算引擎所能找到的。

涉及数字和文字的公式

包含数字和文本的公式可能会产生意外的结果,甚至导致文件内容无法读取。例如,=3+"Hello "期望该公式在Excel中产生错误(#VALUE!)。由于PHP转换"Hello "为数值(零)的事实,该公式的结果被评估为3,而不是错误。这还会导致Excel文档被生成为包含不可读的内容。

公式似乎不是在Excel2003中使用兼容包计算的?

这是兼容包的正常行为,Xlsx正确显示了这一点。使用\PhpOffice\PhpSpreadsheet\Writer\Xls如果你真的需要计算的值,或强制重新计算Excel2003中中。

处理日期和时间值

返回日期和时间值的Excel函数

在Excel中返回日期值的任何Date and Time函数都可以返回Excel时间戳或PHP时间戳或DateTime对象。

脚本可以通过调用\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType() 方法来更改用于返回日期值的数据类型 :

\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType($returnDateType);

以下常量可用于$returnDateType:

  • \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_PHP_NUMERIC
  • \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_PHP_OBJECT
  • \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL

该方法将在成功时返回布尔值True,在失败时返回False(例如,如果为返回日期类型传递了无效值)。

该\PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType() 方法可用于确定此设置的当前值:

$returnDateType = \PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType();

默认值为RETURNDATE_PHP_NUMERIC。

PHP时间戳

如果RETURNDATE_PHP_NUMERIC为Return Date Type设置了返回值,则通过对Excel中的Date和Time函数的任何访问返回到调用脚本的任何日期值将是一个整数值,表示距PHP / Unix基础日期的秒数。PHP / Unix的基准日期(0)在1970年1月1日为美国标准时间00:00。该值可以是正数或负数:因此-3600的值将是1969年12月31日的23:00。而1970年1月1日的+3600值为01:00。这使PHP的日期范围为1901年12月14日至2038年1月19日。

PHP DateTime对象

如果将Return Date Type设置为RETURNDATE_PHP_OBJECT,则通过对Excel中的Date和Time函数的任何访问返回到调用脚本的任何日期值将是一个PHP DateTime对象。

Excel时间戳

如果RETURNDATE_EXCEL为Return Date Type设置了返回值,则通过对Excel中的Date和Time函数的任何访问返回的日期值将是一个浮点值,代表距Excel基本日期的天数。Excel的基本日期由Excel使用的日历决定:Windows 1900或Mac 1904日历。1900年1月1日是Windows 1900日历的基准日期,而1904年1月1日是Mac 1904日历的基准日期。

脚本可以通过调用以下\PhpOffice\PhpSpreadsheet\Shared\Date::setExcelCalendar()方法来更改用于计算Excel日期值的日历 :

\PhpOffice\PhpSpreadsheet\Shared\Date::setExcelCalendar($baseDate);

以下常量可用于$baseDate:

  • \PhpOffice\PhpSpreadsheet\Shared\Date::CALENDAR_WINDOWS_1900
  • \PhpOffice\PhpSpreadsheet\Shared\Date::CALENDAR_MAC_1904

该方法将在成功时返回布尔值True,在失败时返回False(例如,如果传入了无效值)。

该\PhpOffice\PhpSpreadsheet\Shared\Date::getExcelCalendar()方法可用于确定此设置的当前值:

$baseDate = \PhpOffice\PhpSpreadsheet\Shared\Date::getExcelCalendar();

默认值为CALENDAR_WINDOWS_1900。

返回日期/时间值的函数

  • 日期
  • DATEVALUE
  • EDATE
  • EOMONTH
  • 现在
  • 时间
  • 时间值
  • 今天

接受日期和时间值作为参数的Excel函数

作为参数传递给函数的日期值可以是Excel时间戳或PHP时间戳;或DateTime对象;或包含日期值的字符串(例如“ 2009年1月1日”)。PhpSpreadsheet将尝试根据PHP数据类型识别其类型:

整数将被视为PHP / Unix时间戳。实数值(浮点数)将被视为Excel日期/时间戳。任何PHP DateTime对象都将被视为DateTime 对象。任何字符串值(甚至包含直接数字数据的字符串值)都将转换为DateTime用于验证的对象作为基于服务器区域设置的日期值,因此,如果服务器设置为英国,则通过模棱两可的值'07 / 08/2008'将被视为2008年8月7日,而如果服务器为UK,则将其视为2008年7月8日设置为美国。但是,如果您传递的值(例如“ 31/12/2008”)被位于美国的服务器视为错误,但并不明确,则PhpSpreadsheet会尝试将其更正为2008年12月31日。字符串的内容与php DateTime对象实现所识别的任何格式都不匹配strtotime()(可以处理比常规strtotime()函数更广泛的格式),然后该函数将返回#VALUE错误。但是,Excel建议您始终对日期函数使用日期/时间戳,对PhpSpreadsheet的建议也相同:避免字符串,因为结果不可预测。

将数据写入Excel时,将应用相同的原理。包含日期实际值(而不是返回日期值的Excel函数)的单元格始终被写入Excel日期,并在必要时进行转换。如果格式化为日期的单元格包含整数或 DateTime对象值,则将其转换为Excel值以进行写入:如果格式化为日期的单元格包含实数值,则无需进行转换。请注意,字符串值被写为字符串,而不是转换为Excel日期时间戳值。

需要日期/时间值的函数

  • 达蒂夫
  • 天
  • DAYS360
  • EDATE
  • EOMONTH
  • 小时
  • 分钟
  • 月
  • 网络日
  • 第二
  • 平日
  • WEEKNUM
  • 工作日
  • 年
  • 年分会

辅助方法

除了setExcelCalendar()和getExcelCalendar()方法,\PhpOffice\PhpSpreadsheet\Shared\Date该类中还有许多其他方法 可以帮助处理日期:

\ PhpOffice \ PhpSpreadsheet \ Shared \ Date :: excelToTimestamp($ excelDate)

从Excel日期时间戳转换日期/时间以返回PHP序列化的日期/时间戳。

请注意,此方法不会捕获超出PHP日期时间戳有效范围的Excel日期。

\ PhpOffice \ PhpSpreadsheet \ Shared \ Date :: excelToDateTimeObject($ excelDate)

将日期转换为Excel日期/时间戳以返回PHP DateTime 对象。

\ PhpOffice \ PhpSpreadsheet \ Shared \ Date :: PHPToExcel($ PHPDate)

转换PHP序列化的日期/时间戳或PHP DateTime对象以返回Excel日期时间戳。

\ PhpOffice \ PhpSpreadsheet \ Shared \ Date :: formattedPHPToExcel(year,year, month,day,day, hours = 0,minutes=0,minutes=0, seconds = 0)

接受年,月和日值(以及可选的时,分和秒值),并返回Excel日期时间戳值。

时区支持Excel日期时间戳转换

PhpSpreadsheet中日期函数的默认时区为UST(通用标准时间)。如果需要使用其他时区,则可以使用以下方法:

\ PhpOffice \ PhpSpreadsheet \ Shared \ Date :: getDefaultTimezone()

返回PhpSpeadsheet用于处理日期和时间的当前时区值。

\ PhpOffice \ PhpSpreadsheet \ Shared \ Date :: setDefaultTimezone($ timeZone)

将Excel日期时间戳转换的时区设置为$ timeZone,该值必须是有效的PHP DateTimeZone值。返回值是一个布尔值,其中true是成功,false是失败(例如,传递了无效的DateTimeZone值。)

\ PhpOffice \ PhpSpreadsheet \ Shared \ Date :: excelToDateTimeObject(excelDate,excelDate, timeZone)

\ PhpOffice \ PhpSpreadsheet \ Shared \ Date :: excelToTimeStamp(excelDate,excelDate, timeZone)

这些函数支持将时区作为可选的第二个参数。这会将特定时区应用于该函数调用,而不会影响默认的PhpSpreadsheet时区。

功能参考

数据库功能

降级

DAVERAGE函数返回列表或数据库中符合您指定条件的列中单元格的平均值。

句法

DAVERAGE (database, field, criteria)

参量

数据库组成列表或数据库的单元格范围。

数据库是相关数据的列表,其中相关信息的行是记录,数据的列是字段。列表的第一行包含每一列的标签。

字段指示函数中使用数据库的哪一列。

输入列标签为字符串(用双引号引起来),例如“ Age”或“ Yield”,或者输入代表列在列表中位置的数字(不带引号):1为第一列,2用于第二列,依此类推。

条件包含您指定条件的单元格范围。

您可以对criteria参数使用任何范围,只要它包含至少一个列标签和在列标签下方的至少一个单元格(您在其中为该列指定条件)即可。

返回值

float匹配单元格的平均值。

这是统计平均值。

例子

$database = [
    [ 'Tree',  'Height', 'Age', 'Yield', 'Profit' ],
    [ 'Apple',  18,       20,    14,      105.00  ],
    [ 'Pear',   12,       12,    10,       96.00  ],
    [ 'Cherry', 13,       14,     9,      105.00  ],
    [ 'Apple',  14,       15,    10,       75.00  ],
    [ 'Pear',    9,        8,     8,       76.80  ],
    [ 'Apple',   8,        9,     6,       45.00  ],
];

$criteria = [
    [ 'Tree',      'Height', 'Age', 'Yield', 'Profit', 'Height' ],
    [ '="=Apple"', '>10',    NULL,  NULL,    NULL,     '<16'    ],
    [ '="=Pear"',  NULL,     NULL,  NULL,    NULL,     NULL     ],
];

$worksheet->fromArray( $criteria, NULL, 'A1' )
    ->fromArray( $database, NULL, 'A4' );

$worksheet->setCellValue('A12', '=DAVERAGE(A4:E10,"Yield",A1:B2)');

$retVal = $worksheet->getCell('A12')->getCalculatedValue();
// $retVal = 12

笔记

此功能没有其他注释

DCOUNT

DCOUNT函数返回指定列表或数据库中符合条件的列中包含数字的单元格计数。

句法

DCOUNT(database, [field], criteria)

参量

数据库组成列表或数据库的单元格范围。

数据库是相关数据的列表,其中相关信息的行是记录,数据的列是字段。列表的第一行包含每一列的标签。

字段指示函数中使用数据库的哪一列。

输入列标签为字符串(用双引号引起来),例如“ Age”或“ Yield”,或者输入代表列在列表中位置的数字(不带引号):1为第一列,2用于第二列,依此类推。

条件包含您指定条件的单元格范围。

您可以对criteria参数使用任何范围,只要它包含至少一个列标签和在列标签下方的至少一个单元格(您在其中为该列指定条件)即可。

返回值

float匹配单元格的计数。

例子

$database = [
    [ 'Tree',  'Height', 'Age', 'Yield', 'Profit' ],
    [ 'Apple',  18,       20,    14,      105.00  ],
    [ 'Pear',   12,       12,    10,       96.00  ],
    [ 'Cherry', 13,       14,     9,      105.00  ],
    [ 'Apple',  14,       15,    10,       75.00  ],
    [ 'Pear',    9,        8,     8,       76.80  ],
    [ 'Apple',   8,        9,     6,       45.00  ],
];

$criteria = [
    [ 'Tree',      'Height', 'Age', 'Yield', 'Profit', 'Height' ],
    [ '="=Apple"', '>10',    NULL,  NULL,    NULL,     '<16'    ],
    [ '="=Pear"',  NULL,     NULL,  NULL,    NULL,     NULL     ],
];

$worksheet->fromArray( $criteria, NULL, 'A1' )
    ->fromArray( $database, NULL, 'A4' );

$worksheet->setCellValue('A12', '=DCOUNT(A4:E10,"Height",A1:B3)');

$retVal = $worksheet->getCell('A12')->getCalculatedValue();

// $retVal = 3

笔记

在MS Excel中,field参数是可选的。如果省略该字段,则DCOUNT将对数据库中符合条件的所有记录进行计数。PhpSpreadsheet中尚未实现此逻辑。

DCOUNTA

DCOUNT函数返回列表或数据库的列中不为空且匹配您指定条件的单元格计数。

句法

DCOUNTA(database, [field], criteria)

参量

数据库组成列表或数据库的单元格范围。

数据库是相关数据的列表,其中相关信息的行是记录,数据的列是字段。列表的第一行包含每一列的标签。

字段指示函数中使用数据库的哪一列。

输入列标签为字符串(用双引号引起来),例如“ Age”或“ Yield”,或者输入代表列在列表中位置的数字(不带引号):1为第一列,2用于第二列,依此类推。

条件包含您指定条件的单元格范围。

您可以对criteria参数使用任何范围,只要它包含至少一个列标签和在列标签下方的至少一个单元格(您在其中为该列指定条件)即可。

返回值

float匹配单元格的计数。

例子

$database = [
    [ 'Tree',  'Height', 'Age', 'Yield', 'Profit' ],
    [ 'Apple',  18,       20,    14,      105.00  ],
    [ 'Pear',   12,       12,    10,       96.00  ],
    [ 'Cherry', 13,       14,     9,      105.00  ],
    [ 'Apple',  14,       15,    10,       75.00  ],
    [ 'Pear',    9,        8,     8,       76.80  ],
    [ 'Apple',   8,        9,     6,       45.00  ],
];

$criteria = [
    [ 'Tree',      'Height', 'Age', 'Yield', 'Profit', 'Height' ],
    [ '="=Apple"', '>10',    NULL,  NULL,    NULL,     '<16'    ],
    [ '="=Pear"',  NULL,     NULL,  NULL,    NULL,     NULL     ],
];

$worksheet->fromArray( $criteria, NULL, 'A1' )
    ->fromArray( $database, NULL, 'A4' );

$worksheet->setCellValue('A12', '=DCOUNTA(A4:E10,"Yield",A1:A3)');

$retVal = $worksheet->getCell('A12')->getCalculatedValue();

// $retVal = 5

笔记

在MS Excel中,field参数是可选的。如果省略该字段,则DCOUNTA将对数据库中符合条件的所有记录进行计数。PhpSpreadsheet中尚未实现此逻辑。

DGET

DGET函数从与您指定的条件匹配的列表或数据库的列中提取单个值。

句法

DGET(database, field, criteria)

参量

数据库组成列表或数据库的单元格范围。

数据库是相关数据的列表,其中相关信息的行是记录,数据的列是字段。列表的第一行包含每一列的标签。

字段指示函数中使用数据库的哪一列。

输入列标签为字符串(用双引号引起来),例如“ Age”或“ Yield”,或者输入代表列在列表中位置的数字(不带引号):1为第一列,2用于第二列,依此类推。

条件包含您指定条件的单元格范围。

您可以对criteria参数使用任何范围,只要它包含至少一个列标签和在列标签下方的至少一个单元格(您在其中为该列指定条件)即可。

返回值

混合从匹配行的选定列中选择的值。

例子

$database = [
    [ 'Tree',  'Height', 'Age', 'Yield', 'Profit' ],
    [ 'Apple',  18,       20,    14,      105.00  ],
    [ 'Pear',   12,       12,    10,       96.00  ],
    [ 'Cherry', 13,       14,     9,      105.00  ],
    [ 'Apple',  14,       15,    10,       75.00  ],
    [ 'Pear',    9,        8,     8,       76.80  ],
    [ 'Apple',   8,        9,     6,       45.00  ],
];

$criteria = [
    [ 'Tree',      'Height', 'Age', 'Yield', 'Profit', 'Height' ],
    [ '="=Apple"', '>10',    NULL,  NULL,    NULL,     '<16'    ],
    [ '="=Pear"',  NULL,     NULL,  NULL,    NULL,     NULL     ],
];

$worksheet->fromArray( $criteria, NULL, 'A1' )
    ->fromArray( $database, NULL, 'A4' );

$worksheet->setCellValue('A12', '=GET(A4:E10,"Age",A1:F2)');

$retVal = $worksheet->getCell('A12')->getCalculatedValue();
// $retVal = 14

笔记

此功能没有其他注释

DMAX

DMAX函数返回列表或数据库中符合您指定条件的列中的最大数字。

句法

DMAX(database, field, criteria)

参量

数据库组成列表或数据库的单元格范围。

数据库是相关数据的列表,其中相关信息的行是记录,数据的列是字段。列表的第一行包含每一列的标签。

字段指示函数中使用数据库的哪一列。

输入列标签为字符串(用双引号引起来),例如“ Age”或“ Yield”,或者输入代表列在列表中位置的数字(不带引号):1为第一列,2用于第二列,依此类推。

条件包含您指定条件的单元格范围。

您可以对criteria参数使用任何范围,只要它包含至少一个列标签和在列标签下方的至少一个单元格(您在其中为该列指定条件)即可。

返回值

float匹配单元格的最大值。

例子

$database = [
    [ 'Tree',  'Height', 'Age', 'Yield', 'Profit' ],
    [ 'Apple',  18,       20,    14,      105.00  ],
    [ 'Pear',   12,       12,    10,       96.00  ],
    [ 'Cherry', 13,       14,     9,      105.00  ],
    [ 'Apple',  14,       15,    10,       75.00  ],
    [ 'Pear',    9,        8,     8,       76.80  ],
    [ 'Apple',   8,        9,     6,       45.00  ],
];

$criteria = [
    [ 'Tree',      'Height', 'Age', 'Yield', 'Profit', 'Height' ],
    [ '="=Apple"', '>10',    NULL,  NULL,    NULL,     '<16'    ],
    [ '="=Pear"',  NULL,     NULL,  NULL,    NULL,     NULL     ],
];

$worksheet->fromArray( $criteria, NULL, 'A1' )
    ->fromArray( $database, NULL, 'A4' );

$worksheet->setCellValue('A12', '=DMAX(A4:E10,"Profit",A1:B2)');

$retVal = $worksheet->getCell('A12')->getCalculatedValue();
// $retVal = 105

笔记

此功能没有其他注释

DMIN

DMIN函数返回与指定条件匹配的列表或数据库的列中的最小数字。

句法

DMIN(database, field, criteria)

参量

数据库组成列表或数据库的单元格范围。

数据库是相关数据的列表,其中相关信息的行是记录,数据的列是字段。列表的第一行包含每一列的标签。

字段指示函数中使用数据库的哪一列。

输入列标签为字符串(用双引号引起来),例如“ Age”或“ Yield”,或者输入代表列在列表中位置的数字(不带引号):1为第一列,2用于第二列,依此类推。

条件包含您指定条件的单元格范围。

您可以对criteria参数使用任何范围,只要它包含至少一个列标签和在列标签下方的至少一个单元格(您在其中为该列指定条件)即可。

返回值

float匹配单元格的最小值。

例子

$database = [
    [ 'Tree',  'Height', 'Age', 'Yield', 'Profit' ],
    [ 'Apple',  18,       20,    14,      105.00  ],
    [ 'Pear',   12,       12,    10,       96.00  ],
    [ 'Cherry', 13,       14,     9,      105.00  ],
    [ 'Apple',  14,       15,    10,       75.00  ],
    [ 'Pear',    9,        8,     8,       76.80  ],
    [ 'Apple',   8,        9,     6,       45.00  ],
];

$criteria = [
    [ 'Tree',      'Height', 'Age', 'Yield', 'Profit', 'Height' ],
    [ '="=Apple"', '>10',    NULL,  NULL,    NULL,     '<16'    ],
    [ '="=Pear"',  NULL,     NULL,  NULL,    NULL,     NULL     ],
];

$worksheet->fromArray( $criteria, NULL, 'A1' )
    ->fromArray( $database, NULL, 'A4' );

$worksheet->setCellValue('A12', '=DMIN(A4:E10,"Yield",A1:A3)');

$retVal = $worksheet->getCell('A12')->getCalculatedValue();
// $retVal = 6

笔记

此功能没有其他注释

D产品

DPRODUCT函数将匹配您指定条件的列表或数据库的列中的值相乘。

句法

DPRODUCT(database, field, criteria)

参量

数据库组成列表或数据库的单元格范围。

数据库是相关数据的列表,其中相关信息的行是记录,数据的列是字段。列表的第一行包含每一列的标签。

字段指示函数中使用数据库的哪一列。

输入列标签为字符串(用双引号引起来),例如“ Age”或“ Yield”,或者输入代表列在列表中位置的数字(不带引号):1为第一列,2用于第二列,依此类推。

条件包含您指定条件的单元格范围。

您可以对criteria参数使用任何范围,只要它包含至少一个列标签和在列标签下方的至少一个单元格(您在其中为该列指定条件)即可。

返回值

float匹配单元格的乘积。

例子

$database = [
    [ 'Tree',  'Height', 'Age', 'Yield', 'Profit' ],
    [ 'Apple',  18,       20,    14,      105.00  ],
    [ 'Pear',   12,       12,    10,       96.00  ],
    [ 'Cherry', 13,       14,     9,      105.00  ],
    [ 'Apple',  14,       15,    10,       75.00  ],
    [ 'Pear',    9,        8,     8,       76.80  ],
    [ 'Apple',   8,        9,     6,       45.00  ],
];

$criteria = [
    [ 'Tree',      'Height', 'Age', 'Yield', 'Profit', 'Height' ],
    [ '="=Apple"', '>10',    NULL,  NULL,    NULL,     '<16'    ],
    [ '="=Pear"',  NULL,     NULL,  NULL,    NULL,     NULL     ],
];

$worksheet->fromArray( $criteria, NULL, 'A1' )
    ->fromArray( $database, NULL, 'A4' );

$worksheet->setCellValue('A12', '=DPRODUCT(A4:E10,"Yield",A1:B2)');

$retVal = $worksheet->getCell('A12')->getCalculatedValue();
// $retVal = 140

笔记

此功能没有其他注释

DSTDEV

The DSTDEV function estimates the standard deviation of a population based on a sample by using the numbers in a column of a list or database that match conditions that you specify.

Syntax

DSTDEV(database, field, criteria)

Parameters

database The range of cells that makes up the list or database.

A database is a list of related data in which rows of related information are records, and columns of data are fields. The first row of the list contains labels for each column.

field Indicates which column of the database is used in the function.

Enter the column label as a string (enclosed between double quotation marks), such as "Age" or "Yield," or as a number (without quotation marks) that represents the position of the column within the list: 1 for the first column, 2 for the second column, and so on.

criteria The range of cells that contains the conditions you specify.

You can use any range for the criteria argument, as long as it includes at least one column label and at least one cell below the column label in which you specify a condition for the column.

Return Value

float The estimated standard deviation of the matching cells.

Examples

$database = [
    [ 'Tree',  'Height', 'Age', 'Yield', 'Profit' ],
    [ 'Apple',  18,       20,    14,      105.00  ],
    [ 'Pear',   12,       12,    10,       96.00  ],
    [ 'Cherry', 13,       14,     9,      105.00  ],
    [ 'Apple',  14,       15,    10,       75.00  ],
    [ 'Pear',    9,        8,     8,       76.80  ],
    [ 'Apple',   8,        9,     6,       45.00  ],
];

$criteria = [
    [ 'Tree',      'Height', 'Age', 'Yield', 'Profit', 'Height' ],
    [ '="=Apple"', '>10',    NULL,  NULL,    NULL,     '<16'    ],
    [ '="=Pear"',  NULL,     NULL,  NULL,    NULL,     NULL     ],
];

$worksheet->fromArray( $criteria, NULL, 'A1' )
    ->fromArray( $database, NULL, 'A4' );

$worksheet->setCellValue('A12', '=DSTDEV(A4:E10,"Yield",A1:A3)');

$retVal = $worksheet->getCell('A12')->getCalculatedValue();
// $retVal = 2.97

Notes

There are no additional notes on this function

DSTDEVP

The DSTDEVP function calculates the standard deviation of a population based on the entire population by using the numbers in a column of a list or database that match conditions that you specify.

Syntax

DSTDEVP(database, field, criteria)

Parameters

database The range of cells that makes up the list or database.

A database is a list of related data in which rows of related information are records, and columns of data are fields. The first row of the list contains labels for each column.

field Indicates which column of the database is used in the function.

Enter the column label as a string (enclosed between double quotation marks), such as "Age" or "Yield," or as a number (without quotation marks) that represents the position of the column within the list: 1 for the first column, 2 for the second column, and so on.

criteria The range of cells that contains the conditions you specify.

You can use any range for the criteria argument, as long as it includes at least one column label and at least one cell below the column label in which you specify a condition for the column.

Return Value

float The estimated standard deviation of the matching cells.

Examples

$database = [
    [ 'Tree',  'Height', 'Age', 'Yield', 'Profit' ],
    [ 'Apple',  18,       20,    14,      105.00  ],
    [ 'Pear',   12,       12,    10,       96.00  ],
    [ 'Cherry', 13,       14,     9,      105.00  ],
    [ 'Apple',  14,       15,    10,       75.00  ],
    [ 'Pear',    9,        8,     8,       76.80  ],
    [ 'Apple',   8,        9,     6,       45.00  ],
];

$criteria = [
    [ 'Tree',      'Height', 'Age', 'Yield', 'Profit', 'Height' ],
    [ '="=Apple"', '>10',    NULL,  NULL,    NULL,     '<16'    ],
    [ '="=Pear"',  NULL,     NULL,  NULL,    NULL,     NULL     ],
];

$worksheet->fromArray( $criteria, NULL, 'A1' )
    ->fromArray( $database, NULL, 'A4' );

$worksheet->setCellValue('A12', '=DSTDEVP(A4:E10,"Yield",A1:A3)');

$retVal = $worksheet->getCell('A12')->getCalculatedValue();
// $retVal = 2.65

Notes

There are no additional notes on this function

DSUM

The DSUM function adds the numbers in a column of a list or database that matches conditions you specify.

Syntax

DSUM(database, field, criteria)

Parameters

database The range of cells that makes up the list or database.

A database is a list of related data in which rows of related information are records, and columns of data are fields. The first row of the list contains labels for each column.

field Indicates which column of the database is used in the function.

Enter the column label as a string (enclosed between double quotation marks), such as "Age" or "Yield," or as a number (without quotation marks) that represents the position of the column within the list: 1 for the first column, 2 for the second column, and so on.

criteria The range of cells that contains the conditions you specify.

You can use any range for the criteria argument, as long as it includes at least one column label and at least one cell below the column label in which you specify a condition for the column.

Return Value

float The total value of the matching cells.

Examples

$database = [
    [ 'Tree',  'Height', 'Age', 'Yield', 'Profit' ],
    [ 'Apple',  18,       20,    14,      105.00  ],
    [ 'Pear',   12,       12,    10,       96.00  ],
    [ 'Cherry', 13,       14,     9,      105.00  ],
    [ 'Apple',  14,       15,    10,       75.00  ],
    [ 'Pear',    9,        8,     8,       76.80  ],
    [ 'Apple',   8,        9,     6,       45.00  ],
];

$criteria = [
    [ 'Tree',      'Height', 'Age', 'Yield', 'Profit', 'Height' ],
    [ '="=Apple"', '>10',    NULL,  NULL,    NULL,     '<16'    ],
    [ '="=Pear"',  NULL,     NULL,  NULL,    NULL,     NULL     ],
];

$worksheet->fromArray( $criteria, NULL, 'A1' )
    ->fromArray( $database, NULL, 'A4' );

$worksheet->setCellValue('A12', '=DMIN(A4:E10,"Profit",A1:A2)');

$retVal = $worksheet->getCell('A12')->getCalculatedValue();
// $retVal = 225

Notes

There are no additional notes on this function

DVAR

Not yet documented.

DVARP

Not yet documented.

Date and Time Functions

Excel provides a number of functions for the manipulation of dates and times, and calculations based on date/time values. it is worth spending some time reading the section titled "Date and Time Values" on passing date parameters and returning date values to understand how PhpSpreadsheet reconciles the differences between dates and times in Excel and in PHP.

DATE

The DATE function returns an Excel timestamp or a PHP timestamp or DateTime object representing the date that is referenced by the parameters.

Syntax

DATE(year, month, day)

Parameters

year The year number.

If this value is between 0 (zero) and 1899 inclusive (for the Windows 1900 calendar), or between 4 and 1903 inclusive (for the Mac 1904), then PhpSpreadsheet adds it to the Calendar base year, so a value of 108 will interpret the year as 2008 when using the Windows 1900 calendar, or 2012 when using the Mac 1904 calendar.

month The month number.

If this value is greater than 12, the DATE function adds that number of months to the first month in the year specified. For example, DATE(2008,14,2) returns a value representing February 2, 2009.

If the value of month is less than 1, then that value will be adjusted by -1, and that will then be subtracted from the first month of the year specified. For example, DATE(2008,0,2) returns a value representing December 2, 2007; while DATE(2008,-1,2) returns a value representing November 2, 2007.

day The day number.

If this value is greater than the number of days in the month (and year) specified, the DATE function adds that number of days to the first day in the month. For example, DATE(2008,1,35) returns a value representing February 4, 2008.

If the value of day is less than 1, then that value will be adjusted by -1, and that will then be subtracted from the first month of the year specified. For example, DATE(2008,3,0) returns a value representing February 29, 2008; while DATE(2008,3,-2) returns a value representing February 27, 2008.

Return Value

mixed A date/time stamp that corresponds to the given date.

This could be a PHP timestamp value (integer), a PHP DateTime object, or an Excel timestamp value (real), depending on the value of \PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType().

Examples

$worksheet->setCellValue('A1', 'Year')
    ->setCellValue('A2', 'Month')
    ->setCellValue('A3', 'Day');

$worksheet->setCellValue('B1', 2008)
    ->setCellValue('B2', 12)
    ->setCellValue('B3', 31);

$worksheet->setCellValue('D1', '=DATE(B1,B2,B3)');

$retVal = $worksheet->getCell('D1')->getCalculatedValue();
// $retVal = 1230681600
// We're going to be calling the same cell calculation multiple times,
//    and expecting different return values, so disable calculation cacheing
\PhpOffice\PhpSpreadsheet\Calculation\Calculation::getInstance()->setCalculationCacheEnabled(FALSE);

$saveFormat = \PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType();

\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(
    \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL
);

$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATE'],
    [2008, 12, 31]
);
// $retVal = 39813.0

\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(
    \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_PHP_NUMERIC
);

$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATE'],
    [2008, 12, 31]
);
// $retVal = 1230681600

\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType($saveFormat);

Notes

There are no additional notes on this function

DATEDIF

The DATEDIF function computes the difference between two dates in a variety of different intervals, such number of years, months, or days.

Syntax

DATEDIF(date1, date2 [, unit])

Parameters

date1 First Date.

Excel日期值,PHP日期时间戳,PHP DateTime对象或表示为字符串的日期。

date2第二个日期。

Excel日期值,PHP日期时间戳,PHP DateTime对象或表示为字符串的日期。

单位用于计算的间隔类型

这是一个字符串,包含下面列出的值之一:

单元含义描述
米月数在日期之间完成日历月。
d天日期之间的天数。
ÿ年份在日期之间完成日历年。
ym不包括月份的月份在日期之间完成日历月,就好像它们是同一年一样。
码不含年份的天数在日期之间完成日历日,就好像它们是同一年一样。
md不包括年月的天数完成日期之间的日历日,就好像它们是同一月和同一年。

单位值不区分大小写,默认为d。

返回值

整数反映两个日期之间差异的整数值。

这可以是两个日期之间的完整天数,月数或年数,具体取决于传递给函数的间隔单位值作为第三个参数。

例子

$worksheet->setCellValue('A1', 'Year')
    ->setCellValue('A2', 'Month')
    ->setCellValue('A3', 'Day');

$worksheet->setCellValue('B1', 2001)
    ->setCellValue('C1', 2009)
    ->setCellValue('B2', 7)
    ->setCellValue('C2', 12)
    ->setCellValue('B3', 1)
    ->setCellValue('C3', 31);

$worksheet->setCellValue('D1', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"d")')
    ->setCellValue('D2', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"m")')
    ->setCellValue('D3', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"y")')
    ->setCellValue('D4', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"ym")')
    ->setCellValue('D5', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"yd")')
    ->setCellValue('D6', '=DATEDIF(DATE(B1,B2,B3),DATE(C1,C2,C3),"md")');

$retVal = $worksheet->getCell('D1')->getCalculatedValue();
// $retVal = 3105

$retVal = $worksheet->getCell('D2')->getCalculatedValue();
// $retVal = 101

$retVal = $worksheet->getCell('D3')->getCalculatedValue();
// $retVal = 8

$retVal = $worksheet->getCell('D4')->getCalculatedValue();
// $retVal = 5

$retVal = $worksheet->getCell('D5')->getCalculatedValue();
// $retVal = 183

$retVal = $worksheet->getCell('D6')->getCalculatedValue();
// $retVal = 30
$date1 = 1193317015; // PHP timestamp for 25-Oct-2007
$date2 = 1449579415; // PHP timestamp for 8-Dec-2015

$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'],
    [$date1, $date2, 'd']
);
// $retVal = 2966

$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'],
    [$date1, $date2, 'm']
);
// $retVal = 97

$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'],
    [$date1, $date2, 'y']
);
// $retVal = 8

$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'],
    [$date1, $date2, 'ym']
);
// $retVal = 1

$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'],
    [$date1, $date2, 'yd']
);
// $retVal = 44

$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEDIF'],
    [$date1, $date2, 'md']
);
// $retVal = 13

笔记

如果Date1晚于Date2,则DATEDIF将返回#NUM!错误。

DATEVALUE

DATEVALUE函数返回由格式为文本字符串的日期表示的日期。使用DATEVALUE将文本表示的日期转换为序列号。

句法

DATEVALUE(dateString)

参量

date日期字符串。

字符串,表示日期值。

返回值

混合对应于给定日期的日期/时间戳。

根据的值,它可以是PHP时间戳记值(整数),PHP DateTime对象或Excel时间戳记值(实数) \PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType()。

例子

$worksheet->setCellValue('A1', 'Date String');
    ->setCellValue('A2', '31-Dec-2008')
    ->setCellValue('A3', '31/12/2008')
    ->setCellValue('A4', '12-31-2008');

$worksheet->setCellValue('B2', '=DATEVALUE(A2)')
    ->setCellValue('B3', '=DATEVALUE(A3)')
    ->setCellValue('B4', '=DATEVALUE(A4)');

\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(
    \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL
);

$retVal = $worksheet->getCell('B2')->getCalculatedValue();

$retVal = $worksheet->getCell('B3')->getCalculatedValue();

$retVal = $worksheet->getCell('B4')->getCalculatedValue();
// $retVal = 39813.0 for all cases
// We're going to be calling the same cell calculation multiple times,
//    and expecting different return values, so disable calculation cacheing
\PhpOffice\PhpSpreadsheet\Calculation\Calculation::getInstance()->setCalculationCacheEnabled(FALSE);

$saveFormat = \PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType();

\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(
    \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL
);

$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEVALUE'],
    ['31-Dec-2008']
);
// $retVal = 39813.0

\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(
    \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_PHP_NUMERIC
);

$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DATEVALUE'],
    ['31-Dec-2008']
);
// $retVal = 1230681600

\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType($saveFormat);

笔记

DATEVALUE使用的php DateTime对象实现strtotime() (可以处理比常规strtotime() 函数更广泛的格式),并且当参数值是字符串时,也可以为传递给其他日期函数(例如DATEDIF)的任何date参数调用它。

警告: -PhpSpreadsheet比MS Excel接受更广泛的日期格式,因此Excel完全有可能返回#VALUE!传递了无法解释的日期字符串时出错,而PhpSpreadsheet可以将同一字符串转换为正确的日期值。

写入Xls或Xlsx时,在计算中使用字符串格式日期的工作簿中应格外小心。

天

DAY函数返回日期。日期以1到31之间的整数形式给出。

句法

DAY(datetime)

参量

datetime日期。

Excel日期值,PHP日期时间戳,PHP DateTime对象或表示为字符串的日期。

返回值

整数反映月份的整数值。

整数形式,取值范围是1〜31。

例子

$worksheet->setCellValue('A1', 'Date String')
    ->setCellValue('A2', '31-Dec-2008')
    ->setCellValue('A3', '14-Feb-2008');

$worksheet->setCellValue('B2', '=DAY(A2)')
    ->setCellValue('B3', '=DAY(A3)');

$retVal = $worksheet->getCell('B2')->getCalculatedValue();
// $retVal = 31

$retVal = $worksheet->getCell('B3')->getCalculatedValue();
// $retVal = 14
$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DAYOFMONTH'],
    ['25-Dec-2008']
);
// $retVal = 25

笔记

请注意,PhpSpreadsheet函数是 \PhpOffice\PhpSpreadsheet\Calculation\Functions::DAYOFMONTH()静态调用该方法时。

DAYS360

DAYS360函数根据某些会计系统使用的360天年(每个12个相等的期间,每个30天)来计算两个日期之间的差额。

句法

DAYS360(date1, date2 [, method])

参量

date1第一个日期。

Excel日期值,PHP日期时间戳,PHP DateTime对象或表示为字符串的日期。

date2第二个日期。

Excel日期值,PHP日期时间戳,PHP DateTime对象或表示为字符串的日期。

方法布尔标志(TRUE或FALSE)

这是一个标志,用于根据以下列出的值确定要在计算中使用的方法:

方法描述
假U.S. (NASD) method. If the starting date is the last day of a month, it becomes equal to the 30th of the same month. If the ending date is the last day of a month and the starting date is earlier than the 30th of a month, the ending date becomes equal to the 1st of the next month; otherwise the ending date becomes equal to the 30th of the same month.
TRUEEuropean method. Starting dates and ending dates that occur on the 31st of a month become equal to the 30th of the same month.

The method value defaults to FALSE.

Return Value

integer An integer value that reflects the difference between the two dates.

This is the number of full days between the two dates, based on a 360 day year.

Examples

$worksheet->setCellValue('B1', 'Start Date')
    ->setCellValue('C1', 'End Date')
    ->setCellValue('A2', 'Year')
    ->setCellValue('A3', 'Month')
    ->setCellValue('A4', 'Day');

$worksheet->setCellValue('B2', 2003)
    ->setCellValue('B3', 2)
    ->setCellValue('B4', 3);

$worksheet->setCellValue('C2', 2007)
    ->setCellValue('C3', 5)
    ->setCellValue('C4', 31);

$worksheet->setCellValue('E2', '=DAYS360(DATE(B2,B3,B4),DATE(C2,C3,C4))')
    ->setCellValue('E4', '=DAYS360(DATE(B2,B3,B4),DATE(C2,C3,C4),FALSE)');

$retVal = $worksheet->getCell('E2')->getCalculatedValue();
// $retVal = 1558

$retVal = $worksheet->getCell('E4')->getCalculatedValue();
// $retVal = 1557
$date1 = 37655.0; // Excel timestamp for 25-Oct-2007
$date2 = 39233.0; // Excel timestamp for 8-Dec-2015

$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DAYS360'],
    [$date1, $date2]
);
// $retVal = 1558

$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'DAYS360'],
    [$date1, $date2, TRUE]
);
// $retVal = 1557

Notes

警告:-当第三个(可选)参数使用PHP布尔值时(如上例所示),此功能当前不适用于Xls Writer,并且该编写器将生成并出错。如果方法参数使用数字0或1,它将起作用。或者,如果改用Excel TRUE()和FALSE()函数。

EDATE

EDATE函数返回DateTime 代表日期的Excel时间戳或PHP时间戳或对象,该日期是在指定日期(start_date)之前或之后的月份数。使用EDATE来计算到期日或到期日,该到期日或到期日与发行日期在当月的同一天。

句法

EDATE(baseDate, months)

参量

baseDate开始日期。

Excel日期值,PHP日期时间戳,PHP DateTime对象或表示为字符串的日期。

月要添加的月数。

一个整数值,指示baseDate之前或之后的月数。几个月的正值表示将来的日期;负值表示过去的日期。

返回值

混合的日期/时间戳,与基础+月相对应。

根据的值,它可以是PHP时间戳记值(整数),PHP DateTime对象或Excel时间戳记值(实数) \PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType()。

例子

$worksheet->setCellValue('A1', 'Date String')
    ->setCellValue('A2', '1-Jan-2008')
    ->setCellValue('A3', '29-Feb-2008');

$worksheet->setCellValue('B2', '=EDATE(A2,5)')
    ->setCellValue('B3', '=EDATE(A3,-12)');

\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(
    \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL
);

$retVal = $worksheet->getCell('B2')->getCalculatedValue();
// $retVal = 39600.0 (1-Jun-2008)

$retVal = $worksheet->getCell('B3')->getCalculatedValue();
// $retVal = 39141.0 (28-Feb-2007)
\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(
    \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL
);

$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'EDATE'],
    ['31-Oct-2008', 25]
);
// $retVal = 40512.0 (30-Nov-2010)

笔记

警告: -Xls Writer当前不支持此功能,因为它不是Excel 5中的标准功能,而是Analysis ToolPak的外接程序。

EOMONTH

The EOMONTH function returns an Excel timestamp or a PHP timestamp or DateTime object representing the date of the last day of the month that is the indicated number of months before or after a specified date (the start_date). Use EOMONTH to calculate maturity dates or due dates that fall on the last day of the month.

Syntax

EOMONTH(baseDate, months)

Parameters

baseDate Start Date.

An Excel date value, PHP date timestamp, PHP DateTime object, or a date represented as a string.

months Number of months to add.

An integer value indicating the number of months before or after baseDate. A positive value for months yields a future date; a negative value yields a past date.

Return Value

mixed A date/time stamp that corresponds to the last day of basedate + months.

根据的值,它可以是PHP时间戳记值(整数),PHP DateTime对象或Excel时间戳记值(实数) \PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType()。

例子

$worksheet->setCellValue('A1', 'Date String')
    ->setCellValue('A2', '1-Jan-2000')
    ->setCellValue('A3', '14-Feb-2009');

$worksheet->setCellValue('B2', '=EOMONTH(A2,5)')
    ->setCellValue('B3', '=EOMONTH(A3,-12)');

\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(\PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL);

$retVal = $worksheet->getCell('B2')->getCalculatedValue();
// $retVal = 39629.0 (30-Jun-2008)

$retVal = $worksheet->getCell('B3')->getCalculatedValue();
// $retVal = 39507.0 (29-Feb-2008)
\PhpOffice\PhpSpreadsheet\Calculation\Functions::setReturnDateType(
    \PhpOffice\PhpSpreadsheet\Calculation\Functions::RETURNDATE_EXCEL
);

$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'EOMONTH'],
    ['31-Oct-2008', 13]
);
// $retVal = 40147.0 (30-Nov-2010)

笔记

警告: -Xls Writer当前不支持此功能,因为它不是Excel 5中的标准功能,而是Analysis ToolPak的外接程序。

小时

HOUR函数返回时间值的小时。小时以整数形式给出,范围从0(12:00 AM)到23(11:00 PM)。

句法

HOUR(datetime)

参量

datetime时间。

Excel日期/时间值,PHP日期时间戳,PHP DateTime对象或表示为字符串的日期/时间。

返回值

整数反映一天中的小时的整数值。

整数形式,取值范围是0〜23。

例子

$worksheet->setCellValue('A1', 'Time String')
    ->setCellValue('A2', '31-Dec-2008 17:30')
    ->setCellValue('A3', '14-Feb-2008 4:20 AM')
    ->setCellValue('A4', '14-Feb-2008 4:20 PM');

$worksheet->setCellValue('B2', '=HOUR(A2)')
    ->setCellValue('B3', '=HOUR(A3)')
    ->setCellValue('B4', '=HOUR(A4)');

$retVal = $worksheet->getCell('B2')->getCalculatedValue();
// $retVal = 17

$retVal = $worksheet->getCell('B3')->getCalculatedValue();
// $retVal = 4

$retVal = $worksheet->getCell('B4')->getCalculatedValue();
// $retVal = 16
$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'HOUROFDAY'],
    ['09:30']
);
// $retVal = 9

笔记

请注意,PhpSpreadsheet函数是 \PhpOffice\PhpSpreadsheet\Calculation\Functions::HOUROFDAY()静态调用该方法时。

分钟

MINUTE函数返回时间值的分钟。分钟以整数形式给出,范围为0到59。

句法

MINUTE(datetime)

参量

datetime时间。

Excel日期/时间值,PHP日期时间戳,PHP DateTime对象或表示为字符串的日期/时间。

返回值

整数反映小时中分钟的整数值。

整数形式,取值范围是0〜59。

例子

$worksheet->setCellValue('A1', 'Time String')
    ->setCellValue('A2', '31-Dec-2008 17:30')
    ->setCellValue('A3', '14-Feb-2008 4:20 AM')
    ->setCellValue('A4', '14-Feb-2008 4:45 PM');

$worksheet->setCellValue('B2', '=MINUTE(A2)')
    ->setCellValue('B3', '=MINUTE(A3)')
    ->setCellValue('B4', '=MINUTE(A4)');

$retVal = $worksheet->getCell('B2')->getCalculatedValue();
// $retVal = 30

$retVal = $worksheet->getCell('B3')->getCalculatedValue();
// $retVal = 20

$retVal = $worksheet->getCell('B4')->getCalculatedValue();
// $retVal = 45
$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'MINUTE'],
    ['09:30']
);
// $retVal = 30

笔记

请注意,PhpSpreadsheet函数是 \PhpOffice\PhpSpreadsheet\Calculation\Functions::MINUTE()静态调用该方法时。

月

MONTH函数返回日期的月份。月份为1到12之间的整数。

句法

MONTH(datetime)

参量

datetime日期。

Excel日期值,PHP日期时间戳,PHP DateTime对象或表示为字符串的日期。

返回值

integer An integer value that reflects the month of the year.

This is an integer ranging from 1 to 12.

Examples

$worksheet->setCellValue('A1', 'Date String');
$worksheet->setCellValue('A2', '31-Dec-2008');
$worksheet->setCellValue('A3', '14-Feb-2008');

$worksheet->setCellValue('B2', '=MONTH(A2)');
$worksheet->setCellValue('B3', '=MONTH(A3)');

$retVal = $worksheet->getCell('B2')->getCalculatedValue();
// $retVal = 12

$retVal = $worksheet->getCell('B3')->getCalculatedValue();
// $retVal = 2
$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'MONTHOFYEAR'],
    ['14-July-2008']
);
// $retVal = 7

Notes

Note that the PhpSpreadsheet function is \PhpOffice\PhpSpreadsheet\Calculation\Functions::MONTHOFYEAR() when the method is called statically.

NETWORKDAYS

The NETWORKDAYS function returns the number of whole working days between a start date and an end date. Working days exclude weekends and any dates identified in holidays. Use NETWORKDAYS to calculate employee benefits that accrue based on the number of days worked during a specific term.

Syntax

NETWORKDAYS(startDate, endDate [, holidays])

Parameters

startDate Start Date of the period.

An Excel date value, PHP date timestamp, PHP DateTime object, or a date represented as a string.

endDate End Date of the period.

An Excel date value, PHP date timestamp, PHP DateTime object, or a date represented as a string.

假日假日日期的可选数组。

从工作日历中排除的一个或多个日期的可选范围,例如州和联邦假日以及浮动假日。

该列表可以是包含日期的单元格范围,也可以是Excel日期值,PHP日期时间戳,PHP日期对象或表示为字符串的日期的数组常量。

返回值

整数工作天数。

startDate和endDate之间的工作天数。

例子

 
 

笔记

此功能没有其他注释

现在

NOW函数返回当前日期和时间。

句法

NOW()

参量

该NOW()功能没有参数。

返回值

混合的与当前日期和时间相对应的日期/时间戳。

根据的值,它可以是PHP时间戳记值(整数),PHP DateTime对象或Excel时间戳记值(实数) \PhpOffice\PhpSpreadsheet\Calculation\Functions::getReturnDateType()。

例子

 
 

笔记

请注意,PhpSpreadsheet函数是 \PhpOffice\PhpSpreadsheet\Calculation\Functions::DATETIMENOW()静态调用该方法时。

第二

SECOND函数返回时间值的秒数。第二个整数形式,取值范围是0到59。

句法

SECOND(datetime)

参量

datetime时间。

Excel日期/时间值,PHP日期时间戳,PHP DateTime对象或表示为字符串的日期/时间。

返回值

整数反映分钟内秒数的整数值。

整数形式,取值范围是0〜59。

例子

$worksheet->setCellValue('A1', 'Time String')
    ->setCellValue('A2', '31-Dec-2008 17:30:20')
    ->setCellValue('A3', '14-Feb-2008 4:20 AM')
    ->setCellValue('A4', '14-Feb-2008 4:45:59 PM');

$worksheet->setCellValue('B2', '=SECOND(A2)')
    ->setCellValue('B3', '=SECOND(A3)');
    ->setCellValue('B4', '=SECOND(A4)');

$retVal = $worksheet->getCell('B2')->getCalculatedValue();
// $retVal = 20

$retVal = $worksheet->getCell('B3')->getCalculatedValue();
// $retVal = 0

$retVal = $worksheet->getCell('B4')->getCalculatedValue();
// $retVal = 59
$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'SECOND'],
    ['09:30:17']
);
// $retVal = 17

笔记

请注意,PhpSpreadsheet函数是 \PhpOffice\PhpSpreadsheet\Calculation\Functions::SECOND()静态调用该方法时。

时间

尚未记录。

时间值

尚未记录。

今天

尚未记录。

平日

WEEKDAY函数返回给定日期的星期几。该日期以1到7之间的整数形式给出,但可以修改为返回0到6之间的值。

句法

WEEKDAY(datetime [, method])

参量

datetime日期。

Excel日期值,PHP日期时间戳,PHP DateTime对象或表示为字符串的日期。

方法整数标志(值0、1或2)

这是一个标志,用于根据以下列出的值确定要在计算中使用的方法:

method | Description
:-----:|------------------------------------------
0      | Returns 1 (Sunday) through 7 (Saturday).
1      | Returns 1 (Monday) through 7 (Sunday).
2      | Returns 0 (Monday) through 6 (Sunday).

方法值默认为1。

返回值

整数反映星期几的整数值。

这是一个整数,范围是1到7,或0到6,取决于method的值。

例子

$worksheet->setCellValue('A1', 'Date String')
    ->setCellValue('A2', '31-Dec-2008')
    ->setCellValue('A3', '14-Feb-2008');

$worksheet->setCellValue('B2', '=WEEKDAY(A2)')
    ->setCellValue('B3', '=WEEKDAY(A3,0)')
    ->setCellValue('B4', '=WEEKDAY(A3,2)');

$retVal = $worksheet->getCell('B2')->getCalculatedValue();
// $retVal = 12

$retVal = $worksheet->getCell('B3')->getCalculatedValue();
// $retVal = 2

$retVal = $worksheet->getCell('B4')->getCalculatedValue();
// $retVal = 2
$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'WEEKDAY'],
    ['14-July-2008']
);
// $retVal = 7

笔记

请注意,PhpSpreadsheet函数是 \PhpOffice\PhpSpreadsheet\Calculation\Functions::WEEKDAY()静态调用该方法时。

WEEKNUM

尚未记录。

工作日

尚未记录。

年

YEAR函数返回日期的年份。

句法

YEAR(datetime)

参量

datetime日期。

Excel日期值,PHP日期时间戳,PHP DateTime对象或表示为字符串的日期。

返回值

整数反映一年中月份的整数值。

这是整数年值。

例子

$worksheet->setCellValue('A1', 'Date String')
    ->setCellValue('A2', '17-Jul-1982')
    ->setCellValue('A3', '16-Apr-2009');

$worksheet->setCellValue('B2', '=YEAR(A2)')
    ->setCellValue('B3', '=YEAR(A3)');

$retVal = $worksheet->getCell('B2')->getCalculatedValue();
// $retVal = 1982

$retVal = $worksheet->getCell('B3')->getCalculatedValue();
// $retVal = 2009
$retVal = call_user_func_array(
    ['\PhpOffice\PhpSpreadsheet\Calculation\Functions', 'YEAR'],
    ['14-July-2001']
);
// $retVal = 2001
PHP使用PhpSpreadsheet操作Excel 一、PhpSpreadsheet 介绍 1、PhpSpreadsheet 是什么 PhpSpreadsheet是一个用纯PHP编写的库,提供了一组类,使您可以读取和写入不同的电子表格文件格式 PhpSpreadsheet提供了丰富的API接口,可以设置诸多单元格以及文档属性,包括样式、图片、日期、函数等等诸多应用,总之你想要什么样的Excel表格,PhpSpreadsheet都能做到 使用 Ph... 阅读详情

相关推荐

PhpSpreadsheet一个读取和写入电子表格文件的纯PHP库

PhpSpreadsheet一个读取和写入电子表格文件的纯 PHP 库。允许您读取和写入不同的电子表格文件格式,如 Excel 和 LibreOffice Calc。

Phpspreadsheet 中文文档(七)技巧和诀窍

您也可以折叠列。请注意,您还应该将列设置为不可见,否则折叠将在Excel 2007中不可见。请参阅“分组/概述一行”部分以获取有关折叠的完整示例。您可以指示PhpSpreadsheet将摘要添加到右侧(默认)或左侧。您也可以折叠该行。请注意,您还应该将行设置为不可见,否则折叠将在Excel 2007中不可见。$i

withoutfear的博客 3264

gem5 prefetcher

最近在gem5上做预取实验,添加自己的预取算法,这里采用hardware stream prefetcher , 修改了几个bug才给实验调试通过,发文记录下实验过程。 gem5上添加自己的预取算法步骤: (1)路径gem5-master/configs/common/Caches.py下,开启预取:class L1Cache(Cache): assoc = 2 tag_late

编程邦 3845

PhpSpreadsheet中文文档 | 简介

1、PhpSpreadsheet 是什么 PhpSpreadsheet是一个用纯PHP编写的库,提供了一组类,使您可以读取和写入不同的电子表格文件格式 PhpSpreadsheet提供了丰富的API接口,可以设置诸多单元格以及文档属性,包括样式、图片、日期、函数等等诸多应用,总之你想要什么样的Excel表格,PhpSpreadsheet都能做到 使用 PhpSpreadsheet 开发的PHP要求 7.1或更高版本 PhpSpreadsheet 支持链式操作 2、PhpSpreadsheet 支持的文件格式

jackbon8的博客 4万+

HDI盲埋孔设计规范:高速PCB信号完整性落地指南

盲埋孔是高密度互连(HDI)PCB实现高速信号完整性的关键技术,其本质是通过控制过孔stub长度、优化回流路径和提升布线密度,解决GHz级信号在传统通孔结构下出现的谐振、串扰与EMI超标问题。其设计核心在于叠层物理约束、电气参数建模与工艺能力协同,涉及stub长度计算、反焊盘尺寸推导、孔壁粗糙度Rz控制等关键指标。广泛应用于DDR4、PCIe Gen4、USB3.2等高速接口的PCB设计中,尤其在车载ADAS、5G基站及FPGA载板等对SI/PI要求严苛的场景中成为刚性门槛。

weixin_34416754的博客 437

Java Swing之多文档界面设计-JDesktopPane、JInternalFrame类简单应用示例

Java MDI多文档界面是一种用户界面风格,允许在一个主窗口中包含多个子窗口(文档窗口),每个子窗口可以显示不同的内容或功能模块,从而可以满足用户打开不同窗口完成不同功能的需求。开发多文档程序,一般需要JDesktopPane(桌面面板类)和JInternalFrame(内部窗体类)两个类。以下简单介绍,最后通过一个实例介绍实现逻辑。

编程语言教学工作者的博客 2209

phpspreadsheet 中文文档(三) 计算引擎

2019年10月11日13:59:52 使用PhpSpreadsheet计算引擎 执行公式计算 由于PhpSpreadsheet表示内存中的电子表格,因此它还提供公式计算功能。单元格可以是值类型(包含数字或文本),也可以是公式类型(包含可以求值的公式)。例如,该公式=SUM(A1:A10)计算得出A1,A2,...,A10中的值之和。 要计算公式,可以调用包含公式方法的单元格getCalcul...

Z.X的博客 373

【STM32G474实战】RS485通信的DMA优化策略与性能对比

本文深入探讨了在STM32G474微控制器上优化RS485通信的DMA策略。通过对比轮询、中断和DMA三种数据传输方式的代码实现与性能,揭示了DMA在解放CPU、降低占用率方面的巨大优势。文章提供了详细的DMA配置指南、高级应用技巧(如结合空闲中断)以及一个高可靠通信框架的实战思路,旨在帮助开发者构建高效、实时的工业控制系统。

weixin_29268331的博客 304

phpspreadsheet 中文文档 粗翻版

http://get.ftqq.com/9211.get 官方使用文档https://phpspreadsheet.readthedocs.io/en/stable/topics/accessing-cells/ api文档https://phpoffice.github.io/PhpSpreadsheet/master/PhpOffice.html 翻译的是使用文档,机翻+校验,因为现在单个文档内容放的东西有限分成几篇文章 只翻译标题的主要部分 文档部分主要的菜单↓↓ Ac...

jshyzx1999的博客 365

uniapp - 解决真机调试报错 request:fail abort statusCode:-1 java.security.cert.CertPathValidatorException: Tr

uni-app,真机调试报错,app端平台,安卓Android,安卓App发布线上报错,打包后接口请求失效了,api接口报错,真机运行安卓app时接口不通且报错,request:failabortstatusCode:-1java.security.cert.CertPathValidatorException:Trustanchorforcertificationpathnotfound.,真机调试,苹果ios真机报错,Uniapp打包到app后报错,uni-app真机调试报错,服务器

🏆 前端领域优质创作者 1万+

卷积神经网络中的自注意力机制(Self-Attention Mechanism)

卷积神经网络中的机制表现为操作,其实现过程与Seq2Seq模型的自注意力机制类似。标准的卷积层是一种局部滤波操作,其输出特征上的任意位置是由对应输入特征的一个邻域构造的,只能捕捉局部特征之间的关系。而自注意力机制通过计算任意两个位置之间的关系直接捕捉远程依赖,而不用局限于相邻点,相当于构造了一个的卷积核,从而可以捕捉更多信息。在卷积网络的自注意力机制中,首先构造输入特征x的键特征fx, 查询特征gx和值特征hx;

AI天才研究院 1501

给老 Mac 免费升级 macOS:OCLP 从 2008 年机型到 Sequoia 15 落地指南

**OpenCore Legacy Patcher**(OCLP)是一个免费开源的 Python 工具,能帮你把 **macOS Big Sur 11** 到 **Sequoia 15** 装进 2008 年起的老 Intel Mac。它靠 **OpenCore** 引导加载器加 **Lilu** 补丁框架,在内存里绕过苹果的机型校验,不碰固件,也不改主板。对不支持新系统的机器,它是目前最稳的升级

gitblog_00765的博客 618

AI人工智能领域持续学习:提升技术能力的关键

在当今科技飞速发展的时代,AI人工智能已经渗透到我们生活的方方面面。本文章的目的就是帮助大家了解在AI人工智能领域持续学习的重要性,以及如何通过持续学习来提升自己的技术能力。文章会涵盖AI的核心概念、持续学习的原理和方法、实际应用场景等方面,让大家对AI领域的持续学习有一个全面的认识。本文首先会介绍AI人工智能的核心概念,让大家对这个领域有一个初步的了解。然后详细讲解持续学习的原理和方法,通过代码示例展示如何在实际项目中实现持续学习。接着会分析AI持续学习在不同场景的应用,推荐一些学习工具和资源。

AI 原生应用开发的博客 1289

PhpSpreadsheet Excel 函数映射表全解:公式与 PHP 计算实现类的完整对照指南

PhpSpreadsheet 是一套纯 PHP 实现的电子表格读写库,其计算引擎内置了数百个与 Excel 同名同义的公式函数。本文所基于的 [function-list-by-name-compact.md](https://link.gitcode.com/i/cbfc53f99e5e84a3560a2d8726ed8238) 是该项目的"函数注册表"——它以紧凑表格的形式,逐一列出了每个 E

gitblog_00995的博客 492

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

PhpSpreadsheet 计算引擎完全指南:公式求值、缓存管理与日期时间处理

本文是 PhpSpreadsheet 计算引擎(Calculation Engine)的完整技术指南。PhpSpreadsheet 将整个电子表格保存在内存中,因此它不仅能够读写文件,还能像 Excel 一样对单元格中的公式进行实时求值。本文覆盖公式求值 API(`getCalculatedValue()`)、插入/删除行列时的公式自动调整、计算缓存控制、引擎的已知限制,以及 Excel 与 PH

gitblog_00528的博客 60

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

PhpSpreadsheet完全指南:纯PHP读写Excel文件的终极选择

PhpSpreadsheet 是一款纯 PHP 编写的电子表格处理库,无需安装 Excel 或任何办公软件,即可在 PHP 项目中**读取和写入 Excel 文件**,支持 XLSX、XLS、ODS、CSV、HTML 等十余种格式。无论你是想生成报表、解析用户上传的 Excel,还是做数据批量处理,它都是 PHP 生态中最成熟的选择之一。本文带你从零上手,快速掌握 PhpSpreadsheet 读

gitblog_01101的博客 710

PhpSpreadsheet常见问题与实用食谱:10个高频难题的最优解清单

PhpSpreadsheet 是一个纯 PHP 编写的电子表格处理库,可读取和写入 Excel(XLSX/XLS)、ODS、CSV 等多种表格文件。本文精选了新手在社区和官方 FAQ 中遇到的 10 个最高频难题,逐一给出可直接套用的最优解方案。 [![PhpSpreadsheet 电子表格公式计算示例:水果采购清单与求和结果](https://raw.gitcode.com/gh_mirror

gitblog_00841的博客 947

如何搞定Excel日期难题:PhpSpreadsheet日期时间处理实战指南

**PhpSpreadsheet** 是一个纯 PHP 的 Excel 读写库,本文带你用它轻松搞定 **Excel 日期处理**:把年月日转成 Excel 序列号、把文本日期识别成真日期、自定义日期格式、计算工作日与账期。跟着这份实战指南,5 分钟告别日期报错难题 📅。 ## 为什么 Excel 日期总是"坑"不断? 在 Excel 里,日期本质上只是一个**序列号**:`1` 代表 19

gitblog_00651的博客 1080

PhpSpreadsheet 函数全索引:Excel 公式函数名、分类与 PHP 实现映射完全指南

本篇技术指南围绕 PhpSpreadsheet(一个纯 PHP 的电子表格读写与计算库)的函数清单展开,逐项解析 Excel 公式函数(如 `SUM`、`VLOOKUP`、`IRR`、`NORM.DIST`)在 PhpSpreadsheet 计算引擎中的分类归属与底层 PHP 实现位置。读完本文,你将能够:读懂 `docs/references/function-list-by-name.md`

gitblog_00304的博客 236

php iofactory列,phpspreadsheet 中文文档 粗翻版

2019年10月11日09:32:33翻译的是使用文档,机翻+校验,因为现在单个文档内容放的东西有限分成几篇文章只翻译标题的主要部分文档部分主要的菜单↓↓Accessing cells 访问单元格Setting a cell value by coordinate 通过坐标设置单元格值Excel Da...

weixin_32059869的博客 453

PhpSpreadsheet架构深度解析:内存电子表格与Reader/Writer设计完整指南

PhpSpreadsheet 是一个纯 PHP 编写的电子表格读写库,它把 Excel 文件变成内存中的对象模型:你面对的不再是一堆二进制数据,而是由 **Spreadsheet(工作簿)→ Worksheet(工作表)→ Cell(单元格)** 组成的内存电子表格。本文将从源码角度带你理解两大核心设计——内存电子表格对象模型,以及负责文件读写的 Reader/Writer 抽象层,帮你快速建立阅

gitblog_00363的博客 318

PhpSpreadsheet 表格(Excel Table)完整指南:创建、样式、结构化引用与读写支持

本文围绕 PhpSpreadsheet 的"表格(Table)"功能展开,介绍如何将工作表中一组相关的单元格区域转换为 Excel 表格(即旧版所称的 Excel 列表),从而获得结构化引用、自动筛选、汇总行与主题样式等能力。读完本文,你将掌握 `Table`、`TableStyle`、`Table\Column` 三大核心类的完整用法,能够在 Xlsx 读写与 Html 输出两条链路中正确配置表

gitblog_00743的博客 418

PHPExcel 演进史:从 1.0.0 到 1.8.1 的技术遗产与 PhpSpreadsheet 的传承

本篇文章以仓库根目录下的 [CHANGELOG.PHPExcel.md](https://link.gitcode.com/i/83a06ed01daa41fd0165d0dab083ec2f) 为骨架,系统梳理 PhpSpreadsheet 的前身 PHPExcel 从 2006 年初始版本到 2015 年 1.8.1 的完整演进历程。读者将从中看到现代 PhpSpreadsheet 中文件格式

gitblog_06807的博客 215
上一篇: Phpspreadsheet 中文文档(二) 结构+自动筛选
下一篇: Phpspreadsheet 中文文档(四) 创建电子表格+档案格式
withoutfear
博客等级 码龄13年 541粉丝 79原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值