excelR icon indicating copy to clipboard operation
excelR copied to clipboard

getComments() removes expected column names from input$table

Open Mikea0228 opened this issue 4 years ago • 1 comments

After use of getComments() the elements of input$table change from ; "data", "colHeaders", "colTypes, and "forSelectedVals" to just "comments". Seeing as this updates input$table, any observeEvent(input$table) that calls excel_to_r() will fail unless an explicit check of the names of input$table and will not be able to access the data in table until an explicit change is made to the data in the table.

Possible expected behavior would be for input$table to always have "comments" as an element which gets populated upon calls to getComments()

Steps to reproduce

  1. run the below shiny app
  2. Set the value of cell A1 to any number, observe the names of input are printed to the console
  3. Press "Set Comments to cell A1", comment is correctly set
  4. Press "Get Comments from cell A1", comment is correctly pulled to text box but observe that only "comments" is printed to console and hence excel_to_r() will fail
library(excelR)
library(shiny)

shinyApp(
  ui = fluidPage(
    actionButton('set', 'Set Comments to cell A1'),
    actionButton('get', 'Get Comments from cell A1'),
    textAreaInput("fetchedComment", "Comments from A1:"),
    excelOutput("table", height = 175)
  ),
  
  server = function(input, output, session) {
    output$table <-
      renderExcel(excelTable(data = head(iris, 2), allowComments = TRUE))
    
    # Set the comment
    observeEvent(input$set, {
      setComments("table", "A1", "This is a comment")
    })
    
    # Get the comment
    observeEvent(input$get, {
      getComments("table", "A1")
    })
    
    # Print the comment to the text area
    observeEvent(input$table, {
      updateTextAreaInput(session, 'fetchedComment', value = input$table$comment)
      cat("names of input\n")
      print(names(input$table))
      
      if ("data" %in% names(input$table)) {
        cat("\nThe table data is currently available\n")
        print(head(excel_to_R(input$table), 1))
        
      } else{
        cat("\nThe table data is not available")
      }
    })
  }
)

Mikea0228 avatar Mar 12 '21 11:03 Mikea0228

It looks like the issues lies in jexcel.js found in htmlwidgets, relevant code chunk:

        // This function is used to get comments  from table
        Shiny.addCustomMessageHandler("excelR:getComments", function(message) {
          
          var el = document.getElementById(message[0]);
          if (el) {
            var comments = message[1] ? el.excel.getComments(message[1]): el.excel.getComments(null);
            
            Shiny.setInputValue(message[0], 
              {
                comments
              });
            }
          });
        }

Mikea0228 avatar Mar 12 '21 11:03 Mikea0228