sheetjs icon indicating copy to clipboard operation
sheetjs copied to clipboard

When writing hyperlink cell & save the wb, cell created as simple text

Open MayaGi opened this issue 11 years ago • 18 comments

Hi, When creating the workbook from scratch, creating a hyperlink text according to the docs, cell is created as a simple text.

here is the code:

/* writing feature test -- look for TEST: in comments */
    /* vim: set ts=2: */

    function Hyperlink() {
        this.target = "";
        this.display = "";
    }

    var x = new Hyperlink();
    x.target = "http://localhost:9980/HEXA3_FA/index.html#/fm/s/unassociated_failures";
    x.display = "link";

    /* original data */
    var data = [
        [1, 2, 3],
        [true, false, null, "sheetjs"],
        ["foo", "bar", new Date("2014-02-19T14:30Z"), "0.3"],
        ["baz", null, x]
    ];

    var ws_name = "SheetJS";

    var wscols = [
        { wch: 6 },
        { wch: 7 },
        { wch: 10 },
        { wch: 20 }
    ];


    console.log("Sheet Name: " + ws_name);
    console.log("Data: "); for (var i = 0; i != data.length; ++i) console.log(data[i]);
    console.log("Columns :"); for (i = 0; i != wscols.length; ++i) console.log(wscols[i]);



    /* require XLSX */
    if (typeof XLSX === "undefined") { try { XLSX = require('./'); } catch (e) { XLSX = require('../'); } }

    /* dummy workbook constructor */
    function Workbook() {
        if (!(this instanceof Workbook)) return new Workbook();
        this.SheetNames = [];
        this.Sheets = {};
    }
    var wb = new Workbook();


    /* TODO: date1904 logic */
    function datenum(v, date1904) {
        if (date1904) v += 1462;
        var epoch = Date.parse(v);
        return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
    }

    /* convert an array of arrays in JS to a CSF spreadsheet */
    function sheet_from_array_of_arrays(data, opts) {
        var ws = {};
        var range = { s: { c: 10000000, r: 10000000 }, e: { c: 0, r: 0 } };
        for (var R = 0; R != data.length; ++R) {
            for (var C = 0; C != data[R].length; ++C) {
                if (range.s.r > R) range.s.r = R;
                if (range.s.c > C) range.s.c = C;
                if (range.e.r < R) range.e.r = R;
                if (range.e.c < C) range.e.c = C;
                var cell = { v: data[R][C] };
                if (cell.v == null) continue;
                var cell_ref = XLSX.utils.encode_cell({ c: C, r: R });

                /* TEST: proper cell types and value handling */
                if (typeof cell.v === 'number') cell.t = 'n';
                else if (typeof cell.v === 'boolean') cell.t = 'b';
                else if (cell.v instanceof Date) {
                    cell.t = 'n'; cell.z = XLSX.SSF._table[14];
                    cell.v = datenum(cell.v);
                }
                else if (cell.v instanceof Hyperlink) {
                    cell.l = { Target: cell.v.target, tooltip: cell.v.display },
                    cell.v = cell.v.display;
                }
                else cell.t = 's';
                ws[cell_ref] = cell;
            }
        }

        /* TEST: proper range */
        if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
        return ws;
    }
    var ws = sheet_from_array_of_arrays(data);

    /* TEST: add worksheet to workbook */
    wb.SheetNames.push(ws_name);
    wb.Sheets[ws_name] = ws;

    /* TEST: column widths */
    ws['!cols'] = wscols;

    /* write file */
    //XLSX.writeFile(wb, 'sheetjs.xlsx');
    var wbout = XLSX.write(wb, { bookType: "xlsx", bookSST: true, type: 'binary' });
    console.log("wbout: " + wbout);
    var s2ab = function(s) {
        var buf = new ArrayBuffer(s.length);
        var view = new Uint8Array(buf);
        for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
        return buf;
    };

    var saveAs = saveAs || (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob && navigator.msSaveOrOpenBlob.bind(navigator)) || (function (h) { if (typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) { return } var r = h.document, l = function () { return h.URL || h.webkitURL || h }, e = h.URL || h.webkitURL || h, n = r.createElementNS("http://www.w3.org/1999/xhtml", "a"), g = !h.externalHost && "download" in n, j = function (t) { var s = r.createEvent("MouseEvents"); s.initMouseEvent("click", true, false, h, 0, 0, 0, 0, 0, false, false, false, false, 0, null); t.dispatchEvent(s) }, o = h.webkitRequestFileSystem, p = h.requestFileSystem || o || h.mozRequestFileSystem, m = function (s) { (h.setImmediate || h.setTimeout)(function () { throw s }, 0) }, c = "application/octet-stream", k = 0, b = [], i = function () { var t = b.length; while (t--) { var s = b[t]; if (typeof s === "string") { e.revokeObjectURL(s) } else { s.remove() } } b.length = 0 }, q = function (t, s, w) { s = [].concat(s); var v = s.length; while (v--) { var x = t["on" + s[v]]; if (typeof x === "function") { try { x.call(t, w || t) } catch (u) { m(u) } } } }, f = function (t, v) { var w = this, C = t.type, F = false, y, x, s = function () { var G = l().createObjectURL(t); b.push(G); return G }, B = function () { q(w, "writestart progress write writeend".split(" ")) }, E = function () { if (F || !y) { y = s(t) } if (x) { x.location.href = y } else { if (navigator.userAgent.match(/7\.[\d\s\.]+Safari/) && typeof window.FileReader !== "undefined" && t.size <= 1024 * 1024 * 150) { var G = new window.FileReader(); G.readAsDataURL(t); G.onloadend = function () { var H = r.createElement("iframe"); H.src = G.result; H.style.display = "none"; r.body.appendChild(H); B(); return }; w.readyState = w.DONE; w.savedAs = w.SAVEDASUNKNOWN; return } else { window.open(y, "_blank"); w.readyState = w.DONE; w.savedAs = w.SAVEDASBLOB; B(); return } } }, A = function (G) { return function () { if (w.readyState !== w.DONE) { return G.apply(this, arguments) } } }, z = { create: true, exclusive: false }, D; w.readyState = w.INIT; if (!v) { v = "download" } if (g) { y = s(t); r = h.document; n = r.createElementNS("http://www.w3.org/1999/xhtml", "a"); n.href = y; n.download = v; var u = r.createEvent("MouseEvents"); u.initMouseEvent("click", true, false, h, 0, 0, 0, 0, 0, false, false, false, false, 0, null); n.dispatchEvent(u); w.readyState = w.DONE; w.savedAs = w.SAVEDASBLOB; B(); return } if (h.chrome && C && C !== c) { D = t.slice || t.webkitSlice; t = D.call(t, 0, t.size, c); F = true } if (o && v !== "download") { v += ".download" } if (C === c || o) { x = h } if (!p) { E(); return } k += t.size; p(h.TEMPORARY, k, A(function (G) { G.root.getDirectory("saved", z, A(function (H) { var I = function () { H.getFile(v, z, A(function (J) { J.createWriter(A(function (K) { K.onwriteend = function (L) { x.location.href = J.toURL(); b.push(J); w.readyState = w.DONE; w.savedAs = w.SAVEDASBLOB; q(w, "writeend", L) }; K.onerror = function () { var L = K.error; if (L.code !== L.ABORT_ERR) { E() } }; "writestart progress write abort".split(" ").forEach(function (L) { K["on" + L] = w["on" + L] }); K.write(t); w.abort = function () { K.abort(); w.readyState = w.DONE; w.savedAs = w.FAILED }; w.readyState = w.WRITING }), E) }), E) }; H.getFile(v, { create: false }, A(function (J) { J.remove(); I() }), A(function (J) { if (J.code === J.NOT_FOUND_ERR) { I() } else { E() } })) }), E) }), E) }, d = f.prototype, a = function (s, t) { return new f(s, t) }; d.abort = function () { var s = this; s.readyState = s.DONE; s.savedAs = s.FAILED; q(s, "abort") }; d.readyState = d.INIT = 0; d.WRITING = 1; d.DONE = 2; d.FAILED = -1; d.SAVEDASBLOB = 1; d.SAVEDASURI = 2; d.SAVEDASUNKNOWN = 3; d.error = d.onwritestart = d.onprogress = d.onwrite = d.onabort = d.onerror = d.onwriteend = null; h.addEventListener("unload", i, false); a.unload = function () { i(); h.removeEventListener("unload", i, false) }; return a }(typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content));
    saveAs(new Blob([s2ab(wbout)], { type: "application/octet-stream" }), 'sheetjs.xlsx');

MayaGi avatar Jan 01 '15 16:01 MayaGi

@SheetJSDev
I also met this problem. when I am writing hyperlink into Workbook, it's not working.

kenneth-hao avatar Jun 04 '15 11:06 kenneth-hao

+1

dr-dimitru avatar Nov 28 '15 23:11 dr-dimitru

+1

turengege avatar Jan 05 '16 06:01 turengege

+1 same problem. does anyone solve it ?

token97 avatar Feb 10 '16 13:02 token97

:+1:

guillaumervls avatar Mar 15 '16 16:03 guillaumervls

Looks like js-xlsx doesn't support adding Hyperlinks in exported excel. :disappointed:

RawSanj avatar May 03 '16 11:05 RawSanj

+1

martin-liu avatar May 13 '16 00:05 martin-liu

+1

danielbdias avatar May 16 '16 19:05 danielbdias

:+1:

jonahbron avatar Jun 29 '16 22:06 jonahbron

👍

lamebrain avatar Jul 22 '16 23:07 lamebrain

@SheetJSDev any chance this was solved?

rohithshenoyg avatar Sep 15 '16 18:09 rohithshenoyg

+1

SergiiBilyk avatar Dec 29 '16 11:12 SergiiBilyk

Has anyone able to find the fix or workaround for this issue ?

gangadharjannu avatar Mar 10 '17 07:03 gangadharjannu

@guillaumervls check this SO thread

On our end we moved to CSV where is it possible

dr-dimitru avatar Mar 10 '17 11:03 dr-dimitru

We've pushed support for XLSX/XLSM/XLSB/XLML. The BIFF2 format didn't support hyperlinks. If there is enough interest we'll look into ODS hyperlinks.

SheetJSDev avatar Mar 31 '17 01:03 SheetJSDev

Hi @SheetJSDev

Is there any reason XLS isn't on the supported list? I'm assuming there must be some technical limitation or something that stands in the way?

Is there any chance that XLS support may be added at some point?

mrcates avatar May 13 '19 20:05 mrcates

@rcates1 it is currently supported in BIFF8 XLS (used in versions starting from Excel 97), https://jsfiddle.net/sheetjs/24nxpek8/ is a simple test.

SheetJSDev avatar May 14 '19 09:05 SheetJSDev

@rcates1 it is currently supported in BIFF8 XLS (used in versions starting from Excel 97), https://jsfiddle.net/sheetjs/24nxpek8/ is a simple test.

@SheetJSDev The hyperlinks are not highlighted in MS OFFICE EXCEL. While it is highlighted in LiberOffice

ayush17 avatar Aug 08 '20 02:08 ayush17