Export href to excel

Export href to excel

SrViniciusSrVinicius Posts: 4Questions: 3Answers: 0

Hello everyone!

I have the following task: Exporting href attributes when generating an Excel file with DataTables. I'm using the latest version. I searched but couldn't find anything, only some posts saying it's quite "difficult." Is there currently any way to do this? In my project, i just use the default buttons to export to excel

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Sure its possible, but the "difficult" part still applies.

    You'd need to modify the XML that is created to create the appropriate link tag structure and apply whatever attributes are needed in the OpenSpreadsheet spec. I'm afraid that is not something that the Buttons export currently does.

    It might be worth looking at SheetJS if you are considering building a more complex / complete XLSX exporter for DataTables. My Excel export was only intended for basic data.

    Allan

  • lucas_araujolucas_araujo Posts: 12Questions: 5Answers: 0

    I think with using the customize it is doable

  • chocchoc Posts: 95Questions: 11Answers: 8
    edited October 3

    Are you saying that you have cells that contain a link (or whatever) with an href attribute?

    As @lucas_araujo saied, it's doable.

    I will also do the same thing recently, if you can provide a demo I'm happy to have a look.
    I have already played around with the customize function before in DT1 where I added additional content into the Excel cells.

    But in your case, if you don't need to make new rows in Excel. I guess what you need is the exportOptions:

                    exportOptions: {
                        stripHtml: false,
                        format: {
                            body: function (data, row, column) {
                                // do the extraction of the href
                                return data;
                            }
                        }
                    },
    

    Note the use of stripHtml: false, since I parse the html by myself, so I turned it off.

  • rf1234rf1234 Posts: 2,990Questions: 87Answers: 421

    Here is something on hyperlinks in OpenOffice. Something like that and probably more should go into "// do the extraction of the href" above.

    https://wiki.openoffice.org/wiki/Documentation/How_Tos/Calc:_HYPERLINK_function

  • chocchoc Posts: 95Questions: 11Answers: 8
    edited October 4

    If you don't mind that the hyperlink is not automatically render when you open it. (You need to click the cell and press Enter key to render)

    You can simply use:

                            buttons: [
                            {
                              extend: 'excelHtml5',
                              exportOptions: {
                                format: {
                                  body: function (data, row, column) {
                                    if ( column === 0) {
                                      console.log("data:", data)
                                      const href = data.match(/href=['"]([^'"]*)['"]/);
                                      const text = data.match(/<a[^>]*>([^<]*)<\/a>/); // if it's a <a> tag
                                      if (href && text) {
                                        data = `=HYPERLINK("${href[1]}", "${text[1]}")`;
                                      }
                                      return data
                                    }
                                    return data;
                                  }
                                }
                              }
                            }
                          ],
    

    I don't know how to render cells automatically without clicking on it or pressing Enter. If anyone knows how to do this, please let me know!

    (I have Formulas > Calculation Options > Automatic in Excel already)

  • rf1234rf1234 Posts: 2,990Questions: 87Answers: 421

    @choc Cool! Thanks a lot!

Sign In or Register to comment.