andmarti1424 / sc-im

sc-im - Spreadsheet Calculator Improvised -- An ncurses spreadsheet program for terminal
Other
4.77k stars 201 forks source link

Date formatted cell output twice in CSV #879

Open marrs opened 1 month ago

marrs commented 1 month ago

To reproduce, export the following file to CSV.

# This data file was generated by the Spreadsheet Calculator Improvised (sc-im)
# You almost certainly shouldn't edit it.

newsheet "Sheet1"
movetosheet "Sheet1"
offscr_sc_cols 0
offscr_sc_rows 0
nb_frozen_rows 0
nb_frozen_cols 0
nb_frozen_screenrows 0
nb_frozen_screencols 0
format A 13 2 0
leftstring A0 = "1/2/20"
let A0 = 1580515200
fmt A0 "d%d/%m/%y"
leftstring A1 = "1/2/20"
let A1 = 1580515200
fmt A1 "d%b %Y"
goto A1

The following CSV is produced

01/02/201/2/20
Feb 20201/2/20

The following should be produced:

01/02/20
Feb 2020
marrs commented 1 month ago

The following patch fixes the issue, but I don't know the code well enough to know if it might break something elsewhere ;)

diff --git a/src/file.c b/src/file.c
index 738b9e3..40c0cfd 100644
--- a/src/file.c
+++ b/src/file.c
@@ -1884,12 +1884,12 @@ void export_delim(char * fname, char coldelim, int r0, int c0, int rn, int cn, i
             int last_valid_col = right_limit(sh, row)->col; // for issue #374
             if (col > last_valid_col) continue;
             if (*pp) {
+                char field[FBUFLEN] = "";
                 char * s;
                 if ((*pp)->flags & is_valid) {
                     if ((*pp)->cellerror) {
                         (void) fprintf (f, "%*s", sh->fwidth[col], ((*pp)->cellerror == CELLERROR ? "ERROR" : "INVALID"));
                     } else if ((*pp)->format) {
-                        char field[FBUFLEN];
                         if (*((*pp)->format) == 'd') {  // Date format
                             time_t v = (time_t) ((*pp)->v);
                             strftime(field, sizeof(field), ((*pp)->format)+1, localtime(&v));
@@ -1899,13 +1899,12 @@ void export_delim(char * fname, char coldelim, int r0, int c0, int rn, int cn, i
                         ltrim(field, ' ');
                         unspecial(f, field, coldelim);
                     } else { //eng number format
-                        char field[FBUFLEN] = "";
                         (void) engformat(sh->realfmt[col], sh->fwidth[col], sh->precision[col], (*pp)->v, field, sizeof(field));
                         ltrim(field, ' ');
                         unspecial(f, field, coldelim);
                     }
                 }
-                if ((s = (*pp)->label)) {
+                if (field[0] == '\0' && (s = (*pp)->label)) {
                     ltrim(s, ' ');
                     unspecial(f, s, coldelim);
                 }