Skip to content

Instantly share code, notes, and snippets.

View brainysmurf's full-sized avatar

Adam Morris brainysmurf

View GitHub Profile
(function(global,name,Package,helpers){var ref=function wrapper(args){var wrapped=function(){return Package.apply(global.Import&&Import.module?Import._import(name):global[name],[global].concat(Array.prototype.slice.call(arguments)))};for(i in args){wrapped[i]=args[i]}return wrapped}(helpers);if(global.Import&&Import.module){Import.register(name,ref)}else{Object.defineProperty(global,name,{value:ref});global.Import=global.Import||function(lib){return global[lib]};Import.module=false}})(this,
"CacheStore",
function CacheStorePackage_ (global, config) {
config = config || {};
if (config.expiry === 'max') {
config.expiry = 21600;
}
config.expiry = config.expiry || 600; // 10 minutes
@brainysmurf
brainysmurf / testForSync.gs
Last active April 4, 2018 23:48
UrlFetchApp.fetchAll is definitely async.
/* The following code produces the following output in stackdriver log (newer logs higher):
Time for concurrent: 5.848 seconds.
Time for sleeping 10: 5.003 seconds.
Time for sleeping 2: 5.002 seconds.
Time for sleeping 9: 5.004 seconds.
Time for sleeping 8: 5.002 seconds.
Time for sleeping 5: 5.003 seconds.
Time for sleeping 3: 5.003 seconds.
Time for sleeping 1: 5.003 seconds.
Time for sleeping 7: 5.003 seconds.
@brainysmurf
brainysmurf / jsonp.gs
Created April 24, 2018 13:10
jsonp in google apps scripting
/*
Suppose there is some endpoint that returns a jsonp to you, as in Google Visualization API
It gives you a text response that looks like this:
"some.namespace.nameOfFunction({obj:'blah'})"
We want the object inside that function call.
In a google app scripting context, we have no choice but to evaluate it the old-fashioned way, using "evil" eval().
This solution builds a variable that resolves the namespace some.namespace.blah.blah and which ultimately becomes
a function that returns the passed parameter.
We build the variable with raw text, then eval it, giving the local context the ability to resolve the namespace and
@brainysmurf
brainysmurf / DecoratorPattern.md
Last active September 24, 2023 12:10
Decorator Pattern. A way to manipulate function behavior other than sending parameters.

Decorator Pattern in Apps Scripts

Introduction

We'll identify a problem that can be solved with the decorator pattern. This is essentially "decorating" a wrapping function where there is additional implementation "inside". It's an effective way to define a inject some code around before or after a function executes. Here is an example that times how long it took to execute a function:

var timeit, publicFunction;

timeit = function (f) { /* definition below */ };
@brainysmurf
brainysmurf / DatabaseFromSpreadsheet.js
Last active September 24, 2023 12:08
In AppMaker, use this to read in data from a Spreadsheet.
/**
* DataFromSpreadsheet: Read in Spreadsheet info for a Calculated Datasource in AppMaker. Use a spreadsheet to define a datasource.
* Useful for data modeling, simple Apps.
* Does not support paging; sheets with large number of rows will see performance penalties
* @param {object} params
* @param {string} params.spreadsheetId The ID of the source spreadsheet
* @param {string} params.sheetName The name of the source sheet
* @param {string} params.datasource The name of the target datasource
* @param {number} params.numHeaders How many rows are headers (default = 1)
* @param {number} params.headerRow Which row contains the name of the field (default=params.numHeaders-1)
@brainysmurf
brainysmurf / README.md
Last active May 7, 2021 05:44
ManageBac -> Google Spreadsheet API integration

ManageBac -> Google Spreadsheet API integration

About

Hi there! This gist is a bit outdated. The good news is that you can go here instead:

As of May 2021, it is actively supported.

@brainysmurf
brainysmurf / Descriptors.gs
Last active January 4, 2019 17:47
Descriptors for Google Apps Scripts
Object.defineProperty(Object.prototype, '__descriptor', {
set: function (options) {
if (options.getter) {
Object.defineProperty(this, options.property, {
get: options.getter,
configurable: true,
enumerable: true
});
}
if (options.set) {
@brainysmurf
brainysmurf / SampleLibrary.gs
Created January 3, 2019 13:05
Example Sample Library
function works () {
return 'Hello World!';
}
@brainysmurf
brainysmurf / README.md
Created January 14, 2019 11:39
A Tour of Concurrent Processing in Apps Scripts

A Tour of Concurrent Processing Techniques in Apps Scripts

The JavaScript engine implemented in Google Apps Scripts is decidedly syncronous and sequential, there is a time-limit to how long a script can execute, and there are a variety of quotas on every API endpoint.

In use cases where there is a large amount of information to be retrieved from APIs, the developer will need to deploy techniques to work around the discussed limitations.

This gist explores the variety of methods available in the stack that works around these. Specifically, we will work with the Google Drive API to explore this topic as learning tool.

A note on the Code

function testDailyTrigger() {
var e = {}, f = {};
f.test = true;
f.day = app.libraries.interface.dates.today();
//e.day.add(1, "days");
f.agents = ['[email protected]'];
f.updateSite = false;
triggerDailyTrigger({}, f);
}