Skip to content

Instantly share code, notes, and snippets.

View brainysmurf's full-sized avatar

Adam Morris brainysmurf

View GitHub Profile
@brainysmurf
brainysmurf / README.md
Last active April 3, 2024 18:17
Things one can get used to for the V8 Google Apps Scripts engine

Things one can get used to for the V8 Google Apps Scripts engine

A bit of a monologue about various syntax changes and adjustments in learning and using the tool.

Logging is slooooooow

I know. Everyone knows.

That's why I wrote a library that writes to a spreadsheet instead. Ever since I started using it I'm far more productive. I don't know how people are still putting with that god-awful lagging logger.

@brainysmurf
brainysmurf / concurrency.gs
Last active February 22, 2024 07:37
Concurrent processing in App Scripts
/**
* Pretends to take a long time to return two rows of data
*
* @param {string} endpoint
* @return {ResponseObject}
*/
function doSomething (endpoint) {
Utilities.sleep(5 * 1000); // sleep for 5 seconds
return {
numberOfRows: 2,
@brainysmurf
brainysmurf / CoderVsAppsScriptsIDE.md
Last active December 29, 2023 20:40
A conversation between a coder and the new AppsScripts IDE

A conversation between a coder and the new AppsScripts IDE

The main takeaway I have is that coders are going to be talking to their new IDEs, maybe sometimes loudly but most of the time in thanks, and that is a good thing. This was written in early December 2020:

The view from 3000 ft

With the new IDE for AppsScripts, that platform has completed what you might think of as a nearly complete overhaul. It came in two stages, (with hopefully a third one around the corner, but not the focus of this essay).

  1. Months ago: The new V8 engine, giving it a modern runtime environment, and cool new syntax features
  2. This week: A new IDE, which gives us a proper logger and a dependable debugger, and makes finding easy-to-fix programming mistakes a cinch
@brainysmurf
brainysmurf / Bundle.js
Created March 30, 2021 02:27
BundlerPattern for appsscripts libraries
/**
* Should be listed first in the list. Creates the `Import` object and adds properties to it
*/
// create a simple object where we can add namespaces
const Import = Object.create(null);
(function (exports) {
// define your stuff in an iife …
@brainysmurf
brainysmurf / DynamicPropertiesJS.md
Last active December 29, 2023 20:37
Using dynamic property assignments

I was recently wokring on building some objects that had this pattern:

const obj = {
  startRowIndex: 0,
  endRowIndex: 1
}

or

@brainysmurf
brainysmurf / readme.md
Last active December 25, 2023 11:09
Extending classes in Libraries with AppsScripts

A question on an #AppsScripts user forum led me into an investigation into the best way to extend a class, for example a Date class, from inside a library so that the host project can use it.

Let's suppose we want to make a class BetterDate that extends the date object so that it automatically calculates the result of .getTime() once. (Maybe we don't want to waste time? rimshot)

We'll add an instance method on it to calculate how many days we have to wait (until our birthday). We also need a static object now() on it so that we can get a now date that is an instance of BetterDate.

This to me is most elegant:

// lib code
@brainysmurf
brainysmurf / README.md
Last active December 25, 2023 03:00
Named parameters, required parameters, and interfaces

Interfaces in Javascript for Google Apps Scripts

I really like named parameters. I can live without them, but there is one place where I really need them. Cases in which I want to create a class and the paramters are guaranteed to be present, nothing extra. This is one way of implementing them. (It's kinda ugly, but kinda cool.)

Project Key

Either copy into your project, or use as a library: MWzXfEDYTWuJ_Xj0ap9H8-68b30WIDiE_

Wrong behaviour

@brainysmurf
brainysmurf / PackageManager.js
Last active September 24, 2023 12:17
Package Manager: Manage libraries in apps script, establish convention for including packages, optionally use TDD.
/*
* PackageManager © Adam Morris classroomtechtools.ctt@gmail.com
* A toolchain solution for test-driven development and package management
* What it does:
* - Provides boilerplate code to make modular packages publishable as gists
* - Define and adds all those dependencies to the project so that there are completely modular
* - Access them in your own code with "pgk.namespace" where the namespace is defined in the dependency setup file
* - Test the modules with TDD practices!
* To use:
* - Make a copy: https://script.google.com/a/igbis.edu.my/d/1ClAoUIZdC5VJ0nM3_1NIYwW-sr-LLXT_9rLc9B0Tj5SikYWwBuFucyRu/edit)
@brainysmurf
brainysmurf / DBSheets.gs
Last active September 24, 2023 12:13
DBSheeets
(function (global, Factory) {
global.pkg = global.pkg || {};
global.pkg.dbsheets = (function wrapper (args) {
var wrapped = function () { return Factory.apply(Factory, arguments); }
for (i in args) { wrapped[i] = args[i]; }
return wrapped;
}({
extend: {
registered: [],
registerInit: function (func) {
@brainysmurf
brainysmurf / FormatLogger.gs
Last active September 24, 2023 12:12
FormatLogger is an alternative logging approach for the app script stack. To use, just copy the code in FormatLogger.gs into your project.
(function (global, Factory) {
global.formatLogger = function () { return Factory.apply(Factory, [global].concat(Array.prototype.slice.call(arguments))); }
formatLogger(); // can be overwritten with another call if transformers are desired
})(this,
function Package (global, options) {
options = options || {};
options.useLogger = options.useLogger || false;
if (options.useLogger)
options.loggerObject = Logger;