javagoogle-sheets-apigoogle-api-java-clienttext-formatting

Format Google Sheet text in Java


I'm using the Google sheet API, version 4, to write to a spreadsheet. It's working good, but I need to format the text (bold, underline,...) and I can't seem to find any way to do this using the Google Java API client.

I know this is feasible by requesting via GET and POST to http://sheets.googleapis.com but I already developed it with the Java client and I would really appreciate not having to build up everything again.

Does anyone have any thoughts on this?

Just in case it was not clear enough, I'm using the Java Google Sheet client


Solution

  • By using the CellData class it's possible to format the cell with background colors, font family, size and many other options. This is an example of how to use it:

        CellData setUserEnteredValue = new CellData()
                .setUserEnteredValue(new ExtendedValue().setStringValue("example text"));
    
        CellFormat cellFormat = new CellFormat();
        cellFormat.setTextFormat(new TextFormat().setBold(true));
    
        setUserEnteredValue.setUserEnteredFormat(cellFormat);
    

    Here's a link to the documentation