Export only certain data #3021

Closed
opened 2023-10-26 09:27:18 +00:00 by VitseA · 1 comment

Hi,

is it possible to export only certain data.

For example :

image

I would like to export only Sexe = F or Sexe = M

you can find my actual code below (based on html table)

const tbl1 = document.getElementById("table1");

var ws = XLSX.utils.table_to_sheet(tbl1, {raw: true});

ws['!cols'] = [];
ws['!cols'][0] = { hidden: true }; 
ws['!cols'][2] = { hidden: true }; 

var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");

XLSX.writeFile(wb, 'output.' + type);

Thanks in advance for your help

Regard

Alexander

Hi, is it possible to export only certain data. For example : ![image](/attachments/60b03cbd-386a-4f75-a13b-1630982e757b) I would like to export only Sexe = F or Sexe = M you can find my actual code below (based on html table) ``` const tbl1 = document.getElementById("table1"); var ws = XLSX.utils.table_to_sheet(tbl1, {raw: true}); ws['!cols'] = []; ws['!cols'][0] = { hidden: true }; ws['!cols'][2] = { hidden: true }; var wb = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(wb, ws, "Sheet1"); XLSX.writeFile(wb, 'output.' + type); ``` Thanks in advance for your help Regard Alexander
2.8 KiB
Owner

You can hide individual rows:

ws["!rows"] = [];
/* hide Excel row 4 = SheetJS row 3 */
ws["!rows"][3] = { hpx: 20, hidden: true };

If you want to remove the data you need to generate a new worksheet or manually remove rows. The easiest approach is to roundtrip through an array of objects:

// ... after you generate the initial worksheet `ws`

/* generate array of objects */
var aoo = XLSX.utils.sheet_to_json(ws);

/* only select rows where the `sexe` field is `"M"` */
aoo = aoo.filter(function(row) { return row.sexe == "M"; });

/* only extract `nom` field */
aoo = aoo.map(function(row) { return {nom: row.nom}; });

/* generate new worksheet */
ws = XLSX.utils.json_to_sheet(aoo);

// create workbook, add worksheet, export ...
You can [hide individual rows](https://docs.sheetjs.com/docs/csf/features/rowprops#row-visibility): ```js ws["!rows"] = []; /* hide Excel row 4 = SheetJS row 3 */ ws["!rows"][3] = { hpx: 20, hidden: true }; ``` If you want to *remove* the data you need to generate a new worksheet or manually remove rows. The easiest approach is to roundtrip through an array of objects: ```js // ... after you generate the initial worksheet `ws` /* generate array of objects */ var aoo = XLSX.utils.sheet_to_json(ws); /* only select rows where the `sexe` field is `"M"` */ aoo = aoo.filter(function(row) { return row.sexe == "M"; }); /* only extract `nom` field */ aoo = aoo.map(function(row) { return {nom: row.nom}; }); /* generate new worksheet */ ws = XLSX.utils.json_to_sheet(aoo); // create workbook, add worksheet, export ... ```
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#3021
No description provided.