I am trying to format a cell to have multiple font colors using a function in Google Apps Script. I am unable to find any documentation on it. Also, using getFontColor()
doesn't return anything useful.
Is there any way to programmatically reproduce this functionality
that is available to users via the Google Sheets web UI?
The Sheets API is a bit daunting to start using, but allows very fine-grained control over your spreadsheets. You'll have to enable it, as it is an "Advanced Service". I strongly recommend reviewing the Sample Codelab.
With the Sheets API, the
TextFormatRun
property can be manipulated on a cell-by-cell basis. Note:This example uses it to adjust the green value of text, increasing from 0 to 100% over the length of a string in the active cell. Adjust to suit your needs.
Edit: Depending on how the value of the cells to be formatted are set, including the value of the cell in the same request may be necessary as well. See this example on the issue tracker
As on July 2018, Apps-Script support changing individual text colors and other font related styles. Two methods are added to
SpreadsheetApp
.newTextStyle()
andnewRichTextValue()
. The following apps-script changes such fontstyles in A1. For best effects, Use a lengthy string(30 characters or more).