Skip to content

Instantly share code, notes, and snippets.

View mogsdad's full-sized avatar

David Bingham mogsdad

View GitHub Profile

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

@mogsdad
mogsdad / Apps Script pdfToText utility.md
Last active April 10, 2024 01:38
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);
    

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 )

@mogsdad
mogsdad / Download file from URL.js
Last active December 8, 2023 02:39
In StackOverflow question #14573055, the question was how to "download" a file from a URL using Google Apps-Script. See http://stackoverflow.com/questions/14573055/can-i-download-file-from-url-link-generated-by-google-apps-script/14574217#14574217. You can't actually "download" the source file, because apps-script has no access to your PC's file…
/**
* Retrieve a file from the given URL, store into the named folder
* on Google Drive.
*
* @param {String} fileURL URL to source file, e.g. "http://mysite.com/files/file.val1.val22.zip"
* @param {String} folder Name of target folder on Google Drive
*
* @returns {Object} Response of operation, e.g.
* {rc:200,fileName:"test.zip",fileSize:92994392}
*/
@mogsdad
mogsdad / httpGetTemplate.gs
Created September 9, 2015 02:39
Google Apps Script template functions for external host communication with UrlFetchApp. See https://mogsdad.wordpress.com.
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* httpGetTemplate.gs
*
* A pair of template functions for external host communication with
* UrlFetchApp, including parameter encoding and error handling.
*
* From: gist.github.com/mogsdad/a76c32231a2b91ff8b59
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function test_listFilesInFolder() {
listFilesInFolder("StackOverflow");
}
/**
* @OnlyCurrentDoc Limits the script to only accessing the current spreadsheet.
*/
function onOpen() {

Google Apps Script Gmail Utilities

##sendAndLabel(recipient, subject, body, options, label)##

An alternative to GmailApp.sendEmail(), which applies a label to the message thread in the sender's account.

Sends an email message with optional arguments. The email can contain plain text or an HTML body. The size of the email

@mogsdad
mogsdad / Rescue Files.gs
Last active September 14, 2022 16:20
This is the full script solution for Stack Overflow Q14518410, "a google apps script to move or restore google drive's files from the trash"http://stackoverflow.com/questions/14518410/a-google-apps-script-to-move-or-restore-google-drives-files-from-the-trash
// See http://stackoverflow.com/questions/14518410/a-google-apps-script-to-move-or-restore-google-drives-files-from-the-trash
var addonName = 'Rescue Files';
var rescueFldrName = 'Rescued Files'; // The name of the folder that rescued files should be placed into
/**
* Adds an addon menu to the active spreadsheet.
*/
function onOpen() {
var ui = SpreadsheetApp.getUi();
@mogsdad
mogsdad / Google Apps Script Survey Workflow.md
Last active August 1, 2022 07:48
Google Apps Script workflow for an email survey. Written in response to StackOverflow question 18668828. http://stackoverflow.com/a/18669532/1677912

Google Apps Script Survey Workflow

The components involved in this workflow are:

  • A script to generate and send an email with an HTML form.
  • An html template for that email, which allows us to customize the email for each recipient.
  • A doPost() function to handle responses. The script must be [deployed as a Web App][1].
  • A spreadsheet to collect responses. The script will be contained in the spreadsheet, and extends the spreadsheet UI with a menu for sending a copy of the survey. (It could be adapted for standalone use, without the UI component.)

Here is an example of such a workflow, conducting a Commuting Survey. Recipients will receive a survey email like this:

SheetConverter library (formerly "Spreadsheet to HTML")

This is the source repository for the SheetConverter Google Apps Script library.

Libary documentation is available here.

Caveats

This script is incomplete, ignoring some types of formatting. (Feel free to fork and enhance it, if you wish! Broadly applicable enhancements can be merged and the library updated) There are also some known issues: