Skip to content

Instantly share code, notes, and snippets.

View erickoledadevrel's full-sized avatar

Eric Koleda erickoledadevrel

View GitHub Profile
@erickoledadevrel
erickoledadevrel / announcements_publisher.js
Created May 29, 2012 16:21
Apps Script Intro Example Code 2012-05-23
function publish() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('Announcements');
var data = sheet.getDataRange().getValues();
var site = SitesApp.getSite('erickoledauniversity');
var page = site.getChildByName('announcements');
for (var i = 0; i < data.length; i++) {
var row = data[i];
var title = row[1];
var description = row[2];
@erickoledadevrel
erickoledadevrel / SendDocument.js
Last active November 30, 2022 12:57
Send a Google Doc in an email using Apps Script
/**
* Sends an email using the contents of a Google Document as the body.
*/
function sendDocument(documentId, recipient, subject) {
var html = convertToHtml(documentId);
html = inlineCss(html);
GmailApp.sendEmail(recipient, subject, null, {
htmlBody: html
});
}
@erickoledadevrel
erickoledadevrel / Code.js
Last active May 9, 2022 09:47
Formatting date/time values using the user's timezone in Apps Script.
/**
* @file A sample showing how to format date/time values in Apps Script so that they appear in the
* user's timezone. View a working version here:
* https://docs.google.com/spreadsheets/d/1VlI8HibL9kh_wlmO0G1ltMGVo6S-urGrDqDbfEp9eeY/edit
*/
/**
* Add menu item after the spreadsheet opens.
*/
function onOpen() {
@erickoledadevrel
erickoledadevrel / Code.js
Last active February 20, 2020 13:32
Remove multiple line breaks in a Googe Document using Apps Script
function removeMultipleLineBreaks(element) {
if (!element) {
element = DocumentApp.getActiveDocument().getBody();
}
var parent = element.getParent();
// Remove empty paragraphs
if (element.getType() == DocumentApp.ElementType.PARAGRAPH
&& element.asParagraph().getText().replace(/\s/g, '') == '') {
if (!(parent.getType() == DocumentApp.ElementType.BODY_SECTION
&& parent.getChildIndex(element) == parent.getNumChildren() - 1)) {
@erickoledadevrel
erickoledadevrel / drive_console_helper.js
Last active August 29, 2015 14:10
The Google Drive SDK console only allows you to enter MIME types and file extensions one at a time. This script allows you to enter a comma-separated list of values into a text box and then have it split into multiple text boxes automatically.
(function() {
// Include jQuery.
var script = document.createElement('script');
script.src = 'https://code.jquery.com/jquery-2.1.1.min.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
var sections = [
'Default MIME Types',
'Default File Extensions',
@erickoledadevrel
erickoledadevrel / xmlToJson.js
Created December 1, 2014 14:30
A function to convert an XML string to a JSON object in Apps Script, using logic similar to the sunset method Xml.parse().
/**
* Converts an XML string to a JSON object, using logic similar to the
* sunset method Xml.parse().
* @param {string} xml The XML to parse.
* @returns {Object} The parsed XML.
*/
function xmlToJson(xml) {
var doc = XmlService.parse(xml);
var result = {};
var root = doc.getRootElement();
@erickoledadevrel
erickoledadevrel / FormSubmitFallback.gs
Last active April 9, 2022 18:56
Demonstrate how to create a form submit processing script that can handle missing or duplicate trigger firings.
// Change this values based on your spreadsheet.
var SHEET_NAME = 'Form Responses 1';
var STATUS_COLUMN_NUMBER = 4;
var PROCESSED_STATUS = 'Processed';
var LAST_ROW_KEY = 'lastRow';
var LOCK_TIMEOUT_MS = 60000; // 1 minute
var MAX_RUNTIME_MS = 240000; // 4 minutes
/**
@erickoledadevrel
erickoledadevrel / Code.gs
Created January 14, 2016 17:36
Polling in a sidebar only when the page is visible
function onOpen() {
SpreadsheetApp.getUi().createMenu('Testing')
.addItem('Show Sidebar', 'showSidebar')
.addToUi();
}
function showSidebar() {
var sidebar = HtmlService.createHtmlOutputFromFile('Sidebar')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('Sidebar');
@erickoledadevrel
erickoledadevrel / Code.gs
Created February 9, 2016 14:18
Time-based trigger for specific time
function createTrigger() {
var time = new Date();
time.setMinutes(time.getMinutes() + 5);
ScriptApp.newTrigger('run')
.timeBased()
.at(time)
.create();
}
function run() {
@erickoledadevrel
erickoledadevrel / Code.gs
Created February 19, 2016 14:38
Create a Google Calendar event with an attachment in Apps Script
function createEventWithAttachment() {
var driveFileId = '...';
var file = DriveApp.getFileById(driveFileId);
var event = {
summary: 'Test Event with Attachments',
description: 'Woot!',
attachments: [{
fileId: driveFileId,
fileUrl: file.getUrl(),
mimeType: file.getMimeType(),