Skip to content

Instantly share code, notes, and snippets.

@MauricioMoraes
MauricioMoraes / get_first_gmail_draft_attributes.gs
Last active August 29, 2015 14:25
Google Apps Script Function to get the first gmail draft from your account and output its attributes.
function getFirstGmailDraftAttributes() {
var draftThreads = GmailApp.search("in:drafts", 0, 1);
if (draftThreads.length === 0) {
Browser.msgBox("No drafts found.");
return
}
var draft = draftThreads[0].getMessages()[0];
@MauricioMoraes
MauricioMoraes / column_number_by_name.gs
Last active August 29, 2015 14:25
Google Apps Script for Spreadsheets - Gets the column number from a given sheet name and column name. The function assumes the column names are on the first row. The resulting column number is zero-based (Column A -> 0)
function columnNumberByName(sheetName, ColumnName) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName(sheetName)
var firstRowRange = sheet.getRange(1, 1, 1, sheet.getMaxColumns());
var columnNames = firstRowRange.getValues()[0];
for (var columnNumber in columnNames) {
if (columnNames[columnNumber] == ColumnName) {
return parseInt(columnNumber);
@MauricioMoraes
MauricioMoraes / gist:a8f4b4bd89b647b749f5
Created July 27, 2015 16:57
Google Apps Script for Spreadsheets - Find the relative row number within a range that has a a cell with given target value - Range in A1 Notation starting
function findRowNumberForCellWithValue(targetValue, rangeA1Notation, sheet) {
var range = sheet.getRange(rangeA1Notation);
var values = range.getValues()
for (var i = 0; i < values.length; i++) {
if (values[i][0] == targetValue) {
return i+1;
}
}
}
@MauricioMoraes
MauricioMoraes / jquery_datatable_checkboxes_with_pagination.js
Last active August 29, 2015 14:26
Jquery DataTables - Workaround to have a selectable list of options with dataTables and Pagination. When the form is submitted, the script inserts hidden inputs with the values of the checkboxes that are not visible, due to pagination.
function setupDatatableCheckboxesWithPagination(tableSelector, attributeName) {
var dataTableObject = $(tableSelector).dataTable();
form = $(tableSelector).closest("form");
$(form).submit(function(){
insertHiddenInputsWithCheckboxesValues(tableSelector, attributeName);
dataTableObject.fnFilter('');
});
};
@MauricioMoraes
MauricioMoraes / one_level_flatten.js
Created October 22, 2015 12:00
Javascript Flatten (for one level nested arrays) - Useful for google apps scripts on spreadsheets
// Takes and array of arrays matrix and return an array of elements.
function flatten(arrayOfArrays){
return [].concat.apply([], arrayOfArrays);
}
@MauricioMoraes
MauricioMoraes / column_numbers_from_names.gs
Last active October 22, 2015 12:47
Get column numbers from column names - Google apps scripts for spreadsheets
// Pass the column headers range and names, and it will return an object with the relation between column names and column numbers
function getColumnNumbersForNames(sheetName, columnHeadersRangeA1Notation, columnNames){
var thisSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = thisSpreadsheet.getSheetByName(sheetName);
var columnHeadersRange = sheet.getRange(columnHeadersRangeA1Notation);
var columnNamesOnSheet = flatten(columnHeadersRange.getValues());
var columnNumbersForNames = {};
for(var count = 0; count < columnNames.length; count++){
var columnName = columnNames[count]
@MauricioMoraes
MauricioMoraes / brazilian_phone_mask_with_area_code.js
Last active May 16, 2022 04:10
Mask for 8 or 9 digit phones in brazilian format with area code (ddd) - Using Jquery.inputmask plugin
// Using jquery.inputmask: https://github.com/RobinHerbots/jquery.inputmask
// For 8 digit phone fields, the mask is like this: (99) 9999-9999
// For 9 digit phone fields the mask is like this: (99) 99999-9999
function setupPhoneMaskOnField(selector){
var inputElement = $(selector)
setCorrectPhoneMask(inputElement);
inputElement.on('input, keyup', function(){
setCorrectPhoneMask(inputElement);
});
@MauricioMoraes
MauricioMoraes / delete_all_shapes_from_xls.vba
Created March 30, 2016 13:24
Deletes all pictures and shapes from excel spreadsheet (does it for all sheets inside the document)
Option Explicit
Sub delete_all_shapes()
Dim shape As Shape
Dim worksheet As Worksheet
Dim response As Integer
response = MsgBox("This action will delete all pictures and shapes from your spreadsheet. Do you want to continue?", vbYesNo, "Delete all shapes from spreadsheet")
If response = vbYes Then
For Each worksheet In ThisWorkbook.Worksheets
@MauricioMoraes
MauricioMoraes / list-files-in-folder.js
Created June 2, 2016 11:56 — forked from hubgit/list-files-in-folder.js
List all files in a folder (Google Apps Script)
function listFilesInFolder() {
var folder = DocsList.getFolder("Maudesley Debates");
var contents = folder.getFiles();
var file;
var data;
var sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();
@MauricioMoraes
MauricioMoraes / access_postgresql_with_docker.md
Last active August 30, 2024 05:21
Allow Docker Container Access to Host's Postgres Database on linux (ubuntu)

You have to do 2 things in order to allow your container to access your host's postgresql database

  1. Make your postgresql listen to an external ip address
  2. Let this client ip (your docker container) access your postgresql database with a given user

Obs: By "Host" here I mean "the server where docker is running on".

Make your postgresql listen to an external ip address

Find your postgresql.conf (in case you don't know where it is)

$ sudo find / -type f -name postgresql.conf