-1

So I'm totally new at using Google Apps Script, basically just started a few hours ago. Also new to stackoverflow so excuse my formatting. For the life of me, I cant seem to find how to run Bored API to get 20 unique suggested activities.

I am also wondering how to code an If statement within the same cell column , if accessibility is less than or equal to 0.3, then it is Easy If accessibility is between 0.3 and 0.6, then it is Medium If it is 0.6 or more, then it is Hard. (Since google excel doesnt seem to support a cell holding two values at the same time)

Lastly, for the Day column how do I write a snippet that randomly suggests a day to do the suggested activity.

Questions:

  1. How to run 20 Unique suggested activities using same API?
  2. How do I code an if statement for Accessibility?
  3. How to write a snippet that randomly suggest a day to do the suggested activity?

Pls help :'(

Here's the link to the API I used. https://www.boredapi.com/documentation

Here's my current code in Apps Script,

    function myFunction() {
 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var mainSheet = ss.getSheetByName("MAIN");

  var URL_STRING = "http://www.boredapi.com/api/activity/";

  var response = UrlFetchApp.fetch(URL_STRING);
  var json = response.getContentText();
  var data = JSON.parse(json);

  var Activity = data.activity;
  var Accessibility = data.accessibility;
  var Type = data.type;
  var Price = data.price;
  var Link = data.link;
  var Key = data.key;
  
  mainSheet.getRange('B2').setValue([Activity]);
  mainSheet.getRange('C2').setValue([Accessibility]);
  mainSheet.getRange('D2').setValue([Type]);
  mainSheet.getRange('E2').setValue([Price]);
  mainSheet.getRange('G2').setValue([Link]);
  mainSheet.getRange('H2').setValue([Key]);

}

Here's the SS of what it looks like, Excel SS

  • Welcome to [so]. Questions on this site should be specific, meaning only one question per post. Please [edit] the post to ask a single question. Also details about you from the question (you could add them in your SO profile) and whenever be possible add sample data as text. You might use markdown to show the data as a table. – Rubén Oct 11 '22 at 21:46

1 Answers1

0

To answer your first question, use a while loop, Array.includes(), Array.forEach(), Array.push() and finally Range.setValues(), like this:

function append20UniqueActivities() {
  const sheet = SpreadsheetApp.getActive().getSheetByName('MAIN');
  const URL_STRING = 'http://www.boredapi.com/api/activity/';
  const numActivities = 20;
  const fields = ['activity', 'accessibility', 'type', 'price', 'link', 'key'];
  const result = [];
  const activities = [];
  while (true) {
    const response = UrlFetchApp.fetch(URL_STRING);
    const json = response.getContentText();
    const data = JSON.parse(json);
    if (activities.includes(data.activity)) {
      continue;
    }
    activities.push(data.activity);
    const row = [i];
    fields.forEach(field => row.push(data[field]));
    result.push(row);
    if (result.length >= numActivities) {
      break;
    }
  }
  const lastRow = sheet.getLastRow();
  sheet
    .insertRowAfter(lastRow)
    .getRange(lastRow + 1, 1, result.length, result[0].length)
    .setValues(result);
}
doubleunary
  • 13,842
  • 3
  • 18
  • 51