Table Column Formatting
As far as I know, you can only format the following using CSS on the <col>
element:
- background-color
- border
- width
- visibility
This page has more info.
Herb is right - it's better to style the <td>
's directly. What I do is the following:
<style type="text/css">
#mytable tr > td:first-child { color: red;} /* first column */
#mytable tr > td:first-child + td { color: green;} /* second column */
#mytable tr > td:first-child + td + td { color: blue;} /* third column */
</style>
</head>
<body>
<table id="mytable">
<tr>
<td>text 1</td>
<td>text 2</td>
<td>text 3</td>
</tr>
<tr>
<td>text 4</td>
<td>text 5</td>
<td>text 6</td>
</tr>
</table>
This won't work in IE however.
vba applying format for table column
As far as I understood you would like to do something like that
Sub FormatIt()
Dim v As ListColumn
Set v = Sheet1.ListObjects("MyTaqble").ListColumns("Balance")
v.Range.Style = "Currency"
End Sub
or
Sub FormatItA()
Dim v As ListColumn
Set v = Sheet1.ListObjects("MyTaqble").ListColumns("Balance")
v.Range.NumberFormat = _
"_([$€-x-euro2] * #,##0.00_);_([$€-x-euro2] * (#,##0.00);_([$€-x-euro2] * ""-""??_);_(@_)"
End Sub
Table column conditional js formatting
The below script will work for your requirement, HERE is a working example for your reference.
if you have multiple tables just give it a class and amend the script as required.
var trTags = document.getElementsByTagName("tr");
for (var i = 0; i < trTags.length; i++) {
var tdFourthEl = trTags[i].children[3]; // starts with 0, so 3 is the 4th element
if (tdFourthEl.innerText < -2) {
tdFourthEl.style.backgroundColor = "red";
} else if (tdFourthEl.innerText > 2) {
tdFourthEl.style.backgroundColor = "green";
}
}
How do I create a table in R with conditional formatting and row and column totals?
library(janitor); library(gt)
df %>%
tabyl(row_groups, col_groups) %>%
adorn_percentages("all") %>%
adorn_totals(c("col")) -> df_tabyl
gt(df_tabyl) %>%
data_color(columns = col_group1:col_group4,
colors = scales::col_numeric(
palette = c(red_color, yellow_color, green_color),
domain = range(df_tabyl[1:4,2:5])
)
) %>%
fmt_percent(columns = -row_groups,
rows = everything()) %>%
summary_rows(
columns = -row_groups,
fns = list("Total" = "sum"),
formatter = fmt_percent
)
The coloring varies with your example b/c the col_numeric
function maps the colors linearly along the three provided colors, and 11% is only 1/3 of the way between 0% and 33%. Not sure what approach you expect.
Problems with conditional cell formatting for table in R-Markdown (Quarto)
If you change from background
to color
as the argument in cell_spec()
your code works as expected.
If you want a colored background, you need to remove the last line of code kableExtra::kable_styling("striped", full_width = T)
.
If you want full width, you need to adjust latex options in kable_styling
:
beans_stock %>%
mutate(
Progress = kableExtra::cell_spec(
Progress, background = ifelse(Progress > 70,"green",
ifelse(Progress < 30,"red","orange")))
) %>%
kableExtra::kable(escape = FALSE,format = "latex", booktabs = T) %>%
kableExtra::kable_styling(latex_options="scale_down")
How to reactively format data table columns?
The following seems to work. However, I'm not sure why your initial approach doesn't work - it looks good to me.
library(dplyr)
library(DT)
library(shiny)
library(shinyWidgets)
library(tidyverse)
ui <-
fluidPage(fluidRow(
column(
width = 8,
h3("Data table:"),
tableOutput("data"),
h3("Sum the data table columns:"),
radioButtons(
inputId = "grouping",
label = NULL,
choiceNames = c("By period 1", "By period 2"),
choiceValues = c("Period_1", "Period_2"),
selected = "Period_1",
inline = TRUE
),
DT::dataTableOutput("sums")
)
))
server <- function(input, output, session) {
data <- reactive({
data.frame(
Period_1 = c("2020-01", "2020-02", "2020-03", "2020-01", "2020-02", "2020-03"),
Period_2 = c(1, 2, 3, 3, 1, 2),
ColA = c(1000.01, 20, 30, 40, 50, 60),
ColB = c(15.06, 25, 35, 45, 55, 65)
)
})
summed_data <- reactive({
data() %>%
group_by(!!sym(input$grouping)) %>%
select("ColA", "ColB") %>% summarise(across(everything(), sum))
})
output$data <- renderTable(data())
output$sums <- renderDT({
print(names(summed_data())[1])
datatable(
data = summed_data(),
rownames = FALSE,
options = list(columnDefs = list(
list(className = 'dt-left', targets = 0),
list(className = 'dt-center', targets = seq_len(ncol(summed_data())) - 1)
))
)
})
}
shinyApp(ui, server)
How to CREATE TABLE with formatted column
Use CHECK
Constraint:
CREATE TABLE YourTable (
Col1 VARCHAR(10),
CONSTRAINT MyContraint CHECK (Col1 LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9]')
);
Powershell adjust the format table width
You can use Format-Table
with a calculated property to limit the width of the Name
column; e.g.:
... | Format-Table @{ Name='Name'; Expression = 'Name'; Width = 60 }, Resolution
Longer values are then truncated with ...
/ …
in Windows PowerShell / PowerShell (Core) 7+. You may add -Wrap
to show them in full, spread across as many lines as needed.
The alternative - which won't render nicely on screen - is to pipe to Out-String
and use its -Width
parameter with a terminal-column count large enough to accommodate all table columns:
... | Out-String -Width 300
Note: With your output objects, Format-Table
formatting is implied (they have 4 or fewer properties), which is why there's no explicit call to it above.
As an aside:
Format-Table -Autosize
does not help, as it doesn't ensure that all columns are shown - see this answer for more information.
Sample code:
[pscustomobject] @{
Name = 'x' * 80 + 'y'
Resolution = '1920x1080'
},
[pscustomobject] @{
Name = 'x' * 280 + 'z'
Resolution = '2560x1600'
} |
Format-Table -Wrap @{ Name='Name'; Expression = 'Name'; Width = 60 }, Resolution
Output:
Name Resolution
---- ----------
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1920x1080
xxxxxxxxxxxxxxxxxxxxy
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 2560x1600
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz
Format table rows based on the value in specfic column changing in excel
I would use a helper column as well - but calculating the "ID" of the employee.
Formula in C2
=IF(A2<>A1,SUM(C1,1),C1)
Then you can use the same logic as you showed us in your question: =MOD($C2,2)=0
as your format condition.
Related Topics
CSS Sprites with Dynamic Sizing
Ie8's Rendering of Transparent Pngs Is Fubared on My Site
Keeping CSS Out of Js in Angular 2/Angular-Cli
Iframe Border Showing in Webview React Native
How to Restrict Character Limit by Line or # of Characters with CSS
Why Is Transition on 'Margin' and 'Padding' Laggy in Webkit Browsers
CSS Checkboxes & Radio Buttons When Input Is Inside Label
Webkit Scrollbar CSS, Always a White Box in Corner
How to Get My Page Headers to Resize Using Responsive Layout
Browsers Won't Read Updated CSS
CSS Selector for an Element Having Class .A and Class .B
Have Border-Radius Cover Inner Divs
Rotating Glyphicons/Font Awesome in Bootstrap
Ems to Pixel Conversion - Why 62.5% and Not 6.25%
Google Font API Uses Browser Detection. How to Get All Font Variations for Font-Face