Skip to content

Instantly share code, notes, and snippets.

@barrieroberts
Last active April 5, 2021 22:43
Show Gist options
  • Save barrieroberts/022fcfde84558d9afa5d14edb1ded613 to your computer and use it in GitHub Desktop.
Save barrieroberts/022fcfde84558d9afa5d14edb1ded613 to your computer and use it in GitHub Desktop.
6 Nations Report - Get data from website
//Bound to a Google Sheet
//SCRIPT 1
function getData() {
const ss = SpreadsheetApp.getActiveSpreadsheet(),
shResults = ss.getSheetByName('RESULTS'),
results = shResults.getDataRange().getValues();
//Get appropriate week data (months 0-11)
const wk1 = new Date(2021, 1, 6),
wk2 = new Date(2021, 1, 13),
wk3 = new Date(2021, 1, 27),
wk4 = new Date(2021, 2, 13),
wk5 = new Date(2021, 2, 20);
//Get today's date & dates in array
var todaysDate = new Date().setHours(0, 0, 0, 0);
const wks = [wk1, wk2, wk3, wk4, wk5];
//Loop thru dates
wks.forEach(function (wk, w) {
if (todaysDate === wk.setHours(0, 0, 0, 0)) {
//Get row that matches date
var row = (w * 6);
//Get data for that game week
var wkInfo = {
homeTeam1: results[row][0]
.replace("(1 BP)", "").trim(),
awayTeam1: results[row][2]
.replace("(1 BP)", "").trim(),
homeScore1: results[row][1].split('–')[0],
awayScore1: results[row][1].split("–")[1],
homeScorers1: results[row + 1][0]
.replace(/\*/g, ""),
awayScorers1: results[row + 1][2]
.replace(/\*/g, ""),
homeTeam2: results[row + 2][0]
.replace("(1 BP)", "").trim(),
awayTeam2: results[row + 2][2]
.replace("(1 BP)", "").trim(),
homeScore2: results[row + 2][1].split('–')[0],
awayScore2: results[row + 2][1].split("–")[1],
homeScorers2: results[row + 3][0]
.replace(/\*/g, ""),
awayScorers2: results[row + 3][2]
.replace(/\*/g, ""),
homeTeam3: results[row + 4][0]
.replace("(1 BP)", "").trim(),
awayTeam3: results[row + 4][2]
.replace("(1 BP)", "").trim(),
homeScore3: results[row + 4][1].split('–')[0],
awayScore3: results[row + 4][1].split("–")[1],
homeScorers3: results[row + 5][0]
.replace(/\*/g, ""),
awayScorers3: results[row + 5][2]
.replace(/\*/g, "")
};
var weekNo = w + 1;
//Make and send doc
var doc = addResults(wkInfo, weekNo, ss);
var doc = addTable(ss, doc);
sendDoc(doc, weekNo);
}
});
}
//Weekly Trigger
//SCRIPT 2
function addResults(wkInfo, weekNo) {
const doc = DocumentApp.create("6 Nations Results");
const body = doc.getBody();
body.setMarginTop(0);
body.setMarginBottom(0);
//Set styles
var style0 = {};
style0[DocumentApp.Attribute.FONT_FAMILY] = 'Nunito';
style0[DocumentApp.Attribute.FONT_SIZE] = 26;
style0[DocumentApp.Attribute.BOLD] = true;
style0[DocumentApp.Attribute.UNDERLINE] = true;
var style1 = {};
style1[DocumentApp.Attribute.FONT_SIZE] = 22;
style1[DocumentApp.Attribute.BOLD] = true;
style1[DocumentApp.Attribute.FOREGROUND_COLOR] = '#0000ff';
style1[DocumentApp.Attribute.UNDERLINE] = false;
var style2 = {};
style2[DocumentApp.Attribute.FONT_SIZE] = 14;
style2[DocumentApp.Attribute.BOLD] = false;
style2[DocumentApp.Attribute.FOREGROUND_COLOR] = '#666666';
//Flag icons
const england = DriveApp.getFileById('1C62bHrSndYy-uZYUyroFFFhpYN1fIOa3').getBlob();
const france = DriveApp.getFileById('1xJ2yACEm92hUZ7qJznGa3psUGrBENcYY').getBlob();
const ireland = DriveApp.getFileById('1Ldxoe-02M1XIjNG3xb1Ngh-juSkKVTSH').getBlob();
const italy = DriveApp.getFileById('1LbuAXjVSGXaZj-GX-Il9aaDGpp5yXMa9').getBlob();
const scotland = DriveApp.getFileById('1KlZsxwn3PInYht4PIslWvgSjEngGsNSr').getBlob();
const wales = DriveApp.getFileById('1Ra8S3_MYGFgCTl4oSoh2xN0oawCRmCGG').getBlob();
//Add title and results
body.appendParagraph("6 NATIONS RUGBY - WEEK " + weekNo)
.setAttributes(style0);
body.appendParagraph(wkInfo.homeTeam1 + " " + wkInfo.homeScore1)
.setAttributes(style1);
body.appendParagraph(wkInfo.homeScorers1)
.setAttributes(style2);
body.appendParagraph('');
body.appendParagraph(wkInfo.awayTeam1 + " " + wkInfo.awayScore1)
.setAttributes(style1);
body.appendParagraph(wkInfo.awayScorers1)
.setAttributes(style2);
body.appendHorizontalRule();
body.appendParagraph(wkInfo.homeTeam2 + " " + wkInfo.homeScore2)
.setAttributes(style1);
body.appendParagraph(wkInfo.homeScorers2)
.setAttributes(style2);
body.appendParagraph('');
body.appendParagraph(wkInfo.awayTeam2 + " " + wkInfo.awayScore2)
.setAttributes(style1);
body.appendParagraph(wkInfo.awayScorers2)
.setAttributes(style2);
body.appendHorizontalRule();
body.appendParagraph(wkInfo.homeTeam3 + " " + wkInfo.homeScore3)
.setAttributes(style1);
body.appendParagraph(wkInfo.homeScorers3)
.setAttributes(style2);
body.appendParagraph('');
body.appendParagraph(wkInfo.awayTeam3 + " " + wkInfo.awayScore3)
.setAttributes(style1);
body.appendParagraph(wkInfo.awayScorers3)
.setAttributes(style2);
body.appendHorizontalRule();
//Add bolding to Try, Con and Pen and add flag images
var paragraphs = body.getParagraphs();
var scores = ["Try:", "Con:", "Pen:"];
var flags = [england, france, ireland,
italy, scotland, wales];
var countryNames = ["England", "France", "Ireland",
"Italy", "Scotland", "Wales"];
var countries = [wkInfo.homeTeam1, wkInfo.awayTeam1,
wkInfo.homeTeam2, wkInfo.awayTeam2,
wkInfo.homeTeam3, wkInfo.awayTeam3];
//Get order of countries
var iconsInOrder = [];
countries.forEach(function (country) {
iconsInOrder.push(flags[countryNames.indexOf(country)]);
});
for (p = 3; p < paragraphs.length; p += 3) {
var i = (p / 3) - 1;
paragraphs[p - 1].insertInlineImage(0, iconsInOrder[i])
.setHeight(25).setWidth(25);
var paraScores = paragraphs[p].editAsText();
scores.forEach(function (score) {
if (paraScores.findText(score)) {
var scoreText = paraScores.findText(score);
var startScore = scoreText.getStartOffset();
var endScore = scoreText.getEndOffsetInclusive();
paraScores.setBold(startScore, endScore, true);
}
});
}
body.appendPageBreak();
return doc;
}
//SCRIPT 3
function addTable(ss, doc) {
var shTable = ss.getSheetByName('TABLE'),
tableData = shTable.getDataRange().getValues();
//Set up styles
var style3 = {};
style3[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000';
style3[DocumentApp.Attribute.FONT_SIZE] = 22;
style3[DocumentApp.Attribute.BOLD] = true;
var style4 = {};
style4[DocumentApp.Attribute.FONT_SIZE] = 12;
style4[DocumentApp.Attribute.BOLD] = false;
var widths = [35, 70, 40, 40, 40, 40, 45, 45, 45, 45];
var body = doc.getBody();
body.appendParagraph("");
body.appendParagraph("6 NATIONS RUGBY - TABLE") ///body.appendTable(tableData); Show raw data
.setAttributes(style3);
var table = body.appendTable();
//Add rows
tableData.forEach(function (rowData, r) {
if (r > 0) {
var tableRow = table.appendTableRow();
//Add cells
tableData[r].forEach(function (cellInfo, c) {
if (typeof cellInfo == 'number') {
var cellInfo = cellInfo.toFixed(0);
}
//Ignore certain columns
if (c < 9 || c > 13) {
//Remove asterisks, set styling & column widths
var cellInfo = cellInfo.replace(/\*/g, "");
var cell = tableRow.appendTableCell(cellInfo)
.setAttributes(style4);
cell.getChild(0).asParagraph()
.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
cell.setWidth(widths[c]);
//Add header background and bolding
if (r === 1) {
cell.editAsText()
.setBackgroundColor('#93c47d')
.setBold(true);
}
else {
//Add alternative row shading
if (r % 2) {
cell.editAsText()
.setBackgroundColor('#efefef');
}
}
}
});
}
});
//Add missing headers and border width
table.getCell(0, 0).setText("Pos");
table.getCell(0, 1).setText("Nation");
table.getCell(0, 9).setText("Points");
table.setBorderWidth(1.2);
doc.saveAndClose();
return doc;
}
//SCRIPT 4
//Email doc to user
function sendDoc(doc, weekNo) {
var emailBody = HtmlService.createHtmlOutputFromFile('5-email')
.getContent();
//Get G Doc and make a PDF then attach
var pdf = doc.getAs('application/pdf').getBytes();
var attach = {
fileName: '6 Nations.pdf',
content: pdf,
mimeType: 'application/pdf'
};
MailApp.sendEmail("baz@bazroberts.com",
"6 Nations - Week " + weekNo,
'',
{
htmlBody: emailBody,
attachments: [attach]
});
//Delete Doc from Drive
var document = DriveApp.getFileById(doc.getId());
DriveApp.removeFile(document);
}
//HTML 5
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
p {
font-family: verdana;
font-size: 1.2em;
}
#title {
height: 40px;
padding-top: 10px;
border-left-style: outset;
border-left-color: blue;
border-left-width: 10px;
}
</style>
</head>
<body>
<h2 id="title">6 NATIONS</h2>
<p>Attached are the 6 Nations results and table.</p>
<hr>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment