Skip to content

Commit 81bc959

Browse files
committed
Feat(Exception): work book is not initialized
1 parent 2462b45 commit 81bc959

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

include/xlswriter.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ static inline chart_object *php_vtiful_chart_fetch_object(zend_object *obj) {
109109
#define Z_CHART_P(zv) php_vtiful_chart_fetch_object(Z_OBJ_P(zv));
110110
#define Z_FORMAT_P(zv) php_vtiful_format_fetch_object(Z_OBJ_P(zv));
111111

112+
#define WORKBOOK_NOT_INITIALIZED(xls_object_t) \
113+
do { \
114+
if(obj->write_ptr.workbook == NULL) { \
115+
zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130); \
116+
return; \
117+
} \
118+
} while(0);
119+
112120
#define ROW(range) \
113121
lxw_name_to_row(range)
114122

kernel/excel.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,9 @@ PHP_METHOD(vtiful_xls, addSheet)
243243

244244
xls_object *obj = Z_XLS_P(getThis());
245245

246+
WORKBOOK_NOT_INITIALIZED(obj);
246247
SHEET_LINE_INIT(obj)
247248

248-
if(obj->write_ptr.workbook == NULL) {
249-
zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130);
250-
return;
251-
}
252-
253249
if(zs_sheet_name != NULL) {
254250
sheet_name = ZSTR_VAL(zs_sheet_name);
255251
}
@@ -274,10 +270,7 @@ PHP_METHOD(vtiful_xls, checkoutSheet)
274270

275271
xls_object *obj = Z_XLS_P(getThis());
276272

277-
if(obj->write_ptr.workbook == NULL) {
278-
zend_throw_exception(vtiful_exception_ce, "Please create a file first, use the filename method", 130);
279-
return;
280-
}
273+
WORKBOOK_NOT_INITIALIZED(obj);
281274

282275
if ((sheet_t = workbook_get_worksheet_by_name(obj->write_ptr.workbook, ZSTR_VAL(zs_sheet_name))) == NULL) {
283276
zend_throw_exception(vtiful_exception_ce, "Sheet not fund", 140);
@@ -347,6 +340,8 @@ PHP_METHOD(vtiful_xls, header)
347340

348341
xls_object *obj = Z_XLS_P(getThis());
349342

343+
WORKBOOK_NOT_INITIALIZED(obj);
344+
350345
ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(header), header_l_key, header_value)
351346
type_writer(header_value, 0, header_l_key, &obj->write_ptr, NULL, NULL);
352347
zval_ptr_dtor(header_value);
@@ -368,6 +363,8 @@ PHP_METHOD(vtiful_xls, data)
368363

369364
xls_object *obj = Z_XLS_P(getThis());
370365

366+
WORKBOOK_NOT_INITIALIZED(obj);
367+
371368
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(data), data_r_value)
372369
if(Z_TYPE_P(data_r_value) == IS_ARRAY) {
373370
SHEET_LINE_ADD(obj)
@@ -391,6 +388,8 @@ PHP_METHOD(vtiful_xls, output)
391388

392389
xls_object *obj = Z_XLS_P(getThis());
393390

391+
WORKBOOK_NOT_INITIALIZED(obj);
392+
394393
workbook_file(&obj->write_ptr);
395394

396395
ZVAL_COPY(return_value, file_path);
@@ -428,6 +427,8 @@ PHP_METHOD(vtiful_xls, insertText)
428427

429428
xls_object *obj = Z_XLS_P(getThis());
430429

430+
WORKBOOK_NOT_INITIALIZED(obj);
431+
431432
SHEET_LINE_SET(obj, row);
432433

433434
if (format_handle) {
@@ -459,6 +460,7 @@ PHP_METHOD(vtiful_xls, insertDate)
459460

460461
xls_object *obj = Z_XLS_P(getThis());
461462

463+
WORKBOOK_NOT_INITIALIZED(obj);
462464
SHEET_LINE_SET(obj, row);
463465

464466
if (Z_TYPE_P(data) != IS_LONG) {
@@ -502,6 +504,8 @@ PHP_METHOD(vtiful_xls, insertChart)
502504

503505
xls_object *obj = Z_XLS_P(getThis());
504506

507+
WORKBOOK_NOT_INITIALIZED(obj);
508+
505509
chart_writer(row, column, zval_get_chart(chart_resource), &obj->write_ptr);
506510
}
507511
/* }}} */
@@ -528,6 +532,8 @@ PHP_METHOD(vtiful_xls, insertUrl)
528532

529533
xls_object *obj = Z_XLS_P(getThis());
530534

535+
WORKBOOK_NOT_INITIALIZED(obj);
536+
531537
if (argc == 4) {
532538
url_writer(row, column, &obj->write_ptr, url, zval_get_format(format_handle));
533539
}
@@ -559,6 +565,8 @@ PHP_METHOD(vtiful_xls, insertImage)
559565

560566
xls_object *obj = Z_XLS_P(getThis());
561567

568+
WORKBOOK_NOT_INITIALIZED(obj);
569+
562570
image_writer(image, row, column, width, height, &obj->write_ptr);
563571
}
564572
/* }}} */
@@ -580,6 +588,8 @@ PHP_METHOD(vtiful_xls, insertFormula)
580588

581589
xls_object *obj = Z_XLS_P(getThis());
582590

591+
WORKBOOK_NOT_INITIALIZED(obj);
592+
583593
formula_writer(formula, row, column, &obj->write_ptr);
584594
}
585595
/* }}} */
@@ -598,6 +608,8 @@ PHP_METHOD(vtiful_xls, autoFilter)
598608

599609
xls_object *obj = Z_XLS_P(getThis());
600610

611+
WORKBOOK_NOT_INITIALIZED(obj);
612+
601613
auto_filter(range, &obj->write_ptr);
602614
}
603615
/* }}} */
@@ -617,6 +629,8 @@ PHP_METHOD(vtiful_xls, mergeCells)
617629

618630
xls_object *obj = Z_XLS_P(getThis());
619631

632+
WORKBOOK_NOT_INITIALIZED(obj);
633+
620634
merge_cells(range, data, &obj->write_ptr);
621635
}
622636
/* }}} */
@@ -642,6 +656,8 @@ PHP_METHOD(vtiful_xls, setColumn)
642656

643657
xls_object *obj = Z_XLS_P(getThis());
644658

659+
WORKBOOK_NOT_INITIALIZED(obj);
660+
645661
if (argc == 3) {
646662
set_column(range, width, &obj->write_ptr, zval_get_format(format_handle));
647663
}
@@ -673,6 +689,8 @@ PHP_METHOD(vtiful_xls, setRow)
673689

674690
xls_object *obj = Z_XLS_P(getThis());
675691

692+
WORKBOOK_NOT_INITIALIZED(obj);
693+
676694
if (argc == 3) {
677695
set_row(range, height, &obj->write_ptr, zval_get_format(format_handle));
678696
}

0 commit comments

Comments
 (0)