Skip to content

Instantly share code, notes, and snippets.

View mogsdad's full-sized avatar

David Bingham mogsdad

View GitHub Profile
@mogsdad
mogsdad / NSLookup.gs
Last active September 12, 2018 05:48
NSLookup - Google Sheets custom function to Perform a Network Service Lookup, using StatDNS API. From "Nslookup or dig in Google App Script" (http://stackoverflow.com/a/30610580/1677912).
/**
* Perform a Network Service Lookup, using StatDNS API.
*
* @param {"google.com"} dn A well-formed domain name to resolve.
* @return {String} Resolved IP address
* @customfunction
*/
function NSLookup(dn) {
// From gist.github.com/mogsdad/ece531531e0ef5dc355d
var url = "http://api.statdns.com/%FQDN%/a".replace("%FQDN%",dn);
/**
* Return the time that the referenced cell or range was last changed.
* Initial use shows current time.
*
* @param {Sheet3!B32} reference Cell or range to monitor.
* @returns The time the reference was last changed.
* @customfunction
*/
function lastModified( reference ) {
// From gist.github.com/mogsdad/3bef949fcae5b4411524
function test_listFilesInFolder() {
listFilesInFolder("StackOverflow");
}
/**
* @OnlyCurrentDoc Limits the script to only accessing the current spreadsheet.
*/
function onOpen() {
@mogsdad
mogsdad / Apps Script pdfToText utility.md
Last active February 21, 2025 22:46
For http://stackoverflow.com/questions/26613809, a question about getting pdf attachments in gmail as text. I got a little carried away - this does much more than asked.

Google Apps Script pdfToText Utility#

This is a helper function that will convert a given PDF file blob into text, as well as offering options to save the original PDF, intermediate Google Doc, and/or final plain text files. Additionally, the language used for Optical Character Recognition (OCR) may be specified, defaulting to 'en' (English).

Note: Updated 12 May 2015 due to deprecation of DocsList. Thanks to Bruce McPherson for the getDriveFolderFromPath() utility.

    // Start with a Blob object
    var blob = gmailAttchment.getAs(MimeType.PDF);
    
@mogsdad
mogsdad / README.md
Last active January 7, 2017 16:31
jQuery UI theme that is visually consistent with the Google Apps Script CSS package for add-ons. (https://developers.google.com/apps-script/add-ons/css)

"Googly" jQuery UI theme

In order to publish Google Docs & Sheets add-ons, their UIs need to conform with the guidelines provided in the UI Style Guide for Add-ons.

None of the 'canned' css themes for jQuery UI match the look of the CSS Package for Add-ons, which makes the use of jQuery UI components in HtmlService difficult.

Enter: the "Googly" jQuery UI theme! This theme leaves most font selection to be cascaded from the CSS Package for Add-ons, and foregoes all icons. Margins and padding are set to 0 in most cases, leaving spacing to be driven by wrapping elements in add-on css classes.

Recommendation: loading CSS within Google Apps Script is very, very, very slow. If you are using a subset of jQuery UI widgets, take a copy of this css file and remove the sections that are irrelevant to you. To link to the whole theme:

Google Apps Script Spreadsheet Utilities and Custom Functions#

These utilities are grouped into related files, for simpler copy & paste to your scripts.

ConvertA1.gs

A couple of helper functions to convert to & from A1 notation.

cellA1ToIndex( string cellA1, number index )

Apps Script Google+ Domains Service Utilities

  • PlusDomainUtils.js
  • getProfile(userId) - Retreive Google+ Profile for the given userId.
  • getPicUrl(userId) - Return simple url to Google+ profile picture for the given user.
  • test_getPicUrl() - test harness
  • getPathFromUrl(url) - strip query string to return bare URL
@mogsdad
mogsdad / dataTableFromArray.js
Created January 30, 2014 17:51
Google Apps Script's Charts Service uses a DataTable object as the source data for visualizations and tables. There is a built-in method that can create a DataTable given a Spreadsheet Range, but if you have a javascript table or array that you want to visualize, you need to use the DataTableBuilder class' primitive methods. This function simpli…
/**
* Produce a dataTable object suitable for use with Charts, from
* an array of rows (such as you'd get from Range.getValues()).
* Assumes labels are in row 0, and the data types in row 1 are
* representative for the table.
*
* @param {Array} data Array of table rows
*
@ @returns {DataTable} Refer to GAS documentation
*/
@mogsdad
mogsdad / uniqueFrom.js
Created January 9, 2014 18:48
Javascript Array extension to find the members of a set (array) that are unique, that is they are not members of other sets. See http://goo.gl/ArHbvb.
/**
* Returns a non-destructive Array of elements that are not found in
* any of the parameter arrays.
*
* @param {...Array} var_args Arrays to compare.
*/
Array.prototype.uniqueFrom = function() {
if (!arguments.length)
return [];
var a1 = this.slice(0); // Start with a copy

Google Apps Script Document Utilities

  • getAllLinks.js

  • getAllLinks(element) - returns array of all UrlLinks in Document

  • findAndReplaceLinks(searchPattern,replacement) - changes all matching links in Document

  • changeCase.js - Document add-in, provides case-change operations in the add-in Menu.

  • onOpen - installs "Change Case" menu

  • _changeCase - worker function to locate selected text and change text case. Case conversion is managed via callback to a function that accepts a string as a parameter and returns the converted string.

  • helper functions for five cases