SheetJS / sheetjs

📗 SheetJS Spreadsheet Data Toolkit -- New home https://git.sheetjs.com/SheetJS/sheetjs
https://sheetjs.com/
Apache License 2.0
35.15k stars 8k forks source link

Cell with text formated in HTML #2742

Open Seykotron opened 2 years ago

Seykotron commented 2 years ago

We are trying to export an excel that comes from an array of objects, in example:

{ "success": true, "hojas": { "Imputaciones": { "columnas": [ [ "Id", "Email", "Nombre Completo", "Puesto", "Equipos", "Áreas de Práctica", "Socio Referencia", "Manager Referencia", "Fecha Grabación", "Fecha Imputacion", "Centro de Facturación", "Cliente", "Exp. Referencia Antigua", "Exp. Referencia Actual", "Expediente", "Asunto", "Descripcion", "Tiempo", "Horas", "Minutos", "Estado" ] ], "columnas_con_fechas": [ [ "Fecha Grabación", "Fecha Imputacion" ] ], "columnas_con_html": [ [ "Descripcion" ] ], "valores": [ [ 292195, "xxxxx@xxxxx", "xxxxxxx", "xxxxxxx", "xxxxxxx", "xxxxxxx", null, null, "2022-07-13T07:17:40.173", "2022-07-13", "xxxxxxx", "xxxxxxx", "xxxxxxx", "xxxxxxx", "xxxxxxx", null, "<p>Reporte trimestral de los casos en FinRep.&nbsp;</p>\r\n\r\n<p>FPCE. Revisi&oacute;n de los m&oacute;dulos mandatory con sus responsables. Redacci&oacute;n de las respuestas y recopilaci&oacute;n de evidencias.&nbsp;</p>", 7, 7, 0, "Pendiente de revisar" ], ], "total": 8, "title": "Imputaciones" } }, "sheet": "Imputaciones", "title": "imputaciones_todas", "filas_por_pagina": 1000, "pagina": 1 }

The thing is that in excel the colum "Descripcion" its not showing as html but showing the html tags, etc.

I tried to fill the "h" property of the cell but it does not change the output.

I've search a lot and didnt see any issue like this,

can you help me please?

ljluestc commented 3 months ago
const XLSX = require('xlsx');
const htmlToText = require('html-to-text'); // This library helps to convert HTML to plain text

// Sample data
const data = {
    "success": true,
    "hojas": {
        "Imputaciones": {
            "columnas": [
                ["Id", "Email", "Nombre Completo", "Puesto", "Equipos", "Áreas de Práctica", "Socio Referencia", "Manager Referencia", "Fecha Grabación", "Fecha Imputacion", "Centro de Facturación", "Cliente", "Exp. Referencia Antigua", "Exp. Referencia Actual", "Expediente", "Asunto", "Descripcion", "Tiempo", "Horas", "Minutos", "Estado"]
            ],
            "columnas_con_fechas": [
                ["Fecha Grabación", "Fecha Imputacion"]
            ],
            "columnas_con_html": [
                ["Descripcion"]
            ],
            "valores": [
                [292195, "xxxxx@xxxxx", "xxxxxxx", "xxxxxxx", "xxxxxxx", "xxxxxxx", null, null, "2022-07-13T07:17:40.173", "2022-07-13", "xxxxxxx", "xxxxxxx", "xxxxxxx", "xxxxxxx", "xxxxxxx", null, "<p>Reporte trimestral de los casos en FinRep.&nbsp;</p>\r\n\r\n<p>FPCE. Revisi&oacute;n de los m&oacute;dulos mandatory con sus responsables. Redacci&oacute;n de las respuestas y recopilaci&oacute;n de evidencias.&nbsp;</p>", 7, 7, 0, "Pendiente de revisar"]
            ],
            "total": 8,
            "title": "Imputaciones"
        }
    },
    "sheet": "Imputaciones",
    "title": "imputaciones_todas",
    "filas_por_pagina": 1000,
    "pagina": 1
};

// Extract data for the worksheet
const sheetData = [data.hojas.Imputaciones.columnas[0]];
data.hojas.Imputaciones.valores.forEach(row => {
    // Convert HTML content to plain text
    row[16] = htmlToText.convert(row[16]);
    sheetData.push(row);
});

// Create a new workbook and a worksheet
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.aoa_to_sheet(sheetData);

// Add the worksheet to the workbook
XLSX.utils.book_append_sheet(wb, ws, data.sheet);

// Write the workbook to a file
XLSX.writeFile(wb, `${data.title}.xlsx`);