fast_excel icon indicating copy to clipboard operation
fast_excel copied to clipboard

Long values are not saved to datasheet

Open DenShlk opened this issue 2 years ago • 4 comments

to reproduce, use example for saving urls and add this:

FastExcel::URL.new("https://www.luluhypermarket.com/cdn-cgi/image/f=auto/medias/1976246-01.jpg-300Wx300H?context=bWFzdGVyfGltYWdlc3wyOTkyN3xpbWFnZS9qcGVnfGFEUmhMMmcyWlM4eE5ESTNNek14T0RnME5qUTVOQzh4T1RjMk1qUTJMVEF4TG1wd1oxOHpNREJYZURNd01FZ3wyMWY2YzJjNjc0NzU0ZTcyYTJlNGU4YmVmOGZlYjg5MTRjNzk4NDJmYjZmNmYxNGJmOTYyNGY5YTZmNjk0ZDY3")

it will make an empty cell instead of cell with url.

Full code
require 'fast_excel'

workbook = FastExcel.open("example_hyperlink.xlsx", constant_memory: false)
worksheet = workbook.add_worksheet
worksheet.auto_width = true

url_format = workbook.add_format(underline: :underline_single, font_color: :blue)

worksheet.append_row([
                       "Ultra Fast Excel Writer for Ruby",
                       FastExcel::URL.new("https://github.com/Paxa/fast_excel"),
                       FastExcel::URL.new("https://www.luluhypermarket.com/cdn-cgi/image/f=auto/medias/1976246-01.jpg-300Wx300H?context=bWFzdGVyfGltYWdlc3wyOTkyN3xpbWFnZS9qcGVnfGFEUmhMMmcyWlM4eE5ESTNNek14T0RnME5qUTVOQzh4T1RjMk1qUTJMVEF4TG1wd1oxOHpNREJYZURNd01FZ3wyMWY2YzJjNjc0NzU0ZTcyYTJlNGU4YmVmOGZlYjg5MTRjNzk4NDJmYjZmNmYxNGJmOTYyNGY5YTZmNjk0ZDY3")
                     ], [nil, url_format])

# Same as:
#   worksheet.write_value(0, 0, "Ultra Fast Excel Writer for Ruby")
#   worksheet.write_url(0, 1, "https://github.com/Paxa/fast_excel", url_format)
  
workbook.close
puts "Saved to file example_hyperlink.xlsx"

DenShlk avatar Apr 03 '23 11:04 DenShlk

issue seems to be with C library, because it returns code 1 (error_memory_malloc_failed)

DenShlk avatar Apr 03 '23 11:04 DenShlk

I tried to save the same url as a string value, and it failed the same way, so issue is not related to urls, but strings in general

DenShlk avatar Apr 03 '23 11:04 DenShlk

It is also a bit strange that gem does not raise any error even though it gets error code

DenShlk avatar Apr 03 '23 11:04 DenShlk

I tried to localize the problem

$ git diff
diff --git a/libxlsxwriter/include/xlsxwriter/common.h b/libxlsxwriter/include/xlsxwriter/common.h
index c88287b..40954d5 100644
--- a/libxlsxwriter/include/xlsxwriter/common.h
+++ b/libxlsxwriter/include/xlsxwriter/common.h
@@ -134,6 +134,8 @@ typedef enum lxw_error {
     LXW_MAX_ERRNO
 } lxw_error;
 
+#define LXW_ERROR_MEMORY_MALLOC_FAILED (printf("LXW_ERROR_MEMORY_MALLOC_FAILED %s:%d\n", __FILE__, __LINE__) & 1)
+
 /** @brief Struct to represent a date and time in Excel.
  *
  * Struct to represent a date and time in Excel. See @ref working_with_dates.
diff --git a/libxlsxwriter/src/worksheet.c b/libxlsxwriter/src/worksheet.c
index 8403a33..f1e763e 100644
--- a/libxlsxwriter/src/worksheet.c
+++ b/libxlsxwriter/src/worksheet.c
@@ -4353,8 +4353,10 @@ worksheet_write_url_opt(lxw_worksheet *self,
     }
 
     /* Excel limits escaped URL to 255 characters. */
-    if (lxw_utf8_strlen(url_copy) > 255)
+    if (lxw_utf8_strlen(url_copy) > 255) {
+        printf("lxw_utf8_strlen(url_copy) > 255\n");
         goto mem_error;
+    }
 
     err = worksheet_write_string(self, row_num, col_num, string_copy, format);
     if (err)
$ ruby issue.rb 
lxw_utf8_strlen(url_copy) > 255
LXW_ERROR_MEMORY_MALLOC_FAILED worksheet.c:4381
Saved to file example_hyperlink.xlsx

uvlad7 avatar Apr 03 '23 16:04 uvlad7