NotSooFriendly94

Untitled

Jan 14th, 2024
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let ss = SpreadsheetApp.getActiveSpreadsheet();
  2. let sheet = ss.getActiveSheet();
  3. let log = Logger.log;
  4. let sleep = Utilities.sleep;
  5. let range = sheet.getRange;
  6. let liveDate = Utilities.formatDate(new Date, Session.getScriptTimeZone(), 'dd/MM/yyyy');
  7. let liveTime = Utilities.formatDate(new Date, Session.getScriptTimeZone(), 'HH:mm:ss');
  8.  
  9. // Checks on Change for the Checkbox Value, If true, Initiates the Emai Send after resetting the value iof the box to False.
  10. function manualSend() {
  11.   var checkBox = range('C5');
  12.  
  13.   if(checkBox.getValue() === true) {
  14.     checkBox.setValue(false)
  15.     SpreadsheetApp.flush();
  16.     sendCatImageEmail();
  17.   }
  18. }
  19. // Runs on a 30 second Time to check conditions and send Email. Once per day.
  20. function timer() {
  21.  var timeGap = new Date('', '', '', 0, 5, 0, '');
  22.  var ftimeGap = Utilities.formatDate(timeGap, Session.getScriptTimeZone, 'HH:mm:ss');
  23.  
  24.   for (var x = 0; x < 2; x++) { //Checks every 30secs for Conditions.
  25.     Utilities.sleep(29000);
  26.  
  27.     if(range('C7').getValue() === false && liveTime < ftimeGap) { // See if flag is false, also if the live time is less than 00:05:00am.
  28.       range('C7').setValue(true); // Sets Flag to True, This prevents the Email being sent twice.
  29.       SpreadsheetApp.flush();
  30.       sendCatImageEmail();
  31.     }else if(liveTime > ftimeGap && range('C7').getValue() !== false) { // If the flag is True and the Live time past 00:05:00am, Reset the flag to False.
  32.      range('C7').setValue(false);
  33.      SpreadsheetApp.flush();
  34.     }else{
  35.       log('Not the right time to set Automatically.') // If the Flag is False and the time is after 00:05:00am, log.
  36.     }
  37.   }
  38. }
  39. // Helper that fetches JSON values from URL. Returned Data is Parsed and then designated as an object to be used in the email, including a cat Image URL.
  40. function getImage() {
  41.   var url = "https://api.thecatapi.com/v1/images/search?limit=1&has_breeds=1&api_key=8fdfcb5c-d27e-4579-9c7c-1a33a5c1ede0";
  42.   var response = JSON.parse(UrlFetchApp.fetch(url).getContentText());
  43.   //log(response);
  44.   var catInfo = {
  45.     breed: response[0].breeds[0].name,
  46.     description: response[0].breeds[0].description,
  47.     wikipediaURL: response[0].breeds[0].wikipedia_url,
  48.     imageUrl: response[0].url,
  49.     origin: response[0].breeds[0].origin,
  50.     lifeSpan: response[0].breeds[0].life_span,
  51.     alternativeNames: response[0].breeds[0].alt_names,
  52.     adaptability: response[0].breeds[0].adaptability,
  53.     affection: response[0].breeds[0].affection_level,
  54.     childfriendly: response[0].breeds[0].child_friendly,
  55.     dogFriendly: response[0].breeds[0].dog_friendly,
  56.     energyLevel: response[0].breeds[0].energy_level,
  57.     health: response[0].breeds[0].health_issues,
  58.     intelligence: response[0].breeds[0].intelligence,
  59.     shedAmount: response[0].breeds[0].shedding_level,
  60.     socialNeeds: response[0].breeds[0].social_needs,
  61.     strangerFriendly: response[0].breeds[0].stranger_friendly,
  62.     vocalisation: response[0].breeds[0].vocalisation,
  63.   };
  64.   //log(catInfo);
  65.   return catInfo;
  66. }
  67. // Helper that fetches a Random Cat fact to be used in the Email.
  68. function getCatFact() {
  69.   var factURL = 'https://catfact.ninja/fact?max_length=140';
  70.   var catFact = UrlFetchApp.fetch(factURL).getContentText();
  71.  
  72.   try {
  73.     var parsedResponse = JSON.parse(catFact);
  74.     var extractedFact = parsedResponse.fact;
  75.     Logger.log(extractedFact);
  76.     return extractedFact;
  77.   } catch (error) {
  78.     Logger.log("Error parsing JSON: " + error);
  79.   }
  80. }
  81. // Created the email and sends it to the Relevant People.
  82. function sendCatImageEmail() {
  83.   var catFact = getCatFact(); // Calls the Cat Fact Helper to return the Fact.
  84.   var catInfo = getImage(); // Calls the get Image and Info Helper to Return the CatInfo Data.
  85.   var emailSubject = 'Todays Random Cat!';
  86.   var emailBody = '<p style="font-size:28px; color:#FFB8F3; margin: 0;"><strong>Hello Cat Lovers,</strong></p>' +
  87.                 '<p style="font-size:16px; color:#FF5252; margin: 0;"><strong>Here is today\'s cat fact and breed information!</strong></p><br><br>' +
  88.                 '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Fact: </strong>' +
  89.                   '<span style="color:#DB0654;">' + catFact + '</span>' +
  90.                 '</p><br><br>' +
  91.                 '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Cat Breed: </strong>' + '<span style="color:#DB0654;">' + catInfo.breed + '</span>' +
  92.                 '<p style="font-size:15px; color:#4AACED; margin: 0;"><strong>Alternative Names: </strong>' +
  93.                   '<span style="color:#DB0654;">' + catInfo.alternativeNames + '</span>' +
  94.                 '</p>' +
  95.                 '<p style="font-size:15px; color:#4AACED; margin: 0;"><strong>Country of Origin: </strong>' +
  96.                   '<span style="color:#DB0654;">' + catInfo.origin + '</span>' +
  97.                 '</p>' +
  98.                 '<p style="font-size:15px; color:#4AACED; margin: 0;"><strong>Average Life Span: </strong>' +
  99.                   '<span style="color:#DB0654;">' + catInfo.lifeSpan + '</span>' +
  100.                 '</p><br>' +
  101.                 '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Breed Description: </strong><br>' +
  102.                   '<span style="color:#DB0654;">' + catInfo.description + '</span>' +
  103.                 '</p><br><br>' +
  104.                 '<p style="font-size:24px; color:#4AACED; margin: 0;"><strong>Breed Ratings - </strong></p><br>' +
  105.                 '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Adaptability Level: </strong>' +
  106.                   '<span style="color:#DB0654;">' + catInfo.adaptability + '</span>' +
  107.                 '</p>' +
  108.                 '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Affection Level: </strong>' +
  109.                   '<span style="color:#DB0654;">' + catInfo.affection + '</span>' +
  110.                 '</p>' +
  111.                 '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Energy Level: </strong>' +
  112.                   '<span style="color:#DB0654;">' + catInfo.energyLevel + '</span>' +
  113.                 '</p>' +
  114.                 '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Chance of Health Problems: </strong>' +
  115.                   '<span style="color:#DB0654;">' + catInfo.health + '</span>' +
  116.                 '</p>' +
  117.                 '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Intelligence Level: </strong>' +
  118.                   '<span style="color:#DB0654;">' + catInfo.intelligence + '</span>' +
  119.                 '</p>' +
  120.                 '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Shedding Amount: </strong>' +
  121.                   '<span style="color:#DB0654;">' + catInfo.shedAmount + '</span>' +
  122.                 '</p>' +
  123.                 '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Social Need Level: </strong>' +
  124.                   '<span style="color:#DB0654;">' + catInfo.socialNeeds + '</span>' +
  125.                 '</p>' +
  126.                 '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Vocalisation Level: </strong>' +
  127.                   '<span style="color:#DB0654;">' + catInfo.vocalisation + '</span>' +
  128.                 '</p>' +
  129.                 '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Child Friendliness: </strong>' +
  130.                   '<span style="color:#DB0654;">' + catInfo.childfriendly + '</span>' +
  131.                 '</p>' +
  132.                 '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Dog Friendliness: </strong>' +
  133.                   '<span style="color:#DB0654;">' + catInfo.dogFriendly + '</span>' +
  134.                 '</p>' +
  135.                 '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Stranger Friendliness: </strong>' +
  136.                   '<span style="color:#DB0654;">' + catInfo.strangerFriendly + '</span>' +
  137.                 '</p><br>' +
  138.                 '<p style="font-size:16px; color:#4AACED; margin: 0;"><strong>Wikipedia URL: </strong>' +
  139.                   '<span style="color:#DB0654;">' + catInfo.wikipediaURL + '</span>' +
  140.                 '</p><br>' +
  141.                 '<img src="' + catInfo.imageUrl + '" style="max-width: 100%; height: auto; margin: 0;">';
  142.  
  143.   //var recipientEmail = '[email protected]' // Test Email.
  144.   MailApp.sendEmail({
  145.     to: recipientEmail,
  146.     subject: emailSubject,
  147.     htmlBody: emailBody
  148.   });
  149. }
Advertisement
Add Comment
Please, Sign In to add comment