Skip to content

Instantly share code, notes, and snippets.

View mogsdad's full-sized avatar

David Bingham mogsdad

View GitHub Profile
@mogsdad
mogsdad / Rescue Files.gs
Last active September 14, 2022 16:20
This is the full script solution for Stack Overflow Q14518410, "a google apps script to move or restore google drive's files from the trash"http://stackoverflow.com/questions/14518410/a-google-apps-script-to-move-or-restore-google-drives-files-from-the-trash
// See http://stackoverflow.com/questions/14518410/a-google-apps-script-to-move-or-restore-google-drives-files-from-the-trash
var addonName = 'Rescue Files';
var rescueFldrName = 'Rescued Files'; // The name of the folder that rescued files should be placed into
/**
* Adds an addon menu to the active spreadsheet.
*/
function onOpen() {
var ui = SpreadsheetApp.getUi();
@mogsdad
mogsdad / Download file from URL.js
Last active December 8, 2023 02:39
In StackOverflow question #14573055, the question was how to "download" a file from a URL using Google Apps-Script. See http://stackoverflow.com/questions/14573055/can-i-download-file-from-url-link-generated-by-google-apps-script/14574217#14574217. You can't actually "download" the source file, because apps-script has no access to your PC's file…
/**
* Retrieve a file from the given URL, store into the named folder
* on Google Drive.
*
* @param {String} fileURL URL to source file, e.g. "http://mysite.com/files/file.val1.val22.zip"
* @param {String} folder Name of target folder on Google Drive
*
* @returns {Object} Response of operation, e.g.
* {rc:200,fileName:"test.zip",fileSize:92994392}
*/
@mogsdad
mogsdad / Localize Named Range.js
Last active August 26, 2020 05:33
In StackOverflow Q14237351 the user wanted to be able to repeat formulas on multiple sheets within their spreadsheet. See http://stackoverflow.com/questions/14237351/how-to-reference-the-current-spreadsheet-in-a-named-range-in-google-spreadsheets/14287340#14287340.
/**
* Return a string with the A1 notation for the given range, with
* the sheet reference removed. To use in spreadsheet functions,
* enclose with INDIRECT().
*
* Example:
* =index(INDIRECT(localizeNamedRange(NamedRange1)),12,4)
*
* @param {string} rangeName The name of an existing range in the
* active spreadsheet. It does not matter which
@mogsdad
mogsdad / TidyGoogleFormResponses.js
Last active December 14, 2015 02:38
With Google forms, responses are saved into a spreadsheet. You may want to remove some or all responses, but there's no form API that allows that. This gist contains a function that can remove some or all responses, starting with the first (oldest) one. This works with the legacy forms product (pre-February 2013). This was written in response to…
/**
* Delete form response rows. If no input parameter is provided, prompt user
* for the number of form response rows to remove. To remove all rows, input
* -1.
*
* Assumes that the form responses are in the active sheet, that there is one
* row of 'headers' followed by responses (starting in row 2).
*
* @param {number} thisMany (Optional) The number of rows to remove. If not
* specified, user will be prompted for input.
@mogsdad
mogsdad / gist:5045148
Last active December 14, 2015 06:48 — forked from mhawksey/gist:1643207
Replaced isDate() with a version that can more accurately detect Date objects, and will not interpret integers as dates.
// EventManagerV3 glued together by mhawksey http://www.google.com/profiles/m.hawksey
// Related blog post http://mashe.hawksey.info/eventmanagerv3/
// With some code (settings, importIntoCalendar, sendEmails) from
// Romain Vialard's http://www.google.com/profiles/romain.vialard
// Manage your events: Calendar Importer and Registration Form
// https://spreadsheets.google.com/ccc?key=tCHuQkkKh_r69bQGt4yJmNQ
var ss = SpreadsheetApp.getActiveSpreadsheet();
var BOOKING_ACTION_COL = 10;
@mogsdad
mogsdad / appsScriptSaveSpeed.js
Created March 1, 2013 15:05
This gist contains a Google Apps Script that performs some experiments to provide comparisons between data storage options. It was written in response to StackOverflow question [What is faster: ScriptDb or SpreadsheetApp?](http://stackoverflow.com/questions/15145918/what-is-faster-scriptdb-or-spreadsheetapp)
/**
* Run experiments to measure speed of various approaches to saving data in
* Google App Script (GAS).
*/
function testSpeed() {
var numObj = 400;
var numAttr = 10;
var doFlush = false; // Set true to activate calls to SpreadsheetApp.flush()
var arr = buildArray(numObj,numAttr);
@mogsdad
mogsdad / getContiguousRange.js
Created March 5, 2013 04:42
This snippet of Google Apps-Script was written in response to StackOverflow question 15187688, where the user was looking for a way to obtain a contiguous range of cells - a group of cells that contained values, but which was smaller than the range that would result from calling getDataRange().
function testRanges() {
var range1 = getContiguousRange("C3").getA1Notation();
var range2 = getContiguousRange("B8").getA1Notation();
debugger;
}
/**
* Return the contiguous Range that contains the given cell.
*
@mogsdad
mogsdad / ForwardHuntGroup.js
Created April 13, 2013 05:18
This script was written as the answer to a StackOverflow question about forwarding emails to a rotating list of people. See http://stackoverflow.com/a/15984468/1677912.
/**
* Retrieves a given user label by name and forwards unread messages
* associated with that that label to a member of the Hunt Group.
*/
function huntGroupForward() {
// get the label for given name
var labelName = "elephant"
var label = GmailApp.getUserLabelByName(labelName);
if (label == null) throw new Error("No messages for label "+labelName);
// get count of all threads in the given label
/**
* Move all values from source range to destination range. Upon
* completion, source range will be cleared. Source values will
* be moved into a destination range starting at the "top left"
* of the destination range, using the dimensions of the source
* range. This is a blind copy, with no overwrite test.
*
* @param {Range} source Range Object to take values from.
* @param {Range} destination Range Object to receive values.
*
@mogsdad
mogsdad / digest.js
Last active October 29, 2021 03:28
This Google Apps Script function returns a string representing the 16-byte MD5 digest of a given message. It was originally written as an answer to StackOverflow question http://stackoverflow.com/questions/16216868/get-back-a-string-representation-from-computedigestalgorithm-value-byte. It's been refactored to support adaptation to other digest …
/**
* Return string representation of MD5 digest of the given message.
*
* @param {String} message Message to be encoded.
*
* @return {String} 16-byte digest value
*/
function signMd5(message){
return digest(Utilities.DigestAlgorithm.MD5, message);
}