-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sheet.c
100 lines (80 loc) · 2.43 KB
/
sheet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <Python.h>
#include "xlsxwriter.h"
#include "sheet.h"
static int
init(PyXLSXSheet *self)
{
self->handler = NULL;
self->book = NULL;
return 0;
}
static void
dealloc(PyXLSXSheet *self)
{
PyObject_Free(self);
}
static PyObject *
write_string(PyXLSXSheet *self, PyObject *args)
{
int row, col;
const char *val;
if(!PyArg_ParseTuple(args, "iis", &row, &col, &val)) return NULL;
worksheet_write_string(self->handler, row, col, val, NULL);
Py_RETURN_TRUE;
}
static PyObject *
write_number(PyXLSXSheet *self, PyObject *args)
{
int row, col;
double val;
if(!PyArg_ParseTuple(args, "iid", &row, &col, &val)) return NULL;
worksheet_write_number(self->handler, row, col, val, NULL);
Py_RETURN_TRUE;
}
static PyObject *
write_boolean(PyXLSXSheet *self, PyObject *args)
{
int row, col, val;
if(!PyArg_ParseTuple(args, "iii", &row, &col, &val)) return NULL;
worksheet_write_boolean(self->handler, row, col, val, NULL);
Py_RETURN_TRUE;
}
static PyObject *
write_datetime(PyXLSXSheet *self, PyObject *args)
{
int row, col, year, month, day, hour, minute;
double second;
if(!PyArg_ParseTuple(args, "iiiiiiid", &row, &col, &year, &month, &day, &hour, &minute, &second)) return NULL;
lxw_datetime datetime = {year, month, day, hour, minute, second};
lxw_format *format = workbook_add_format(self->book);
format_set_num_format(format, "mmm d yyyy hh:mm AM/PM");
worksheet_write_datetime(self->handler, row, col, &datetime, format);
Py_RETURN_TRUE;
}
static PyMethodDef methods[] = {
{"write_string", (PyCFunction) write_string, METH_VARARGS,
"Random write a string into a cell"
"Returns False if error occurs."},
{"write_boolean", (PyCFunction) write_boolean, METH_VARARGS,
"Random write a boolean into a cell"
"Returns False if error occurs."},
{"write_number", (PyCFunction) write_number, METH_VARARGS,
"Random write a number into a cell"
"Returns False if error occurs."},
{"write_datetime", (PyCFunction) write_datetime, METH_VARARGS,
"Random write a number into a cell"
"Returns False if error occurs."},
{NULL}
};
PyTypeObject SheetType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "sheet",
.tp_doc = "sheet definition",
.tp_basicsize = sizeof(PyXLSXSheet),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = PyType_GenericNew,
.tp_init = (initproc) init,
.tp_dealloc = (destructor) dealloc,
.tp_methods = methods,
};