Skip to content

Instantly share code, notes, and snippets.

Andrew Roberts andrewroberts

Block or report user

Report or block andrewroberts

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@andrewroberts
andrewroberts / generateRandomString.gs
Created Sep 1, 2019
Generate a randon 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 Sep 27, 2019
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
Created Aug 28, 2017
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 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
value - it is emailed to the recipient in the "Email" column.
@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

@andrewroberts
andrewroberts / PDFCreator_EmailAllResponses
Last active Sep 25, 2019
Google Apps Script to create a PDF from each of the rows in a Google Sheet
View PDFCreator_EmailAllResponses
/*
PDF Creator - Email all responses
=================================
When you click "Create PDF > Create a PDF for each row" this script
constructs a PDF for each row in the attached GSheet. The value in the
"File Name" column is used to name the file and - if there is a
value - it is emailed to the recipient in the "Email" column.
@andrewroberts
andrewroberts / convertSpreadsheetToPdf.gs
Last active Apr 11, 2019
Convert a multi-sheet Google Spreadsheet into a PDF, with option to email it.
View convertSpreadsheetToPdf.gs
/*
* Save spreadsheet as a PDF
*
* Based on Dr.Queso's answer in http://stackoverflow.com/questions/30367547/convert-all-sheets-to-pdf-with-google-apps-script/30492812#30492812
*
* @param {String} email Where to send the PDF [OPTIONAL]
* @param {String} spreadsheetId Or the active spreadsheet[OPTIONAL]
* @param {String} sheetName The tab to output [OPTIONAL]
* @param {String} PdfName [OPTIONAL]
*/
@andrewroberts
andrewroberts / EmailPDFOfFormSubmission.gs
Last active Mar 22, 2018
Email a PDF of a Google Form submission.
View EmailPDFOfFormSubmission.gs
/*
PDF Create - Email on form submit
=================================
This script creates a PDF populated with the values submitted in a
Google form.
The "on form submit" trigger needs to be manually created:
You can’t perform that action at this time.