Search code examples
jsongoogle-apps-scriptgoogle-sheetsurlfetch

Why is one file creating an App Script error?


Can you help me understand why the script isn't working for the double double odds? Here is the place that I am trying to get the information from:

https://sportsbook.draftkings.com/leagues/basketball/88670846?category=player-props&subcategory=double-double

When I change the value in the source, as I did for the others, it gives me an error. I believe it is because, unlike the other prop types, double double is either Yes/No instead of Over/Under. Here is the googlesheet with the script! I need help with the file named Double Double.

https://docs.google.com/spreadsheets/d/1ecZFvHwrhRC6Vn3gM67cTjYKmqOslarln-91aLeURJ4/edit?usp=sharing

Here is the code written but I am getting an error:

function SPORTBOOK_DD() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('DD');
  const url = `https://sportsbook.draftkings.com//sites/US-SB/api/v4/eventgroups/88670846/categories/583/subcategories/7136?format=json`

  const response = UrlFetchApp.fetch(url);
  const rawData = JSON.parse(response.getContentText());
  const events = rawData.eventGroup.events;
  const offerCatergories = rawData.eventGroup.offerCategories;
  const playerProps = offerCatergories.filter(offer => offer.offerCategoryId == 583)[0];
  const pointsByEvent = playerProps.offerSubcategoryDescriptors.filter(sub => sub.subcategoryId == 7136)[0].offerSubcategory.offers;

  const output = [];

  pointsByEvent.forEach((eventPoint, i) => {
    const { name, startDate, teamName1, teamName2 } = events[i];
    eventPoint.forEach((point, j) => {
      const outcome = point.outcomes;
      const object = {
        Event: name,
        Startdate: startDate,
        Team1: teamName1,
        Team2: teamName2,
        Player: outcome[0].participant,
        Over_American: outcome[0].oddsAmerican,
        Over_Decimal: outcome[0].oddsDecimal,
        Over_Fractional: outcome[0].oddsFractional,
        Over_Line: outcome[0].line,
        Under_American: outcome[1].oddsAmerican,
        Under_Decimal: outcome[1].oddsDecimal,
        Under_Fractional: outcome[1].oddsFractional,
        Under_Line: outcome[1].line
      }

      if (i == 0 && j == 0) {
        output.push(Object.keys(object));
      };

      output.push(Object.values(object));
    });
  });

  sheet.getDataRange().clearContent();
  sheet.getRange(1, 1, output.length, output[0].length).setValues(output);

}

Solution

  • Since there are times the object outcome[1] is undefined, you need to catch those circumstances. Try these modifications for those possible issues:

    Script Modification:

    Under_American: outcome[1] ? outcome[1].oddsAmerican : '',
    Under_Decimal: outcome[1] ? outcome[1].oddsDecimal : '',
    Under_Fractional: outcome[1] ? outcome[1].oddsFractional : '',
    Under_Line: outcome[1] ? outcome[1].line : ''
    

    What the modification does is that it will check if the object outcome[1] is defined. If it is, then assign the value normally. If not, assign a blank string. This is just a simplified if/else statement we call Conditional (ternary) operator. Its form is condition ? true : false.

    Output:

    output

    Note:

    • I haven't included outcome[0] in the check since it seems like they are always defined. If it errors and points to the outcome[0] lines, then do the same thing we did above.