1

In Google Sheets using Apps Script, I want to read in the font color in many cells, change some values (change colors) in the array, and write them back to the cells.

I have previously used getFontColors() and it worked, but it got deprecated so trying to update my script. I can read in the font colors using getFontColorObjects(), I can display them in the execution log (outRngFontColors.asRgbColor().asHexString() ), but I don't know how to change the values in the Color[][] array (ex. #000000 to #ff00ff). So how do I do that? I only want to read the font colors once and then write them back out only once.

var ss = SpreadsheetApp.getActiveSpreadsheet();<br>
var sheet = ss.getActiveSheet();<br>
var outColRange = sheet.getRange(1, 1, 3);<br>
var outRngFontColors = outColRange.getFontColorObjects();
1

1 Answer 1

0

get and set

In Google Apps Scripts, the prefix get is used in many methods that obtain a value or object. In the same way, the prefix set is used in many methods that change a value or object.

So, if you already found getFontColors returns the values, then it is very likely that the method that changes those values might be setFontColors. This could be easily confirmed directly in the Google Apps Script code editor by using the autocomplete feature. Another way is to go to https://developers.google.com/apps-script/ and search for the method. You might also go https://developers.google.com/apps-script/reference/spreadsheet/range. This page list all the methods for Class Range from the Spreadsheet Service.

The Google Apps Script code editor will show you help to show the method syntax. This usually includes examples.

https://developers.google.com/apps-script/reference/spreadsheet/range#setFontColorObjects(Color) also includes an example:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var colorAccent1 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT1)
    .build();
var colorAccent2 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT2)
    .build();
var colorAccent3 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT3)
    .build();
var colorAccent4 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT4)
    .build();

var colors = [
  [colorAccent1, colorAccent2],
  [colorAccent3, colorAccent4]
];

var cell = sheet.getRange("B5:C6");
cell.setFontColorObjects(colors);
1
  • Yes, I already had my program working with getFontColors and setFontColors. What I was looking for is how to manipulate the Colors[][] array when using getFontColorObjects and setFontColorObjects. I did neglect to say that I did go into the developers.google.com link before asking this question. May 8 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.