How can I add formatting, specifically cell borders, to the copy button?

How can I add formatting, specifically cell borders, to the copy button?

PakePake Posts: 2Questions: 1Answers: 0

Hello all. Complete rookie here.

I have many users of reports I generate using datatables that want to copy/paste my tables into Word and Excel, but don't have Excel formatting skills.

I'd like to help them out by customizing the output of the copy button to include formatting that will include cell borders (pipe dream: bold headers).

I have no real javascript experience, but use RStudio, and the following code to generate my table.

datatable(data1,class='cell-border',extensions='Buttons',rownames=FALSE,filter=c("top"),options=list(dom='Blfrtip',buttons=list(list(extend='copy',exportOptions=list(modifier=list(page='current'),exportData=list(stripHtml=list('false')))))))
**Debugger code (debug.datatables.net)**: 
**Error messages shown**: 
**Description of problem**: 

I've seen that there are format.header and format.body here: https://datatables.net/reference/api/buttons.exportData()
but cannot find an example where cell borders are a part of the formatting that happens.

Is there a means of doing this, especially that might play nicely with the R version of coding?

I've asked a simarly question on stack overflow here: https://stackoverflow.com/questions/66784020/how-can-i-add-a-table-to-my-rmarkdown-html-output-that-will-paste-into-excel-wit but with no answers.

Answers

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    edited March 2021

    Does this example get you started?

    Kevin

  • PakePake Posts: 2Questions: 1Answers: 0

    Thank you for the link. This is definitely a good sign for hope that what I'm trying to accomplish is possible. Just to test as an intermediate step, I copied the cord there and I can't seem to get it working with:

    `jscode<-"function ( xlsx ){var sheet = xlsx.xl.worksheets['sheet1.xml']; $('row c[r*=10]', sheet).attr( 's', '25' ); }"

    datatable(data1,class='cell-border',extensions='Buttons',rownames=FALSE,filter=c("top"),options=list(dom='Blfrtip',buttons=list(list(extend='excel',customize=fromJSON(jscode)))))`

    I get an error message:

    "Error: lexical error: invalid string in json text." with an arrow pointed at the word "function"

    When I remove the fromJSON(), my datatable will render, but my button is a spinning button of doom.

    Getting this to work is surely hurdle one, but ultimately I imagine I'm going to have trouble translating that snip of javascript to apply the attr() to all the data, and to point the formatting just to the clipboard and not an exported file.

This discussion has been closed.