0

我试图通过调节单元格的子集字符串值来为 R 中的 handonstable 的单个单元格着色:

library(rhandsontable)

DF = data.frame( bool = TRUE,val = 1:10, big = LETTERS[1:10],
                 small = letters[1:10],
                 stringsAsFactors = FALSE)

###Checkboxes not Present/Entire row not highlighted
rhandsontable(DF, readOnly = FALSE, width = 750, height = 300) %>%
    hot_cols(renderer = "
             function (instance, td, row, col, prop, value, cellProperties) {
             Handsontable.renderers.NumericRenderer.apply(this, arguments);

             if (value == 'C') {
             td.style.background = 'pink';
             } else if (value == 'D') {
             td.style.background = 'green';
             }
             }")

所以上面的工作正常,但是当我添加一个子集字符串的函数时,整个事情就崩溃了:

library(rhandsontable)

DF = data.frame( bool = TRUE,val = 1:10, big = LETTERS[1:10],
                 small = letters[1:10],
                 stringsAsFactors = FALSE)

###Checkboxes not Present/Entire row not highlighted
rhandsontable(DF, readOnly = FALSE, width = 750, height = 300) %>%
    hot_cols(renderer = "
             function (instance, td, row, col, prop, value, cellProperties) {
             Handsontable.renderers.NumericRenderer.apply(this, arguments);


             if (value.substring(0, 1) == 'C') {
             td.style.background = 'pink';
             } else if (value == 'D') {
             td.style.background = 'green';
             }
             }")

我需要使用 substring 函数来确定单元格内容的前几个字母,以便我可以相应地为它们着色。我该怎么做?

我对Java的了解也是零。只知道R。

谢谢

亚历克斯

4

1 回答 1

0

在使用之前,您必须使用toString方法将值转换为字符串substring

您的代码将如下所示:

DF = data.frame( bool = TRUE,val = 1:10, big = LETTERS[1:10],
                 small = letters[1:10],
                 stringsAsFactors = FALSE)

###Checkboxes not Present/Entire row not highlighted

    rhandsontable(DF, readOnly = FALSE, width = 750, height = 300) %>%
      hot_cols(renderer = "
               function (instance, td, row, col, prop, value, cellProperties) {
               Handsontable.renderers.NumericRenderer.apply(this, arguments);
               if (value.toString().substring(0, 1) == 'C') {
               td.style.background = 'pink';
               } else if (value == 'D') {
               td.style.background = 'green';
               }
               }")

希望能帮助到你!

于 2018-01-30T13:45:52.577 回答