excelR icon indicating copy to clipboard operation
excelR copied to clipboard

Making part of the table read-only

Open gueyenono opened this issue 4 years ago • 9 comments

I was wondering if there was a way to make part of an excelTable read-only (e.g. a cell, several specific cells, a row, a column...). The excelTable() function has the editable argument, which makes the entire table editable or not.

Thank you

gueyenono avatar Jul 17 '21 13:07 gueyenono

It is possible with updateTable.

    updateTable <- "function(instance, td, col, row, value, label, cellName) {
    
      var header = instance.jexcel.getHeader (col);
      
      if (header == 'Your header name') {
        td.classList.add ('readonly') ;
      }
    }


    excelTable(
      data = df,
      updateTable = htmlwidgets::JS(updateTable)
    )

daeyoonlee avatar Jul 19 '21 06:07 daeyoonlee

Okay, so you need to do it with JavaScript. I'll try it out then close the comment once it works. Thank you so much!

gueyenono avatar Jul 19 '21 06:07 gueyenono

@gueyenono Thanks you for the question. This could be a functionality that we could add it the future releases. I'll label this as a feature request.

Swechhya avatar Jul 22 '21 03:07 Swechhya

@daeyoonlee is it possible to freeze (readonly), 2 or more variables ? Thanks

mos790 avatar Sep 30 '21 23:09 mos790

@daeyoonlee is it possible to freeze (readonly), 2 or more variables ? Thanks

I'm not really comfortable with JS, but this work.

updateTable = "function(instance, td, col, row, value, label, cellName) {
      var header = instance.jexcel.getHeader(col);
      if (header != 'Availability') {
        td.classList.add('readonly') ;
      } 
    }"


data = data.frame(Model = c('Mazda', 'Pegeout', 'Honda Fit', 'Honda CRV'),
                  Date=c('2006-01-01', '2005-01-01','2004-01-01', '2003-01-01' ),
                  Availability = c(TRUE, FALSE, TRUE, TRUE))


columns = data.frame(title=c('Model', 'Date', 'Availability'),
                     width= c(500, 500, 300),
                     type=c('text', 'calendar', 'checkbox'))

excelTable(data=data, columns = columns, updateTable = htmlwidgets::JS(updateTable))

mos790 avatar Oct 05 '21 13:10 mos790

@mostafa790 works well, but then you get the greyed out look, not very readable. I have a situation of two reference columns and one column to be edited based on the values in the reference columns.

Ljupch0 avatar Dec 01 '21 10:12 Ljupch0

@mostafa790 hello I change your code into

my_col=c("Model","Date")
updateTable = "function(instance, td, col, row, value, label, cellName) {
      var header = instance.jexcel.getHeader(col);
      if (header %in% my_col) {
         td.classList.add('readonly') ;
      } 
   }"

But this doesn't seem work. I'm don't know anything about JS. Can you fix this?

zhangjunjiang avatar Jan 25 '22 00:01 zhangjunjiang

hello @zhangjunjiang , Sorry for my late message.

You cant combine R and JS code, try to remplace header %in% my_col by !(header in my_col)

Good luck :)

mos790 avatar Feb 11 '22 11:02 mos790

Hi

Thanks to @Swechhya for publishing this package and for all users who responded to this thread. @mostafa790 provided a workaround for rendering columns read-only. I was wondering if there was a similar mechanisms for rendering rows read-only.

More specifically, is there a way to make rows stored in reactive variables read only?

Thanks in advance

sbihorel avatar Nov 24 '22 15:11 sbihorel