Question

I'm trying to write a script in Google Sheets that will:

  • compare my weekly fantasy stats to every other team in my league
  • determine if I would have won, lost, or tied in each statistical category
  • calculate the final score against each opponent

Given the following spreadsheet:

enter image description here

I have the following function that is starting to get close, but not quite there.

function onEdit() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet2');

  var myStats = sheet.getRange('b2:k2');
  var myVals = myStats.getValues();

  var opps = sheet.getRange('b4:k');
  var oppsVals = opps.getValues();

  for (var i = 0; i <= 11; i++) {

    for (var x in oppsVals[i]){

      if (oppsVals[i][x] > myVals[0][x]) {
        // make opponent cell green
      } else if (oppsVals[i][x] == myVals[0][x]) {
        // make opponent cell gray
      } else {
        // make opponent cell red
      }
    }

    // calculate score (i.e. 6-3-1, 7-3-0, etc)

  }

};

If the function were going through Row 4 and comparing the stats, the opponents cells would be green (because they won) in Columns B,C,D,E,G,I,J,K. Column F would be red (because I won) and column H would be grey (tie).

Column L would populate with a final score: 1-8-1 (opp win, my win, tie)

As you may have noticed, there is another wrinkle in the Columns J & K are determined to be won by lower values.

Any help is appreciated!

Was it helpful?

Solution

I was able to make this work with the following function. Hopefully, this may prove useful to anyone who stumbles upon this later.

function onEdit() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet2');

  var myStats = sheet.getRange('b2:k2');
  var myVals = myStats.getValues();

  var opps = sheet.getRange('b4:k');
  var oppsVals = opps.getValues();

  for (var i = 0; i <= 10; i++) {

    var wins = [];
    var losses = [];
    var ties = [];

    for (var x in oppsVals[i]){

      var alphas = ["b","c","d","e","f","g","h","i","j", "k", "l"];
      var nums = ['4','5','6','7','8','9','10','11','12','13','14'];

      var cell = alphas[x] + nums[i];

      if ( alphas[x] == 'j' || alphas[x] == 'k' ) {

          if (oppsVals[i][x] < myVals[0][x]) {
            sheet.getRange(cell).setFontWeight('normal');
            losses.push(cell);
          } else if (oppsVals[i][x] == myVals[0][x]) {
            sheet.getRange(cell).setFontStyle('italic');
            ties.push(cell);
          } else {
            sheet.getRange(cell).setFontWeight('bold');
            wins.push(cell);
          }

      } else {

          if (oppsVals[i][x] < myVals[0][x]) {
            sheet.getRange(cell).setFontWeight('bold');
            wins.push(cell);
          } else if (oppsVals[i][x] == myVals[0][x]) {
            sheet.getRange(cell).setFontStyle('italic');
            ties.push(cell);
          } else {
            sheet.getRange(cell).setFontWeight('normal');
            losses.push(cell);
          }

      }

    }

    var winTotal = wins.length;
    var lossesTotal = losses.length;
    var tiesTotal = ties.length;

    sheet.getRange("l" + nums[i]).setValue(winTotal + "-" + lossesTotal + "-" + tiesTotal);

    if ( winTotal > lossesTotal ) {
        sheet.getRange("l" + nums[i]).setFontWeight('bold').setBackground('green');
    }

    if ( winTotal < lossesTotal ) {
        sheet.getRange("l" + nums[i]).setFontWeight('bold').setBackground('red');
    }

    if ( winTotal == lossesTotal ) {
      if ( oppsVals[i][0] < myVals[0][0] ) {
        sheet.getRange("l" + nums[i]).setFontWeight('bold').setBackground('green');
      } else {
        sheet.getRange("l" + nums[i]).setFontWeight('bold').setBackground('red');
      }

    }

  }

};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top