Author Topic: Export to Excel doesnt convert html cells to text  (Read 2996 times)

akraines

  • Pro Ultimate
  • Newbie
  • *
  • Posts: 34
    • View Profile
Export to Excel doesnt convert html cells to text
« on: December 19, 2019, 06:47:07 pm »
I have a column with dataType = 'html', and the cells contain html, I'd like to control the export to excel option to strip out the html. How can this be done?

akraines

  • Pro Ultimate
  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: Export to Excel doesnt convert html cells to text
« Reply #1 on: December 19, 2019, 08:40:57 pm »
In the meantime I used the following approach:
//Remove all html tags from exported data. (can be tweaked to only act on html columns)
.map(w1.sheets, s => _.map(s.rows, r=> _.map(r.cells, c=> _.isString(c.value) ? c.value = c.value.replace(/<[^>]*>/g,"") : "")));

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Export to Excel doesnt convert html cells to text
« Reply #2 on: December 19, 2019, 09:35:59 pm »
Good approach!

Another way is to write column.render callback for html column and remove html tags from ui.cellData when ui.Export is true.

Code: [Select]
render: function( ui ){
  if(ui.Export){
    return ui.cellData.replace(/<[^>]*>/g,"")
  }
}

akraines

  • Pro Ultimate
  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: Export to Excel doesnt convert html cells to text
« Reply #3 on: December 19, 2019, 09:41:46 pm »
Thanks - that is much better - it will work for all forms of export - so I don't need to special case xslx.

Sivakumar

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: Export to Excel doesnt convert html cells to text
« Reply #4 on: May 05, 2020, 08:58:18 am »
Getting the following error in console

Cannot read property 'replace' of undefined.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6309
    • View Profile
Re: Export to Excel doesnt convert html cells to text
« Reply #5 on: May 05, 2020, 09:09:49 am »
This error may be caused if there are undefined values in your column data.

in which case it may be updated to

Code: [Select]
render: function( ui ){
  if(ui.Export){
    return ( ui.cellData || "" ).replace(/<[^>]*>/g,"")
  }
}