phpspreadsheet 中文文檔(三) 計算引擎


2019年10月11日13:59:52

使用PhpSpreadsheet計算引擎

執行公式計算

由於PhpSpreadsheet表示內存中的電子表格,因此它還提供公式計算功能。單元格可以是值類型(包含數字或文本),也可以是公式類型(包含可以求值的公式)。例如,該公式=SUM(A1:A10) 計算得出A1,A2,...,A10中的值之和。

要計算公式,可以調用包含公式方法的單元格getCalculatedValue(),例如:

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

如果您在PhpSpreadsheet隨附的發票演示中編寫了以下代碼行,則其評估值為“ 64”:

09-command-line-calculation.png

PhpSpreadsheet公式解析器的另一個不錯的功能是,它可以在插入/刪除行/列時自動調整公式。這是一個例子:

09-formula-in-cell-1.png

您會看到單元格E11中包含的公式是“ SUM(E4:E9)”。現在,當我編寫以下代碼行時,添加了兩個新的產品線:

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

09-formula-in-cell-2.png

你注意到了嗎?以前的單元格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,$ month,$ day,$ hours = 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,$ timeZone)

\ PhpOffice \ PhpSpreadsheet \ Shared \ Date :: excelToTimeStamp($ 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.
TRUE European 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 
筆記

此功能沒有其他注釋

年分會

尚未記錄


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM