I want to set Spreadsheet cell background color and text size. I use this Java code to set the text into the cell but I can't find a solution how to set the style.
CellData setUserEnteredValue = new CellData()
.setUserEnteredValue(new ExtendedValue()
.setStringValue("cell text"));
Is there any solution?
Google Sheets conditional formatting allows you to change the aspect of a cell—that is, a cell's background color or the style of the cell's text—based on rules you set. Every rule you set is an if/then statement. For example, you might say "If cell B2 is empty, then change that cell's background color to black."
I had to go through allot of useless answers, but this worked for me. Here you go:
requests.add(new Request()
.setRepeatCell(new RepeatCellRequest()
.setCell(new CellData()
.setUserEnteredValue( new ExtendedValue().setStringValue("cell text"))
.setUserEnteredFormat(new CellFormat()
.setBackgroundColor(new Color()
.setRed(Float.valueOf("1"))
.setGreen(Float.valueOf("0"))
.setBlue(Float.valueOf("0"))
)
.setTextFormat(new TextFormat()
.setFontSize(15)
.setBold(Boolean.TRUE)
)
)
)
.setRange(new GridRange()
.setSheetId(sheetID)
.setStartRowIndex(1)
.setEndRowIndex(0)
.setStartColumnIndex(0)
.setEndColumnIndex(1)
)
.setFields("*")
)
);
// For handling single cells this is how you do it, where j and k are row and columns in g sheet .
CellData setUserEnteredValue =new CellData()
.setUserEnteredValue(new ExtendedValue()
.setStringValue(maxs));
List<CellData> values1.add(setUserEnteredValue);
requests.add(new Request()
.setUpdateCells(new UpdateCellsRequest()
.setStart(new GridCoordinate()
.setRowIndex(j)
.setColumnIndex(k))
.setRows(Arrays.asList(
new RowData().setValues(values1)))
.setFields("userEnteredValue,userEnteredFormat.backgroundColor")));
CellFormat myFormat = new CellFormat();
myFormat.setBackgroundColor(new Color().setRed(Float.valueOf("1"))); // red background
setUserEnteredValue.setUserEnteredFormat(myFormat);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With