workbook.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. * libxlsxwriter
  3. *
  4. * Copyright 2014-2018, John McNamara, [email protected]. See LICENSE.txt.
  5. */
  6. /**
  7. * @page workbook_page The Workbook object
  8. *
  9. * The Workbook is the main object exposed by the libxlsxwriter library. It
  10. * represents the entire spreadsheet as you see it in Excel and internally it
  11. * represents the Excel file as it is written on disk.
  12. *
  13. * See @ref workbook.h for full details of the functionality.
  14. *
  15. * @file workbook.h
  16. *
  17. * @brief Functions related to creating an Excel xlsx workbook.
  18. *
  19. * The Workbook is the main object exposed by the libxlsxwriter library. It
  20. * represents the entire spreadsheet as you see it in Excel and internally it
  21. * represents the Excel file as it is written on disk.
  22. *
  23. * @code
  24. * #include "xlsxwriter.h"
  25. *
  26. * int main() {
  27. *
  28. * lxw_workbook *workbook = workbook_new("filename.xlsx");
  29. * lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
  30. *
  31. * worksheet_write_string(worksheet, 0, 0, "Hello Excel", NULL);
  32. *
  33. * return workbook_close(workbook);
  34. * }
  35. * @endcode
  36. *
  37. * @image html workbook01.png
  38. *
  39. */
  40. #ifndef __LXW_WORKBOOK_H__
  41. #define __LXW_WORKBOOK_H__
  42. #include <stdint.h>
  43. #include <stdio.h>
  44. #include <errno.h>
  45. #include "worksheet.h"
  46. #include "chart.h"
  47. #include "shared_strings.h"
  48. #include "hash_table.h"
  49. #include "common.h"
  50. #define LXW_DEFINED_NAME_LENGTH 128
  51. /* Define the tree.h RB structs for the red-black head types. */
  52. RB_HEAD(lxw_worksheet_names, lxw_worksheet_name);
  53. /* Define the queue.h structs for the workbook lists. */
  54. STAILQ_HEAD(lxw_worksheets, lxw_worksheet);
  55. STAILQ_HEAD(lxw_charts, lxw_chart);
  56. TAILQ_HEAD(lxw_defined_names, lxw_defined_name);
  57. /* Struct to represent a worksheet name/pointer pair. */
  58. typedef struct lxw_worksheet_name {
  59. const char *name;
  60. lxw_worksheet *worksheet;
  61. RB_ENTRY (lxw_worksheet_name) tree_pointers;
  62. } lxw_worksheet_name;
  63. /* Wrapper around RB_GENERATE_STATIC from tree.h to avoid unused function
  64. * warnings and to avoid portability issues with the _unused attribute. */
  65. #define LXW_RB_GENERATE_NAMES(name, type, field, cmp) \
  66. RB_GENERATE_INSERT_COLOR(name, type, field, static) \
  67. RB_GENERATE_REMOVE_COLOR(name, type, field, static) \
  68. RB_GENERATE_INSERT(name, type, field, cmp, static) \
  69. RB_GENERATE_REMOVE(name, type, field, static) \
  70. RB_GENERATE_FIND(name, type, field, cmp, static) \
  71. RB_GENERATE_NEXT(name, type, field, static) \
  72. RB_GENERATE_MINMAX(name, type, field, static) \
  73. /* Add unused struct to allow adding a semicolon */ \
  74. struct lxw_rb_generate_names{int unused;}
  75. /**
  76. * @brief Macro to loop over all the worksheets in a workbook.
  77. *
  78. * This macro allows you to loop over all the worksheets that have been
  79. * added to a workbook. You must provide a lxw_worksheet pointer and
  80. * a pointer to the lxw_workbook:
  81. *
  82. * @code
  83. * lxw_workbook *workbook = workbook_new("test.xlsx");
  84. *
  85. * lxw_worksheet *worksheet; // Generic worksheet pointer.
  86. *
  87. * // Worksheet objects used in the program.
  88. * lxw_worksheet *worksheet1 = workbook_add_worksheet(workbook, NULL);
  89. * lxw_worksheet *worksheet2 = workbook_add_worksheet(workbook, NULL);
  90. * lxw_worksheet *worksheet3 = workbook_add_worksheet(workbook, NULL);
  91. *
  92. * // Iterate over the 3 worksheets and perform the same operation on each.
  93. * LXW_FOREACH_WORKSHEET(worksheet, workbook) {
  94. * worksheet_write_string(worksheet, 0, 0, "Hello", NULL);
  95. * }
  96. * @endcode
  97. */
  98. #define LXW_FOREACH_WORKSHEET(worksheet, workbook) \
  99. STAILQ_FOREACH((worksheet), (workbook)->worksheets, list_pointers)
  100. /* Struct to represent a defined name. */
  101. typedef struct lxw_defined_name {
  102. int16_t index;
  103. uint8_t hidden;
  104. char name[LXW_DEFINED_NAME_LENGTH];
  105. char app_name[LXW_DEFINED_NAME_LENGTH];
  106. char formula[LXW_DEFINED_NAME_LENGTH];
  107. char normalised_name[LXW_DEFINED_NAME_LENGTH];
  108. char normalised_sheetname[LXW_DEFINED_NAME_LENGTH];
  109. /* List pointers for queue.h. */
  110. TAILQ_ENTRY (lxw_defined_name) list_pointers;
  111. } lxw_defined_name;
  112. /**
  113. * Workbook document properties.
  114. */
  115. typedef struct lxw_doc_properties {
  116. /** The title of the Excel Document. */
  117. char *title;
  118. /** The subject of the Excel Document. */
  119. char *subject;
  120. /** The author of the Excel Document. */
  121. char *author;
  122. /** The manager field of the Excel Document. */
  123. char *manager;
  124. /** The company field of the Excel Document. */
  125. char *company;
  126. /** The category of the Excel Document. */
  127. char *category;
  128. /** The keywords of the Excel Document. */
  129. char *keywords;
  130. /** The comment field of the Excel Document. */
  131. char *comments;
  132. /** The status of the Excel Document. */
  133. char *status;
  134. /** The hyperlink base url of the Excel Document. */
  135. char *hyperlink_base;
  136. time_t created;
  137. } lxw_doc_properties;
  138. /**
  139. * @brief Workbook options.
  140. *
  141. * Optional parameters when creating a new Workbook object via
  142. * workbook_new_opt().
  143. *
  144. * The following properties are supported:
  145. *
  146. * - `constant_memory`: Reduces the amount of data stored in memory so that
  147. * large files can be written efficiently.
  148. *
  149. * @note In this mode a row of data is written and then discarded when a
  150. * cell in a new row is added via one of the `worksheet_write_*()`
  151. * functions. Therefore, once this option is active, data should be written in
  152. * sequential row order. For this reason the `worksheet_merge_range()`
  153. * doesn't work in this mode. See also @ref ww_mem_constant.
  154. *
  155. * - `tmpdir`: libxlsxwriter stores workbook data in temporary files prior
  156. * to assembling the final XLSX file. The temporary files are created in the
  157. * system's temp directory. If the default temporary directory isn't
  158. * accessible to your application, or doesn't contain enough space, you can
  159. * specify an alternative location using the `tempdir` option.
  160. */
  161. typedef struct lxw_workbook_options {
  162. /** Optimize the workbook to use constant memory for worksheets */
  163. uint8_t constant_memory;
  164. /** Directory to use for the temporary files created by libxlsxwriter. */
  165. char *tmpdir;
  166. } lxw_workbook_options;
  167. /**
  168. * @brief Struct to represent an Excel workbook.
  169. *
  170. * The members of the lxw_workbook struct aren't modified directly. Instead
  171. * the workbook properties are set by calling the functions shown in
  172. * workbook.h.
  173. */
  174. typedef struct lxw_workbook {
  175. FILE *file;
  176. struct lxw_worksheets *worksheets;
  177. struct lxw_worksheet_names *worksheet_names;
  178. struct lxw_charts *charts;
  179. struct lxw_charts *ordered_charts;
  180. struct lxw_formats *formats;
  181. struct lxw_defined_names *defined_names;
  182. lxw_sst *sst;
  183. lxw_doc_properties *properties;
  184. struct lxw_custom_properties *custom_properties;
  185. char *filename;
  186. lxw_workbook_options options;
  187. uint16_t num_sheets;
  188. uint16_t first_sheet;
  189. uint16_t active_sheet;
  190. uint16_t num_xf_formats;
  191. uint16_t num_format_count;
  192. uint16_t drawing_count;
  193. uint16_t font_count;
  194. uint16_t border_count;
  195. uint16_t fill_count;
  196. uint8_t optimize;
  197. uint8_t has_png;
  198. uint8_t has_jpeg;
  199. uint8_t has_bmp;
  200. lxw_hash_table *used_xf_formats;
  201. } lxw_workbook;
  202. /* *INDENT-OFF* */
  203. #ifdef __cplusplus
  204. extern "C" {
  205. #endif
  206. /* *INDENT-ON* */
  207. /**
  208. * @brief Create a new workbook object.
  209. *
  210. * @param filename The name of the new Excel file to create.
  211. *
  212. * @return A lxw_workbook instance.
  213. *
  214. * The `%workbook_new()` constructor is used to create a new Excel workbook
  215. * with a given filename:
  216. *
  217. * @code
  218. * lxw_workbook *workbook = workbook_new("filename.xlsx");
  219. * @endcode
  220. *
  221. * When specifying a filename it is recommended that you use an `.xlsx`
  222. * extension or Excel will generate a warning when opening the file.
  223. *
  224. */
  225. lxw_workbook *workbook_new(const char *filename);
  226. /**
  227. * @brief Create a new workbook object, and set the workbook options.
  228. *
  229. * @param filename The name of the new Excel file to create.
  230. * @param options Workbook options.
  231. *
  232. * @return A lxw_workbook instance.
  233. *
  234. * This function is the same as the `workbook_new()` constructor but allows
  235. * additional options to be set.
  236. *
  237. * @code
  238. * lxw_workbook_options options = {.constant_memory = 1,
  239. * .tmpdir = "C:\\Temp"};
  240. *
  241. * lxw_workbook *workbook = workbook_new_opt("filename.xlsx", &options);
  242. * @endcode
  243. *
  244. * The options that can be set via #lxw_workbook_options are:
  245. *
  246. * - `constant_memory`: Reduces the amount of data stored in memory so that
  247. * large files can be written efficiently.
  248. *
  249. * @note In this mode a row of data is written and then discarded when a
  250. * cell in a new row is added via one of the `worksheet_write_*()`
  251. * functions. Therefore, once this option is active, data should be written in
  252. * sequential row order. For this reason the `worksheet_merge_range()`
  253. * doesn't work in this mode. See also @ref ww_mem_constant.
  254. *
  255. * - `tmpdir`: libxlsxwriter stores workbook data in temporary files prior
  256. * to assembling the final XLSX file. The temporary files are created in the
  257. * system's temp directory. If the default temporary directory isn't
  258. * accessible to your application, or doesn't contain enough space, you can
  259. * specify an alternative location using the `tempdir` option.*
  260. *
  261. * See @ref working_with_memory for more details.
  262. *
  263. */
  264. lxw_workbook *workbook_new_opt(const char *filename,
  265. lxw_workbook_options *options);
  266. /* Deprecated function name for backwards compatibility. */
  267. lxw_workbook *new_workbook(const char *filename);
  268. /* Deprecated function name for backwards compatibility. */
  269. lxw_workbook *new_workbook_opt(const char *filename,
  270. lxw_workbook_options *options);
  271. /**
  272. * @brief Add a new worksheet to a workbook.
  273. *
  274. * @param workbook Pointer to a lxw_workbook instance.
  275. * @param sheetname Optional worksheet name, defaults to Sheet1, etc.
  276. *
  277. * @return A lxw_worksheet object.
  278. *
  279. * The `%workbook_add_worksheet()` function adds a new worksheet to a workbook:
  280. *
  281. * At least one worksheet should be added to a new workbook: The @ref
  282. * worksheet.h "Worksheet" object is used to write data and configure a
  283. * worksheet in the workbook.
  284. *
  285. * The `sheetname` parameter is optional. If it is `NULL` the default
  286. * Excel convention will be followed, i.e. Sheet1, Sheet2, etc.:
  287. *
  288. * @code
  289. * worksheet = workbook_add_worksheet(workbook, NULL ); // Sheet1
  290. * worksheet = workbook_add_worksheet(workbook, "Foglio2"); // Foglio2
  291. * worksheet = workbook_add_worksheet(workbook, "Data"); // Data
  292. * worksheet = workbook_add_worksheet(workbook, NULL ); // Sheet4
  293. *
  294. * @endcode
  295. *
  296. * @image html workbook02.png
  297. *
  298. * The worksheet name must be a valid Excel worksheet name, i.e. it must be
  299. * less than 32 character and it cannot contain any of the characters:
  300. *
  301. * / \ [ ] : * ?
  302. *
  303. * In addition, you cannot use the same, case insensitive, `sheetname` for more
  304. * than one worksheet.
  305. *
  306. */
  307. lxw_worksheet *workbook_add_worksheet(lxw_workbook *workbook,
  308. const char *sheetname);
  309. /**
  310. * @brief Create a new @ref format.h "Format" object to formats cells in
  311. * worksheets.
  312. *
  313. * @param workbook Pointer to a lxw_workbook instance.
  314. *
  315. * @return A lxw_format instance.
  316. *
  317. * The `workbook_add_format()` function can be used to create new @ref
  318. * format.h "Format" objects which are used to apply formatting to a cell.
  319. *
  320. * @code
  321. * // Create the Format.
  322. * lxw_format *format = workbook_add_format(workbook);
  323. *
  324. * // Set some of the format properties.
  325. * format_set_bold(format);
  326. * format_set_font_color(format, LXW_COLOR_RED);
  327. *
  328. * // Use the format to change the text format in a cell.
  329. * worksheet_write_string(worksheet, 0, 0, "Hello", format);
  330. * @endcode
  331. *
  332. * See @ref format.h "the Format object" and @ref working_with_formats
  333. * sections for more details about Format properties and how to set them.
  334. *
  335. */
  336. lxw_format *workbook_add_format(lxw_workbook *workbook);
  337. /**
  338. * @brief Create a new chart to be added to a worksheet:
  339. *
  340. * @param workbook Pointer to a lxw_workbook instance.
  341. * @param chart_type The type of chart to be created. See #lxw_chart_type.
  342. *
  343. * @return A lxw_chart object.
  344. *
  345. * The `%workbook_add_chart()` function creates a new chart object that can
  346. * be added to a worksheet:
  347. *
  348. * @code
  349. * // Create a chart object.
  350. * lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
  351. *
  352. * // Add data series to the chart.
  353. * chart_add_series(chart, NULL, "Sheet1!$A$1:$A$5");
  354. * chart_add_series(chart, NULL, "Sheet1!$B$1:$B$5");
  355. * chart_add_series(chart, NULL, "Sheet1!$C$1:$C$5");
  356. *
  357. * // Insert the chart into the worksheet
  358. * worksheet_insert_chart(worksheet, CELL("B7"), chart);
  359. * @endcode
  360. *
  361. * The available chart types are defined in #lxw_chart_type. The types of
  362. * charts that are supported are:
  363. *
  364. * | Chart type | Description |
  365. * | :--------------------------------------- | :------------------------------------ |
  366. * | #LXW_CHART_AREA | Area chart. |
  367. * | #LXW_CHART_AREA_STACKED | Area chart - stacked. |
  368. * | #LXW_CHART_AREA_STACKED_PERCENT | Area chart - percentage stacked. |
  369. * | #LXW_CHART_BAR | Bar chart. |
  370. * | #LXW_CHART_BAR_STACKED | Bar chart - stacked. |
  371. * | #LXW_CHART_BAR_STACKED_PERCENT | Bar chart - percentage stacked. |
  372. * | #LXW_CHART_COLUMN | Column chart. |
  373. * | #LXW_CHART_COLUMN_STACKED | Column chart - stacked. |
  374. * | #LXW_CHART_COLUMN_STACKED_PERCENT | Column chart - percentage stacked. |
  375. * | #LXW_CHART_DOUGHNUT | Doughnut chart. |
  376. * | #LXW_CHART_LINE | Line chart. |
  377. * | #LXW_CHART_PIE | Pie chart. |
  378. * | #LXW_CHART_SCATTER | Scatter chart. |
  379. * | #LXW_CHART_SCATTER_STRAIGHT | Scatter chart - straight. |
  380. * | #LXW_CHART_SCATTER_STRAIGHT_WITH_MARKERS | Scatter chart - straight with markers. |
  381. * | #LXW_CHART_SCATTER_SMOOTH | Scatter chart - smooth. |
  382. * | #LXW_CHART_SCATTER_SMOOTH_WITH_MARKERS | Scatter chart - smooth with markers. |
  383. * | #LXW_CHART_RADAR | Radar chart. |
  384. * | #LXW_CHART_RADAR_WITH_MARKERS | Radar chart - with markers. |
  385. * | #LXW_CHART_RADAR_FILLED | Radar chart - filled. |
  386. *
  387. *
  388. *
  389. * See @ref chart.h for details.
  390. */
  391. lxw_chart *workbook_add_chart(lxw_workbook *workbook, uint8_t chart_type);
  392. /**
  393. * @brief Close the Workbook object and write the XLSX file.
  394. *
  395. * @param workbook Pointer to a lxw_workbook instance.
  396. *
  397. * @return A #lxw_error.
  398. *
  399. * The `%workbook_close()` function closes a Workbook object, writes the Excel
  400. * file to disk, frees any memory allocated internally to the Workbook and
  401. * frees the object itself.
  402. *
  403. * @code
  404. * workbook_close(workbook);
  405. * @endcode
  406. *
  407. * The `%workbook_close()` function returns any #lxw_error error codes
  408. * encountered when creating the Excel file. The error code can be returned
  409. * from the program main or the calling function:
  410. *
  411. * @code
  412. * return workbook_close(workbook);
  413. * @endcode
  414. *
  415. */
  416. lxw_error workbook_close(lxw_workbook *workbook);
  417. /**
  418. * @brief Set the document properties such as Title, Author etc.
  419. *
  420. * @param workbook Pointer to a lxw_workbook instance.
  421. * @param properties Document properties to set.
  422. *
  423. * @return A #lxw_error.
  424. *
  425. * The `%workbook_set_properties` function can be used to set the document
  426. * properties of the Excel file created by `libxlsxwriter`. These properties
  427. * are visible when you use the `Office Button -> Prepare -> Properties`
  428. * option in Excel and are also available to external applications that read
  429. * or index windows files.
  430. *
  431. * The properties that can be set are:
  432. *
  433. * - `title`
  434. * - `subject`
  435. * - `author`
  436. * - `manager`
  437. * - `company`
  438. * - `category`
  439. * - `keywords`
  440. * - `comments`
  441. * - `hyperlink_base`
  442. *
  443. * The properties are specified via a `lxw_doc_properties` struct. All the
  444. * members are `char *` and they are all optional. An example of how to create
  445. * and pass the properties is:
  446. *
  447. * @code
  448. * // Create a properties structure and set some of the fields.
  449. * lxw_doc_properties properties = {
  450. * .title = "This is an example spreadsheet",
  451. * .subject = "With document properties",
  452. * .author = "John McNamara",
  453. * .manager = "Dr. Heinz Doofenshmirtz",
  454. * .company = "of Wolves",
  455. * .category = "Example spreadsheets",
  456. * .keywords = "Sample, Example, Properties",
  457. * .comments = "Created with libxlsxwriter",
  458. * .status = "Quo",
  459. * };
  460. *
  461. * // Set the properties in the workbook.
  462. * workbook_set_properties(workbook, &properties);
  463. * @endcode
  464. *
  465. * @image html doc_properties.png
  466. *
  467. */
  468. lxw_error workbook_set_properties(lxw_workbook *workbook,
  469. lxw_doc_properties *properties);
  470. /**
  471. * @brief Set a custom document text property.
  472. *
  473. * @param workbook Pointer to a lxw_workbook instance.
  474. * @param name The name of the custom property.
  475. * @param value The value of the custom property.
  476. *
  477. * @return A #lxw_error.
  478. *
  479. * The `%workbook_set_custom_property_string()` function can be used to set one
  480. * or more custom document text properties not covered by the standard
  481. * properties in the `workbook_set_properties()` function above.
  482. *
  483. * For example:
  484. *
  485. * @code
  486. * workbook_set_custom_property_string(workbook, "Checked by", "Eve");
  487. * @endcode
  488. *
  489. * @image html custom_properties.png
  490. *
  491. * There are 4 `workbook_set_custom_property_string_*()` functions for each
  492. * of the custom property types supported by Excel:
  493. *
  494. * - text/string: `workbook_set_custom_property_string()`
  495. * - number: `workbook_set_custom_property_number()`
  496. * - datetime: `workbook_set_custom_property_datetime()`
  497. * - boolean: `workbook_set_custom_property_boolean()`
  498. *
  499. * **Note**: the name and value parameters are limited to 255 characters
  500. * by Excel.
  501. *
  502. */
  503. lxw_error workbook_set_custom_property_string(lxw_workbook *workbook,
  504. const char *name,
  505. const char *value);
  506. /**
  507. * @brief Set a custom document number property.
  508. *
  509. * @param workbook Pointer to a lxw_workbook instance.
  510. * @param name The name of the custom property.
  511. * @param value The value of the custom property.
  512. *
  513. * @return A #lxw_error.
  514. *
  515. * Set a custom document number property.
  516. * See `workbook_set_custom_property_string()` above for details.
  517. *
  518. * @code
  519. * workbook_set_custom_property_number(workbook, "Document number", 12345);
  520. * @endcode
  521. */
  522. lxw_error workbook_set_custom_property_number(lxw_workbook *workbook,
  523. const char *name, double value);
  524. /* Undocumented since the user can use workbook_set_custom_property_number().
  525. * Only implemented for file format completeness and testing.
  526. */
  527. lxw_error workbook_set_custom_property_integer(lxw_workbook *workbook,
  528. const char *name,
  529. int32_t value);
  530. /**
  531. * @brief Set a custom document boolean property.
  532. *
  533. * @param workbook Pointer to a lxw_workbook instance.
  534. * @param name The name of the custom property.
  535. * @param value The value of the custom property.
  536. *
  537. * @return A #lxw_error.
  538. *
  539. * Set a custom document boolean property.
  540. * See `workbook_set_custom_property_string()` above for details.
  541. *
  542. * @code
  543. * workbook_set_custom_property_boolean(workbook, "Has Review", 1);
  544. * @endcode
  545. */
  546. lxw_error workbook_set_custom_property_boolean(lxw_workbook *workbook,
  547. const char *name,
  548. uint8_t value);
  549. /**
  550. * @brief Set a custom document date or time property.
  551. *
  552. * @param workbook Pointer to a lxw_workbook instance.
  553. * @param name The name of the custom property.
  554. * @param datetime The value of the custom property.
  555. *
  556. * @return A #lxw_error.
  557. *
  558. * Set a custom date or time number property.
  559. * See `workbook_set_custom_property_string()` above for details.
  560. *
  561. * @code
  562. * lxw_datetime datetime = {2016, 12, 1, 11, 55, 0.0};
  563. *
  564. * workbook_set_custom_property_datetime(workbook, "Date completed", &datetime);
  565. * @endcode
  566. */
  567. lxw_error workbook_set_custom_property_datetime(lxw_workbook *workbook,
  568. const char *name,
  569. lxw_datetime *datetime);
  570. /**
  571. * @brief Create a defined name in the workbook to use as a variable.
  572. *
  573. * @param workbook Pointer to a lxw_workbook instance.
  574. * @param name The defined name.
  575. * @param formula The cell or range that the defined name refers to.
  576. *
  577. * @return A #lxw_error.
  578. *
  579. * This function is used to defined a name that can be used to represent a
  580. * value, a single cell or a range of cells in a workbook: These defined names
  581. * can then be used in formulas:
  582. *
  583. * @code
  584. * workbook_define_name(workbook, "Exchange_rate", "=0.96");
  585. * worksheet_write_formula(worksheet, 2, 1, "=Exchange_rate", NULL);
  586. *
  587. * @endcode
  588. *
  589. * @image html defined_name.png
  590. *
  591. * As in Excel a name defined like this is "global" to the workbook and can be
  592. * referred to from any worksheet:
  593. *
  594. * @code
  595. * // Global workbook name.
  596. * workbook_define_name(workbook, "Sales", "=Sheet1!$G$1:$H$10");
  597. * @endcode
  598. *
  599. * It is also possible to define a local/worksheet name by prefixing it with
  600. * the sheet name using the syntax `'sheetname!definedname'`:
  601. *
  602. * @code
  603. * // Local worksheet name.
  604. * workbook_define_name(workbook, "Sheet2!Sales", "=Sheet2!$G$1:$G$10");
  605. * @endcode
  606. *
  607. * If the sheet name contains spaces or special characters you must follow the
  608. * Excel convention and enclose it in single quotes:
  609. *
  610. * @code
  611. * workbook_define_name(workbook, "'New Data'!Sales", "=Sheet2!$G$1:$G$10");
  612. * @endcode
  613. *
  614. * The rules for names in Excel are explained in the
  615. * [Microsoft Office
  616. documentation](http://office.microsoft.com/en-001/excel-help/define-and-use-names-in-formulas-HA010147120.aspx).
  617. *
  618. */
  619. lxw_error workbook_define_name(lxw_workbook *workbook, const char *name,
  620. const char *formula);
  621. /**
  622. * @brief Get a worksheet object from its name.
  623. *
  624. * @param workbook Pointer to a lxw_workbook instance.
  625. * @param name Worksheet name.
  626. *
  627. * @return A lxw_worksheet object.
  628. *
  629. * This function returns a lxw_worksheet object reference based on its name:
  630. *
  631. * @code
  632. * worksheet = workbook_get_worksheet_by_name(workbook, "Sheet1");
  633. * @endcode
  634. *
  635. */
  636. lxw_worksheet *workbook_get_worksheet_by_name(lxw_workbook *workbook,
  637. const char *name);
  638. /**
  639. * @brief Validate a worksheet name.
  640. *
  641. * @param workbook Pointer to a lxw_workbook instance.
  642. * @param sheetname Worksheet name to validate.
  643. *
  644. * @return A #lxw_error.
  645. *
  646. * This function is used to validate a worksheet name according to the rules
  647. * used by Excel:
  648. *
  649. * - The name is less than or equal to 31 UTF-8 characters.
  650. * - The name doesn't contain any of the characters: ` [ ] : * ? / \ `
  651. * - The name isn't already in use.
  652. *
  653. * @code
  654. * lxw_error err = workbook_validate_worksheet_name(workbook, "Foglio");
  655. * @endcode
  656. *
  657. * This function is called by `workbook_add_worksheet()` but it can be
  658. * explicitly called by the user beforehand to ensure that the worksheet
  659. * name is valid.
  660. *
  661. */
  662. lxw_error workbook_validate_worksheet_name(lxw_workbook *workbook,
  663. const char *sheetname);
  664. void lxw_workbook_free(lxw_workbook *workbook);
  665. void lxw_workbook_assemble_xml_file(lxw_workbook *workbook);
  666. void lxw_workbook_set_default_xf_indices(lxw_workbook *workbook);
  667. /* Declarations required for unit testing. */
  668. #ifdef TESTING
  669. STATIC void _workbook_xml_declaration(lxw_workbook *self);
  670. STATIC void _write_workbook(lxw_workbook *self);
  671. STATIC void _write_file_version(lxw_workbook *self);
  672. STATIC void _write_workbook_pr(lxw_workbook *self);
  673. STATIC void _write_book_views(lxw_workbook *self);
  674. STATIC void _write_workbook_view(lxw_workbook *self);
  675. STATIC void _write_sheet(lxw_workbook *self,
  676. const char *name, uint32_t sheet_id, uint8_t hidden);
  677. STATIC void _write_sheets(lxw_workbook *self);
  678. STATIC void _write_calc_pr(lxw_workbook *self);
  679. STATIC void _write_defined_name(lxw_workbook *self,
  680. lxw_defined_name *define_name);
  681. STATIC void _write_defined_names(lxw_workbook *self);
  682. STATIC lxw_error _store_defined_name(lxw_workbook *self, const char *name,
  683. const char *app_name,
  684. const char *formula, int16_t index,
  685. uint8_t hidden);
  686. #endif /* TESTING */
  687. /* *INDENT-OFF* */
  688. #ifdef __cplusplus
  689. }
  690. #endif
  691. /* *INDENT-ON* */
  692. #endif /* __LXW_WORKBOOK_H__ */