2

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

1
  • It should be sheet.getRange(actRow, 2).setBackground('red'); inside the if block Commented May 17, 2017 at 13:53

1 Answer 1

4

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');
}
}

Sign up to request clarification or add additional context in comments.

5 Comments

I believe the question States that the code should change the background of column 2 of the selected row. Not the active cell itself.
Ahh, yes I missed that- I seen your answer above and have edited the above code.
Thanks a lot guys, sorry if the question was confusing. I do aim to change the color of the selected cell and not the cell in 2nd column.
@OblongMedulla : Your old code worked like a charm. Thanks a billion, if you edit your code back to the old one, I'll mark it as an answer. Cheers!
Error: { "message": "Script error.", "filename": "", "lineno": 0, "colno": 0 }

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.