Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let ss = SpreadsheetApp.getActiveSpreadsheet();
- let sheet = ss.getActiveSheet();
- let log = Logger.log;
- let sleep = Utilities.sleep;
- let range = sheet.getRange;
- let liveDate = Utilities.formatDate(new Date, Session.getScriptTimeZone(), 'dd/MM/yyyy');
- let liveTime = Utilities.formatDate(new Date, Session.getScriptTimeZone(), 'HH:mm:ss');
- // Checks on Change for the Checkbox Value, If true, Initiates the Emai Send after resetting the value iof the box to False.
- function manualSend() {
- var checkBox = range('C5');
- if(checkBox.getValue() === true) {
- checkBox.setValue(false)
- SpreadsheetApp.flush();
- sendCatImageEmail();
- }
- }
- // Runs on a 30 second Time to check conditions and send Email. Once per day.
- function timer() {
- var timeGap = new Date('', '', '', 0, 5, 0, '');
- var ftimeGap = Utilities.formatDate(timeGap, Session.getScriptTimeZone, 'HH:mm:ss');
- for (var x = 0; x < 2; x++) { //Checks every 30secs for Conditions.
- Utilities.sleep(29000);
- if(range('C7').getValue() === false && liveTime < ftimeGap) { // See if flag is false, also if the live time is less than 00:05:00am.
- range('C7').setValue(true); // Sets Flag to True, This prevents the Email being sent twice.
- SpreadsheetApp.flush();
- sendCatImageEmail();
- }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.
- range('C7').setValue(false);
- SpreadsheetApp.flush();
- }else{
- log('Not the right time to set Automatically.') // If the Flag is False and the time is after 00:05:00am, log.
- }
- }
- }
- // 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.
- function getImage() {
- var url = "https://api.thecatapi.com/v1/images/search?limit=1&has_breeds=1&api_key=8fdfcb5c-d27e-4579-9c7c-1a33a5c1ede0";
- var response = JSON.parse(UrlFetchApp.fetch(url).getContentText());
- //log(response);
- var catInfo = {
- breed: response[0].breeds[0].name,
- description: response[0].breeds[0].description,
- wikipediaURL: response[0].breeds[0].wikipedia_url,
- imageUrl: response[0].url,
- origin: response[0].breeds[0].origin,
- lifeSpan: response[0].breeds[0].life_span,
- alternativeNames: response[0].breeds[0].alt_names,
- adaptability: response[0].breeds[0].adaptability,
- affection: response[0].breeds[0].affection_level,
- childfriendly: response[0].breeds[0].child_friendly,
- dogFriendly: response[0].breeds[0].dog_friendly,
- energyLevel: response[0].breeds[0].energy_level,
- health: response[0].breeds[0].health_issues,
- intelligence: response[0].breeds[0].intelligence,
- shedAmount: response[0].breeds[0].shedding_level,
- socialNeeds: response[0].breeds[0].social_needs,
- strangerFriendly: response[0].breeds[0].stranger_friendly,
- vocalisation: response[0].breeds[0].vocalisation,
- };
- //log(catInfo);
- return catInfo;
- }
- // Helper that fetches a Random Cat fact to be used in the Email.
- function getCatFact() {
- var factURL = 'https://catfact.ninja/fact?max_length=140';
- var catFact = UrlFetchApp.fetch(factURL).getContentText();
- try {
- var parsedResponse = JSON.parse(catFact);
- var extractedFact = parsedResponse.fact;
- Logger.log(extractedFact);
- return extractedFact;
- } catch (error) {
- Logger.log("Error parsing JSON: " + error);
- }
- }
- // Created the email and sends it to the Relevant People.
- function sendCatImageEmail() {
- var catFact = getCatFact(); // Calls the Cat Fact Helper to return the Fact.
- var catInfo = getImage(); // Calls the get Image and Info Helper to Return the CatInfo Data.
- var emailSubject = 'Todays Random Cat!';
- var emailBody = '<p style="font-size:28px; color:#FFB8F3; margin: 0;"><strong>Hello Cat Lovers,</strong></p>' +
- '<p style="font-size:16px; color:#FF5252; margin: 0;"><strong>Here is today\'s cat fact and breed information!</strong></p><br><br>' +
- '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Fact: </strong>' +
- '<span style="color:#DB0654;">' + catFact + '</span>' +
- '</p><br><br>' +
- '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Cat Breed: </strong>' + '<span style="color:#DB0654;">' + catInfo.breed + '</span>' +
- '<p style="font-size:15px; color:#4AACED; margin: 0;"><strong>Alternative Names: </strong>' +
- '<span style="color:#DB0654;">' + catInfo.alternativeNames + '</span>' +
- '</p>' +
- '<p style="font-size:15px; color:#4AACED; margin: 0;"><strong>Country of Origin: </strong>' +
- '<span style="color:#DB0654;">' + catInfo.origin + '</span>' +
- '</p>' +
- '<p style="font-size:15px; color:#4AACED; margin: 0;"><strong>Average Life Span: </strong>' +
- '<span style="color:#DB0654;">' + catInfo.lifeSpan + '</span>' +
- '</p><br>' +
- '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Breed Description: </strong><br>' +
- '<span style="color:#DB0654;">' + catInfo.description + '</span>' +
- '</p><br><br>' +
- '<p style="font-size:24px; color:#4AACED; margin: 0;"><strong>Breed Ratings - </strong></p><br>' +
- '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Adaptability Level: </strong>' +
- '<span style="color:#DB0654;">' + catInfo.adaptability + '</span>' +
- '</p>' +
- '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Affection Level: </strong>' +
- '<span style="color:#DB0654;">' + catInfo.affection + '</span>' +
- '</p>' +
- '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Energy Level: </strong>' +
- '<span style="color:#DB0654;">' + catInfo.energyLevel + '</span>' +
- '</p>' +
- '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Chance of Health Problems: </strong>' +
- '<span style="color:#DB0654;">' + catInfo.health + '</span>' +
- '</p>' +
- '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Intelligence Level: </strong>' +
- '<span style="color:#DB0654;">' + catInfo.intelligence + '</span>' +
- '</p>' +
- '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Shedding Amount: </strong>' +
- '<span style="color:#DB0654;">' + catInfo.shedAmount + '</span>' +
- '</p>' +
- '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Social Need Level: </strong>' +
- '<span style="color:#DB0654;">' + catInfo.socialNeeds + '</span>' +
- '</p>' +
- '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Vocalisation Level: </strong>' +
- '<span style="color:#DB0654;">' + catInfo.vocalisation + '</span>' +
- '</p>' +
- '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Child Friendliness: </strong>' +
- '<span style="color:#DB0654;">' + catInfo.childfriendly + '</span>' +
- '</p>' +
- '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Dog Friendliness: </strong>' +
- '<span style="color:#DB0654;">' + catInfo.dogFriendly + '</span>' +
- '</p>' +
- '<p style="font-size:20px; color:#4AACED; margin: 0;"><strong>Stranger Friendliness: </strong>' +
- '<span style="color:#DB0654;">' + catInfo.strangerFriendly + '</span>' +
- '</p><br>' +
- '<p style="font-size:16px; color:#4AACED; margin: 0;"><strong>Wikipedia URL: </strong>' +
- '<span style="color:#DB0654;">' + catInfo.wikipediaURL + '</span>' +
- '</p><br>' +
- '<img src="' + catInfo.imageUrl + '" style="max-width: 100%; height: auto; margin: 0;">';
- //var recipientEmail = '[email protected]' // Test Email.
- MailApp.sendEmail({
- to: recipientEmail,
- subject: emailSubject,
- htmlBody: emailBody
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment