Home > Back-end >  How to set font color when appending values?
How to set font color when appending values?

Time:12-09

I can write some values to a specific range in Google Sheets, and at the same time set the font color:

function changeColor()
{var values = [["just","some","words"]];
var range = 
SpreadsheetApp.getActive().getSheetByName("Sheet1").getRange("A100:C100").setValues(values);
range.setValues(values);
range.setFontColor("#FF0000");
}

Or I can append some values to the sheet:

function writeValues (){
var values = ["just","some","words"];
SpreadsheetApp.getActiveSheet().appendRow(values);
}

But how to append values and set the font color at the same time? Can I do it at the time of appending or do I have to work out where the values went somehow, and then apply the color?

CodePudding user response:

Thanks Diego, while you were posting that answer I just came up with this:

function writeValues (){
var values = [["just","some","words"]];
sheet = SpreadsheetApp.getActiveSheet().appendRow(values[0]);
sheet.getRange(sheet.getLastRow(),1,1,3).setFontColor("#0000FF");

Which I suppose is doing something similar using getLastRow() - and it works which is the main thing. I'll compare the two and work from there, so thanks y'all for your help.

CodePudding user response:

You can get started with this:

function writeValues (){
var values = ["just","some","words"];
var ss = SpreadsheetApp.getActiveSheet();
var range = ss.getRange(1,1, ss.getLastRow(), 3);
if (ss.appendRow(values) == true)
{
range.setFontColor("#FF0000").setValues(values);
} 
}

One problem I faced with this though is that the first row should contain a value first so that the font color will append properly to the last row. You can modify this to set a few try-catch or error handling methods.

  • Related