Skip to content

Instantly share code, notes, and snippets.

@drunkrhin0
Created September 29, 2023 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drunkrhin0/d962d616dbe649e30e51552cdb805f64 to your computer and use it in GitHub Desktop.
Save drunkrhin0/d962d616dbe649e30e51552cdb805f64 to your computer and use it in GitHub Desktop.
Send a Google Doc with Google App script at a random time to someone. Has a 70% probability of sending it Between Midnight and 6AM when they're sleeping
/*
Set the Subject & recipient email.
Change the prbability times if you feel inclined.
Go to Extensions -> App Script and dump this code in so it fetches it dynamically
App uses.gs but syntax highlight with JS.
*/
function sendDocByEmail() {
// Fetch the ID of the document where the script editor has been opened
var docId = DocumentApp.getActiveDocument().getId();
// Fetch the content of the doc as plain text
var content = DocumentApp.openById(docId).getBody().getText();
// Specify the recipient's email and subject of the email
var recipientEmail = "recipient@example.com";
var subject = "Daily Google Doc Content";
// Send the email
GmailApp.sendEmail(recipientEmail, subject, content);
// Log the send action
Logger.log('Sent email to ' + recipientEmail + ' at ' + new Date().toLocaleString());
}
function getRandomHour() {
// 70% probability of choosing an hour between Midnight to 6AM
if (Math.random() < 0.7) {
return Math.floor(Math.random() * 7);
} else {
// 30% probability of choosing an hour between 7AM and 11PM
return 7 + Math.floor(Math.random() * 17);
}
}
function clearExistingTriggers() {
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
}
function setDailyTrigger() {
clearExistingTriggers();
var randomHour = getRandomHour();
Logger.log('Setting trigger for ' + randomHour + ' hour on ' + new Date().toLocaleDateString());
ScriptApp.newTrigger('sendDocByEmail')
.timeBased()
.everyDays(1)
.atHour(randomHour)
.create();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment