So I have been playing around with
Google Apps Script. It's pretty neat! I decided to putogether a google spreedsheet that would track my progress for training for a Marathon. I'm not actually signed up for any specific marathon I just wanted to set some goals to excercise and to see if I could actually run 26.2 miles. So I put this spreedsheet together to hold me acountable for my progress. Here is a screen shot:
I have no excel experience and have never put together a google spreedsheet doc before. But one thing I wanted to do was to change a cell background color based off of another cell. This apparently is not possible with the "Change colors with rules..." option from the menu. This only allows you to change the color of the current cell based on it's value. But what I wanted was a way to modify the completed cell background color if completed was equal to total. So I started playing around with google apps scripts to see if I could accomplish such a task. I did accomplish the task with the following code.
I created a named rage called weekData that goes from A2:L22. (
Range Names) There are a number of different way of invoking scripts. Referencing it from a spreadshee formula as if it was an existing function, by click on the run button from the online script editor, from a custom menu, and by click on a drawing.
I really liked the idea of referencing it from a spreadsheet cell formula but quickly found out that when using a custom fuction you may only modify the value of the local cell, and trying to perform other actions to modify the attributes of the cell will result in a script error. Invoking the script via the other methods does allow me to properly change the cell attributes like the background color. I also found that you can have the script execute when the document loads or refreshes by adding the following function to the top of the script:
function onOpen() {
}
So I did that along with adding a menu option to update on demand while making changes to the spreadsheet.
Here is the entire script:
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
getCompleted();
var menuEntries = [ {name: "Update Schedule", functionName: "getCompleted"} ];
ss.addMenu("Update", menuEntries)
}
function getCompleted() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var weekDataRange = ss.getRangeByName("weekData");
var weekObjects = getObjects(weekDataRange);
//var week1 = weekObjects[0];
//Browser.msgBox(week1.day1finish);
ProcessSchedule(weekObjects);
}
function getObjects(dataRange)
{
var data = dataRange.getValues();
var objects = [];
for (var i = 0; i < data.length; ++i)
{
var object = {};
object['week'] = data[i][0];
object['date'] = data[i][1];
object['day1num'] = data[i][2];
object['day1finish'] = isSet(data[i][3]);
object['day2num'] = data[i][4];
object['day2finish'] = isSet(data[i][5]);
object['day3num'] = data[i][6];
object['day3finish'] = isSet(data[i][7]);
object['day4num'] = data[i][8];
object['day4finish'] = isSet(data[i][9]);
object['total'] = data[i][10];
object['sum'] = 0;
object['range'] = dataRange.getCell(i+1,12);
objects.push(object);
}
return objects;
}
function isSet(cellData)
{
return cellData == "x" || cellData == "X";
}
function ProcessSchedule(weekObjects)
{
for (var i = 0; i < weekObjects.length; ++i)
{
if (weekObjects[i].day1finish) {
weekObjects[i].sum += weekObjects[i].day1num;
}
if (weekObjects[i].day2finish) {
weekObjects[i].sum += weekObjects[i].day2num;
}
if (weekObjects[i].day3finish) {
weekObjects[i].sum += weekObjects[i].day3num;
}
if (weekObjects[i].day4finish) {
weekObjects[i].sum += weekObjects[i].day4num;
}
if (weekObjects[i].sum == weekObjects[i].total) {
weekObjects[i].range.setBackgroundRGB(179,213,128);
} else {
weekObjects[i].range.setBackgroundRGB(255,0,0);
}
//weekObjects[i].range.setValue(weekObjects[i].sum);
}
}
If you want to see this live here is alink to check it out:
Marathon Training