Permalink
Browse files

added ExcelSheet::addrToRowCol() and ExcelSheet::rowColToAddr() metho…

…ds for translating Excel cell position to row/column definitions
  • Loading branch information...
1 parent 8d27315 commit f597dc84c829b30bad51b494567c4491e4bcf4e6 @iliaal committed Oct 16, 2012
Showing with 100 additions and 4 deletions.
  1. +1 −4 ChangeLog
  2. +69 −0 excel.c
  3. +30 −0 tests/070.phpt
View
@@ -2,10 +2,7 @@
* Updated INFO section of the extension to show libXL version
* Added new methods (requires LibXL 3.2.4+):
- added ExcelSheet::getTopLeftView() and ExcelSheet::setTopLeftView() methods for getting/setting a view position in the sheet
-
-TODO
- - added two auxiliary methods for converting cell reference to row and column and vice versa
-
+ - added ExcelSheet::addrToRowCol() and ExcelSheet::rowColToAddr() methods for translating Excel cell position to row/column definitions
[2011-10-12] - Version 0.9.6
* Added new methods (requires LibXL 3.2.3+):
View
69 excel.c
@@ -2995,6 +2995,60 @@ EXCEL_METHOD(Sheet, setTopLeftView)
RETURN_TRUE;
}
/* }}} */
+
+/* {{{ proto bool ExcelSheet::rowColToAddr(int row, int col, boolean row_relative, boolean col_relative)
+ Converts row and column to a cell reference. */
+EXCEL_METHOD(Sheet, rowColToAddr)
+{
+ SheetHandle sheet;
+ zval *object = getThis();
+ zend_bool row_relative = 1, col_relative = 1;
+ long row, col;
+ const char *cel_ref;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll|bb", &row, &col, &row_relative, &col_relative) == FAILURE) {
+ RETURN_FALSE;
+ }
+
+ SHEET_FROM_OBJECT(sheet, object);
+
+ cel_ref = xlSheetRowColToAddr(sheet, row, col, row_relative, col_relative);
+ if (!cel_ref) {
+ RETURN_FALSE;
+ }
+ RETURN_STRING(cel_ref, 1);
+}
+/* }}} */
+
+/* {{{ proto bool ExcelSheet::addrToRowCol(string cell_reference)
+ Converts a cell reference to row and column. */
+EXCEL_METHOD(Sheet, addrToRowCol)
+{
+ SheetHandle sheet;
+ zval *object = getThis();
+ char *cell_reference;
+ int cell_reference_len;
+ int row = 0, col = 0, rowRelative = 0, colRelative = 0;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &cell_reference, &cell_reference_len) == FAILURE) {
+ RETURN_FALSE;
+ }
+
+ if (!cell_reference_len) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cell reference cannot be empty");
+ RETURN_FALSE;
+ }
+
+ SHEET_FROM_OBJECT(sheet, object);
+
+ xlSheetAddrToRowCol(sheet, cell_reference, &row, &col, &rowRelative, &colRelative);
+ array_init(return_value);
+ add_assoc_long(return_value, "row", row);
+ add_assoc_long(return_value, "column", col);
+ add_assoc_bool(return_value, "col_relative", colRelative);
+ add_assoc_bool(return_value, "row_relative", rowRelative);
+}
+/* }}} */
#endif
/* {{{ proto void ExcelSheet::setPrintGridlines(bool value)
@@ -4505,6 +4559,19 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_Sheet_setTopLeftView, 0, 0, 2)
ZEND_ARG_INFO(0, row)
ZEND_ARG_INFO(0, column)
ZEND_END_ARG_INFO()
+
+PHP_EXCEL_ARGINFO
+ZEND_BEGIN_ARG_INFO_EX(arginfo_Sheet_rowColToAddr, 0, 0, 2)
+ ZEND_ARG_INFO(0, row)
+ ZEND_ARG_INFO(0, column)
+ ZEND_ARG_INFO(0, row_relative)
+ ZEND_ARG_INFO(0, col_relative)
+ZEND_END_ARG_INFO()
+
+PHP_EXCEL_ARGINFO
+ZEND_BEGIN_ARG_INFO_EX(arginfo_Sheet_addrToRowCol, 0, 0, 1)
+ ZEND_ARG_INFO(0, cell_reference)
+ZEND_END_ARG_INFO()
#endif
PHP_EXCEL_ARGINFO
@@ -4902,6 +4969,8 @@ zend_function_entry excel_funcs_sheet[] = {
#if LIBXL_VERSION >= 0x03020400
EXCEL_ME(Sheet, setTopLeftView, arginfo_Sheet_setTopLeftView, 0)
EXCEL_ME(Sheet, getTopLeftView, arginfo_Sheet_getTopLeftView, 0)
+ EXCEL_ME(Sheet, rowColToAddr, arginfo_Sheet_rowColToAddr, 0)
+ EXCEL_ME(Sheet, addrToRowCol, arginfo_Sheet_addrToRowCol, 0)
#endif
{NULL, NULL, NULL}
};
View
@@ -0,0 +1,30 @@
+--TEST--
+Sheet::addrToRowCol() / Sheet::rowColToAddr()
+--SKIPIF--
+<?php if (!extension_loaded("excel") || !in_array('addrToRowCol', get_class_methods('ExcelSheet'))) print "skip"; ?>
+--FILE--
+<?php
+ $x = new ExcelBook();
+ $s = $x->addSheet("Sheet 1");
+ var_dump($s->write(1, 1, "Test"));
+ $data = $s->rowColToAddr(1,1);
+
+ var_dump($data, $s->addrToRowCol($data));
+ var_dump($s->addrToRowCol(""));
+?>
+--EXPECTF--
+bool(true)
+string(2) "B2"
+array(4) {
+ ["row"]=>
+ int(1)
+ ["column"]=>
+ int(1)
+ ["col_relative"]=>
+ bool(true)
+ ["row_relative"]=>
+ bool(true)
+}
+
+Warning: ExcelSheet::addrToRowCol(): Cell reference cannot be empty in %s on line %d
+bool(false)

0 comments on commit f597dc8

Please sign in to comment.