Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set cell background color and text size using Google Java API

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?

like image 511
Peter Penzov Avatar asked Dec 30 '16 08:12

Peter Penzov


People also ask

Can you do an if statement in Google sheets based on color?

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."


2 Answers

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("*")
                )
        );
like image 182
Julian Avatar answered Oct 12 '22 11:10

Julian


// 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);
like image 24
Tank Avatar answered Oct 12 '22 11:10

Tank