Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
Cranking out the code... 👨‍💻

Andrew Roberts andrewroberts

💭
Cranking out the code... 👨‍💻
View GitHub Profile
@andrewroberts
andrewroberts / FuzzySet.gs
Created Sep 8, 2020
FuzzySet (https://glench.github.io/fuzzyset.js/) with a bit of re-packaging for use with Google Apps Script
View FuzzySet.gs
/**
* @param {string} value - value to test
* @param {array} valuesToTest
*
* @return the best match in the test array
*/
function getBestMatch(value, valuesToTest) {
var results = {}
@andrewroberts
andrewroberts / exportSpreadsheet.gs
Last active Oct 15, 2021 — forked from Spencer-Easton/exportSpreadsheet.gs
Example on how to export a Google sheet to various formats, includes most PDF options. Updated from comments in [Spencer Easton's Gist](https://gist.github.com/Spencer-Easton/78f9867a691e549c9c70)
View exportSpreadsheet.gs
function exportSpreadsheet() {
//All requests must include id in the path and a format parameter
//https://docs.google.com/spreadsheets/d/{SpreadsheetId}/export
//FORMATS WITH NO ADDITIONAL OPTIONS
//format=xlsx //excel
//format=ods //Open Document Spreadsheet
//format=zip //html zipped
@andrewroberts
andrewroberts / formatString.js
Created Feb 14, 2020
JavaScript function to format various value types as a string.
View formatString.js
function testNumber() {
var value = 1234.56789
var a = format(value, {numberOfDecimalPlaces: 2})
debugger
}
function testDate() {
var value = new Date()
var a = format(value)
@andrewroberts
andrewroberts / generateRandomString.gs
Last active Jul 26, 2020
Generate a random string of length n
View generateRandomString.gs
function generateRandomString(n) {
var chars = ['a', 'b','c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
chars.push('A', 'B', 'C', 'D', 'E', 'F','G','H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
var randomString = '';
for (i=0; i < n; i++) {
r = Math.random();
r = r * 61;
r = Math.round(r);
randomString = randomString + chars[r];
}
@andrewroberts
andrewroberts / Properties_.gs
Created Jul 16, 2018
Google Apps Script snippet to manage and store persistent state. Uses both Firebase (FirebaseApp library), CacheService and BBLog for logging.
View Properties_.gs
// 34567890123456789012345678901234567890123456789012345678901234567890123456789
// JSHint - TODO
/* jshint asi: true */
(function() {"use strict"})()
// Properties_.gs
// ==============
//
@andrewroberts
andrewroberts / createPDF_filename.gs
Last active Mar 11, 2021
This is a Google Apps Script that creates a PDF file using a Google Doc template and the values in a Google Spreadsheet. It is based on www.andrewroberts.net/2014/10/google-apps-script-create-pdf/ but allows placeholders to be used in the filename.
View createPDF_filename.gs
// dev: andrewroberts.net
// Replace this with ID of your template document.
var TEMPLATE_ID = ''
// var TEMPLATE_ID = '1wtGEp27HNEVwImeh2as7bRNw-tO4HkwPGcAsTrSNTPc' // Demo template
// Demo script - http://bit.ly/createPDF
// You can specify a name for the new PDF file here, or leave empty to use the
// name of the template. Placeholders can also be placed in here.
@andrewroberts
andrewroberts / API_.gs
Created Sep 25, 2017
Google Apps Script snippet for accessing the Quick Books Online API
View API_.gs
// 34567890123456789012345678901234567890123456789012345678901234567890123456789
// JSHint - TODO
/* jshint asi: true */
(function() {"use strict"})()
// API_.gs
// =======
//
@andrewroberts
andrewroberts / fillInHtmlTemplate.gs
Created Sep 17, 2017
Google Apps Script function to replace placeholder values in an HTML template (from a draft GMail for example)
View fillInHtmlTemplate.gs
/**
* For each placeholder - "{{[placeholder]}}", strip out any HTML and replace
* it with the appropriate key value. An example use of this could be using the
* HTML from a draft GMail as a template.
*
* E.g. If the obect were:
*
* {PlaceHolder1: newValue}
*
* In the template "{{PlaceHolder1}}" would be replaced with "newValue" even
@andrewroberts
andrewroberts / PDFCreator_SendMultiplePDFs.gs
Last active Nov 15, 2020
Google Apps Script for creating and emailing multiple PDFs using data from a Google Sheet and a Google doc template.
View PDFCreator_SendMultiplePDFs.gs
/*
PDF Creator - Send multiple PDFs
================================
On selecting a number of contiguous rows and clicking "Create PDFs > Create PDFs
for selected rows" this script constructs a PDF for each selected row in the
attached GSheet.
The value in the "File Name" column is used to name the file and - if there is a
@andrewroberts
andrewroberts / StackTraces-Logging-GAS.md
Last active Jul 7, 2017 — forked from hlecuanda/StackTraces-Logging-GAS.md
Example using Stackdriver logging for debugging a custom function for sheets in Google Apps Script. / Writing a Caching custom function
View StackTraces-Logging-GAS.md

Logging custom functions using stackdriver in google apps script

Recently, console logging became available for Google Apps Script projects. It used to be impossible to use the Log service that comes with the GAS runtime, but now all you need to do is throw an exception. Exceptions get logged in stackdriver logging and when enabled, unhandled exceptions will not stop your script execution. This adds up to nearly 0 lag if you are using this feature (?) by purposely throwing exceptions, and you can get creative with your error message to avoid having to expand stackdriver's log messages (which are pretty comprehensive stacktraces!)

Setup