Skip to content

Instantly share code, notes, and snippets.

@bennettscience
bennettscience / isTimeUp.gs
Last active November 16, 2017 13:35
Prevent script timeouts in Google Apps Script
function isTimeUp_(starttime) {
var now = new Date();
return now.getTime() - starttime.getTime() > 270000; // 4.5 minutes
}
// Set the start time outside the loop
var starttime = new Date();
// Inside your function, check the time and break to prevent failure mid-loop
for(var i=0; i<data.length; i++) {
@bennettscience
bennettscience / code.gs
Created November 17, 2017 02:54
Auto-tweet images of slides as a Google Slideshow is presented
// The rest of the backend code.
// postTweet.gs and getThumbnails.gs can be appended to this file.
// When combined, click Publish > Deploy as web app
// You can then test by visiting your app page.
var KEY = "YOUR_APP_KEY";
var SECRET = "YOUR_APP_SECRET";
// Initialize Twitter login
// See https://mashe.hawksey.info/2014/10/twtrservice-a-twitter-api-client-for-google-apps-script/
@bennettscience
bennettscience / feedback.gs
Last active September 11, 2020 13:46
Automating feedback forms with Google Apps Script
// Copy this script into the feedback spreadsheet TEMPLATE
/************************ OnOpen *******************************/
function onOpen(e) {
var ui = SpreadsheetApp.getUi().createMenu("Shortlink")
.addItem("Show URL and QR code", "showQrAndLink")
.addToUi()
}
/************************ GLOBALS *******************************/
@bennettscience
bennettscience / code.gs
Last active November 28, 2017 19:32
Automatically create bit.ly shortlinks of Google Forms via the API
// Copyright 2017 Brian E. Bennett
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
@bennettscience
bennettscience / index.js
Created December 31, 2017 02:34
Node JS function to watch Firebase realtime database
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const nodemailer = require('nodemailer');
admin.initializeApp(functions.config().firebase);
// set up email credentials in firebase.config
// See https://github.com/firebase/functions-samples/tree/master/quickstarts/email-users for example
const gmailEmail = functions.config().gmail.email;
@bennettscience
bennettscience / comment.js
Created December 31, 2017 02:38
Push a comment to a firebase database from a static HTML page
$(function() {
var ref = new Firebase("https://nodes-commenting.firebaseio.com/"),
postRef = ref.child(window.location.pathname);
postRef.on("child_added", function(snapshot) {
var newPost = snapshot.val();
if(newPost.moderated) {
$("#comment-list").prepend('<div class="comment">' +
'<h4>' + escapeHtml(newPost.name) + '</h4>' +
'<div class="profile-image"><img src="http://www.gravatar.com/avatar/' + escapeHtml(newPost.md5Email) + '?s=100&d=retro"/></div> ' +
@bennettscience
bennettscience / outcome.py
Created February 1, 2018 16:34
Playing with the Canvas LMS python API
from canvasapi import Canvas
API_URL = 'https://my.url.com'
API_KEY = 'mySTringKEy123'
canvas = Canvas(API_URL, API_KEY)
outcome = canvas.get_outcome(5426)
class Rating:
@bennettscience
bennettscience / config
Created March 17, 2018 18:30
git config sample
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
@bennettscience
bennettscience / helpbotBrain.gs
Last active March 22, 2018 11:46
Main files for an apps script bot and webapp pair
/**
* getLookup - Get an array of videos matching the search key request
*
* @param {Object[]} keys - Array of search terms
* @returns {Object[]} matches - Array of video objects matched to the tag
*/
function getLookup(keys) {
var sheet = ss.getSheetByName("db");
var data = sheet.getDataRange().getValues();
@bennettscience
bennettscience / onEdit.gs
Last active May 9, 2023 07:22
Dynamic data validation in Apps Script
function onEdit(e) {
var row = e.range.getRow();
var col = e.range.getColumn();
// If the edited column was 2 or 3, do the next part. Ignore other changes
if(col == 2 || col == 3) {
var value = sheet.getRange(row, col).getValue();
updateList(row,col, value);
}
}