Rhandsontable:使用' value'的函数进行条件格式化字符串

时间:2018-01-30 06:50:51

标签: javascript r conditional-formatting rhandsontable

我试图通过调整单元格的子集字符串值来调整R中handsonstable的单个单元格:

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。

由于

亚历

1 个答案:

答案 0 :(得分: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';
               }
               }")

希望它有所帮助!