Ayush Sharma
Ayush Sharma

Reputation: 23

Adding background color to the selected cell in Google sheets by script

All,

I am trying to make a timestamp sheet. The idea is whenever a user will press a button the selected cell will get the current time and background color will change to RED. I managed to fix the time part but not able to get it change color. The closest I got to it is :

function timeStampM() {
SpreadsheetApp.getActiveSheet()
    .getActiveCell()
   .setValue(new Date());

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var actCell = sheet.getActiveCell();
var actData = actCell.getValue();
var actRow = actCell.getRow();
if (actData != '' && actRow != 1)  //Leaving out empty and header rows
{
range.getRange(actRow, 2).setBackground('red');
}
}

This colors the cell in the 2 column of the selected cell rather than the cell itself. Any help will be hugely appreciated.

_Best Regards

Upvotes: 2

Views: 4499

Answers (1)

OblongMedulla
OblongMedulla

Reputation: 1595

this appears to work:

function timeStampM() {
SpreadsheetApp.getActiveSheet()
    .getActiveCell()
   .setValue(new Date());

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var actCell = sheet.getActiveCell();
var actData = actCell.getValue();
var actRow = actCell.getRow();
if (actData != '' && actRow != 1)  //Leaving out empty and header rows
{
actCell.setBackground('Red');
}
}

Upvotes: 4

Related Questions