Added worksheet_write_rich_string_html() function.

Added workaround/optimized worksheet_write_rich_string_html()
function to write raw rich string html. This avoid an expensive
file open()/close() in each call to worksheet_write_rich_string().
This commit is contained in:
John McNamara 2020-08-21 21:06:24 +01:00
parent 5269d07825
commit 5c160dd4e8
7 changed files with 163 additions and 1 deletions

View File

@ -1864,6 +1864,12 @@ lxw_error worksheet_write_rich_string(lxw_worksheet *worksheet,
lxw_rich_string_tuple *rich_string[], lxw_rich_string_tuple *rich_string[],
lxw_format *format); lxw_format *format);
lxw_error worksheet_write_rich_string_html(lxw_worksheet *worksheet,
lxw_row_t row_num,
lxw_col_t col_num,
char *rich_string,
lxw_format *format);
/** /**
* @brief Write a comment to a worksheet cell. * @brief Write a comment to a worksheet cell.
* *

View File

@ -5627,6 +5627,53 @@ mem_error:
return LXW_ERROR_MEMORY_MALLOC_FAILED; return LXW_ERROR_MEMORY_MALLOC_FAILED;
} }
/*
* Write a rich string to an Excel file using user created html.
*
* Optimized workaround to write raw html representing a rich string to avoid
* the overhead of opening a tmpfile to generate the html.
*/
lxw_error
worksheet_write_rich_string_html(lxw_worksheet *self,
lxw_row_t row_num,
lxw_col_t col_num,
char *rich_string, lxw_format *format)
{
lxw_cell *cell;
int32_t string_id;
struct sst_element *sst_element;
lxw_error err;
err = _check_dimensions(self, row_num, col_num, LXW_FALSE, LXW_FALSE);
if (err)
return err;
if (lxw_utf8_strlen(rich_string) > LXW_STR_MAX) {
return LXW_ERROR_MAX_STRING_LENGTH_EXCEEDED;
}
if (!self->optimize) {
/* Get the SST element and string id. */
sst_element = lxw_get_sst_index(self->sst, rich_string, LXW_TRUE);
if (!sst_element)
return LXW_ERROR_SHARED_STRING_INDEX_NOT_FOUND;
string_id = sst_element->index;
cell = _new_string_cell(row_num, col_num, string_id,
sst_element->string, format);
}
else {
cell = _new_inline_rich_string_cell(row_num,
col_num,
lxw_strdup(rich_string), format);
}
_insert_cell(self, row_num, col_num, cell);
return LXW_NO_ERROR;
}
/* /*
* Write a comment to a worksheet cell in Excel. * Write a comment to a worksheet cell in Excel.
*/ */

View File

@ -0,0 +1,33 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook_options options = {LXW_TRUE, NULL, LXW_FALSE};
lxw_workbook *workbook = workbook_new_opt("test_optimize54.xlsx", &options);
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
char *rich_string = "<r><t>a</t></r><r><rPr><b/><sz val=\"11\"/><color theme=\"1\"/><rFont val=\"Calibri\"/><family val=\"2\"/><scheme val=\"minor\"/></rPr><t>bc</t></r><r><rPr><sz val=\"11\"/><color theme=\"1\"/><rFont val=\"Calibri\"/><family val=\"2\"/><scheme val=\"minor\"/></rPr><t>defg</t></r>";
lxw_format *bold = workbook_add_format(workbook);
lxw_format *italic = workbook_add_format(workbook);
format_set_bold(bold);
format_set_italic(italic);
worksheet_write_string(worksheet, CELL("A1"), "Foo", bold);
worksheet_write_string(worksheet, CELL("A2"), "Bar", italic);
worksheet_write_rich_string_html(worksheet, CELL("A3"), rich_string, NULL);
return workbook_close(workbook);
}

View File

@ -0,0 +1,31 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_rich_string51.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
char *rich_string = "<r><t>a</t></r><r><rPr><b/><sz val=\"11\"/><color theme=\"1\"/><rFont val=\"Calibri\"/><family val=\"2\"/><scheme val=\"minor\"/></rPr><t>bc</t></r><r><rPr><sz val=\"11\"/><color theme=\"1\"/><rFont val=\"Calibri\"/><family val=\"2\"/><scheme val=\"minor\"/></rPr><t>defg</t></r>";
lxw_format *bold = workbook_add_format(workbook);
lxw_format *italic = workbook_add_format(workbook);
format_set_bold(bold);
format_set_italic(italic);
worksheet_write_string(worksheet, CELL("A1"), "Foo", bold);
worksheet_write_string(worksheet, CELL("A2"), "Bar", italic);
worksheet_write_rich_string_html(worksheet, CELL("A3"), rich_string, NULL);
return workbook_close(workbook);
}

View File

@ -0,0 +1,33 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2020, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
int main() {
lxw_workbook *workbook = workbook_new("test_rich_string55.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
char *rich_string = "<r><t xml:space=\"preserve\">This is </t></r><r><rPr><b/><sz val=\"11\"/><color theme=\"1\"/><rFont val=\"Calibri\"/><family val=\"2\"/><scheme val=\"minor\"/></rPr><t>bold</t></r><r><rPr><sz val=\"11\"/><color theme=\"1\"/><rFont val=\"Calibri\"/><family val=\"2\"/><scheme val=\"minor\"/></rPr><t xml:space=\"preserve\"> and this is </t></r><r><rPr><i/><sz val=\"11\"/><color theme=\"1\"/><rFont val=\"Calibri\"/><family val=\"2\"/><scheme val=\"minor\"/></rPr><t>italic</t></r>";
lxw_format *bold = workbook_add_format(workbook);
lxw_format *italic = workbook_add_format(workbook);
format_set_bold(bold);
format_set_italic(italic);
worksheet_set_column(worksheet, 0, 0, 30, NULL);
worksheet_write_string(worksheet, CELL("A1"), "Foo", bold);
worksheet_write_string(worksheet, CELL("A2"), "Bar", italic);
worksheet_write_rich_string_html(worksheet, CELL("A3"), rich_string, NULL);
return workbook_close(workbook);
}

View File

@ -56,3 +56,7 @@ class TestCompareXLSXFiles(base_test_class.XLSXBaseTest):
def test_optimize26(self): def test_optimize26(self):
self.run_exe_test('test_optimize26') self.run_exe_test('test_optimize26')
# Test the worksheet_rich_string_html() function.
def test_optimize54(self):
self.run_exe_test('test_optimize54', 'optimize04.xlsx')

View File

@ -8,6 +8,7 @@
import base_test_class import base_test_class
class TestCompareXLSXFiles(base_test_class.XLSXBaseTest): class TestCompareXLSXFiles(base_test_class.XLSXBaseTest):
""" """
Test file created with libxlsxwriter against a file created by Excel. Test file created with libxlsxwriter against a file created by Excel.
@ -15,8 +16,8 @@ class TestCompareXLSXFiles(base_test_class.XLSXBaseTest):
def test_rich_string01(self): def test_rich_string01(self):
self.run_exe_test('test_rich_string01') self.run_exe_test('test_rich_string01')
def test_rich_string02(self): def test_rich_string02(self):
self.run_exe_test('test_rich_string02') self.run_exe_test('test_rich_string02')
def test_rich_string03(self): def test_rich_string03(self):
@ -48,3 +49,10 @@ class TestCompareXLSXFiles(base_test_class.XLSXBaseTest):
def test_rich_string12(self): def test_rich_string12(self):
self.run_exe_test('test_rich_string12') self.run_exe_test('test_rich_string12')
# Test the worksheet_rich_string_html() function.
def test_rich_string51(self):
self.run_exe_test('test_rich_string51', 'rich_string01.xlsx')
def test_rich_string55(self):
self.run_exe_test('test_rich_string55', 'rich_string05.xlsx')