Last active
August 29, 2015 14:05
-
-
Save dan-bennett/15acde2500ac659ccac2 to your computer and use it in GitHub Desktop.
Add Indent and Unindent Functionality to Google Sheets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add indentation / unindentation to Google Sheets | |
* | |
* @author Dan Bennett <[email protected]> | |
* @package SpreadsheetIndent | |
* @version 5.0.0 | |
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-clause | |
* @todo Toggles for common indentation setups (4 spaces, 1 tab, ">>", " »") | |
* @todo Keyboard shortcuts not possible, what about right-click? | |
*/ | |
/** | |
* Create Indentation menu | |
* | |
* @return void | |
*/ | |
function onOpen() { | |
setLength(getLength()); | |
setCharacter(getCharacter()); | |
var ui = SpreadsheetApp.getUi(); | |
ui.createMenu('Indentation') | |
.addItem('Indent', 'doIndent') | |
.addItem('Unindent', 'doUnindent') | |
.addItem('Fully Unindent', 'doFullUnindent') | |
.addSeparator() | |
.addItem('Change Indentation Level', 'showLengthPrompt') | |
.addItem('Change Indentation Character', 'showCharacterPrompt') | |
.addToUi(); | |
}; | |
/** | |
* Loop all selected cells and prepend indentation to any that contain a value | |
* | |
* @return void | |
*/ | |
function doIndent() { | |
var indent = getIndent(); | |
var range = SpreadsheetApp.getActiveRange(); | |
var values = range.getValues(); | |
values.forEach (function (row, ri) { | |
row.forEach ( function (cell, ci) { | |
if(cell.length > 0){ | |
values[ri][ci] = indent + cell; | |
} | |
}); | |
}); | |
range.setValues(values); | |
}; | |
/** | |
* Loop all selected cells and remove one level of indentation from the beginning if there is any | |
* | |
* @return void | |
*/ | |
function doUnindent() { | |
var indent = getIndent(); | |
var range = SpreadsheetApp.getActiveRange(); | |
var values = range.getValues(); | |
values.forEach (function (row, ri) { | |
row.forEach ( function (cell, ci) { | |
if(cell.indexOf(indent) === 0){ | |
values[ri][ci] = cell.substring(indent.length); | |
} | |
}); | |
}); | |
range.setValues(values); | |
}; | |
/** | |
* Loop all selected cells and remove all indentation from the beginning if there is any | |
* | |
* @return void | |
*/ | |
function doFullUnindent() { | |
var range = SpreadsheetApp.getActiveRange(); | |
var values = range.getValues(); | |
values.forEach (function (row, ri) { | |
row.forEach ( function (cell, ci) { | |
if(cell.length > 0){ | |
values[ri][ci] = ltrim(cell); | |
} | |
}); | |
}); | |
range.setValues(values); | |
} | |
/** | |
* Show a prompt displaying the current number of characters used and asking for a new value | |
* | |
* @return void | |
*/ | |
function showLengthPrompt() { | |
var length = getLength(); | |
var ui = SpreadsheetApp.getUi(); | |
var result = ui.prompt( | |
'Change Indentation Level', | |
'Indentation currently set to '+length+' characters.\r\n\r\nPlease enter the number of characters you wish to use:', | |
ui.ButtonSet.OK_CANCEL); | |
// Process the user's response. | |
var button = result.getSelectedButton(); | |
length = result.getResponseText(); | |
if (button == ui.Button.OK) { | |
setLength(parseInt(length)); | |
} | |
} | |
/** | |
* Set the amount of characters used to indent | |
* | |
* @param string The amount of characters to use for indentation | |
* | |
* @return void | |
*/ | |
function setLength(length) { | |
if(isNaN(length)) { | |
length = '4'; | |
} | |
var userProperties = PropertiesService.getDocumentProperties(); | |
userProperties.setProperty("INDENT_LENGTH", length); | |
} | |
/** | |
* Get the amount of characters used to indent | |
* | |
* @return string The amount of characters to use for indentation | |
*/ | |
function getLength() { | |
var userProperties = PropertiesService.getDocumentProperties(); | |
return parseInt(userProperties.getProperty("INDENT_LENGTH")); | |
} | |
/** | |
* Show a prompt displaying the current indentation character used and asking for a new value | |
* | |
* @return void | |
*/ | |
function showCharacterPrompt() { | |
var char = getCharacter(); | |
var ui = SpreadsheetApp.getUi(); | |
var result = ui.prompt( | |
'Change Indentation Character', | |
'Indentation is currently using "'+char+'".\r\n\r\nPlease enter the new character you wish to use:', | |
ui.ButtonSet.OK_CANCEL); | |
// Process the user's response. | |
var button = result.getSelectedButton(); | |
char = result.getResponseText(); | |
if (button == ui.Button.OK) { | |
setCharacter(char); | |
} | |
} | |
/** | |
* Set the character used to indent | |
* | |
* @param string char The character to use for indentation | |
* | |
* @return void | |
*/ | |
function setCharacter(char) { | |
if("" == char) { | |
char = " "; | |
} | |
var userProperties = PropertiesService.getDocumentProperties(); | |
userProperties.setProperty("INDENT_CHARACTER", char); | |
} | |
/** | |
* Get the character used to indent | |
* | |
* @return string The character to use for indentation | |
*/ | |
function getCharacter() { | |
var userProperties = PropertiesService.getDocumentProperties(); | |
return userProperties.getProperty("INDENT_CHARACTER"); | |
} | |
/** | |
* Create the identation string to use for adding/removing indent levels | |
* | |
* @return string A string of `char` characters of length `spaces` | |
*/ | |
function getIndent(){ | |
var char = getCharacter(); | |
var spaces = getLength(); | |
var indent = ""; | |
for(i = 0; i < spaces; i++){ | |
indent += char; | |
} | |
return indent; | |
} | |
/** | |
* Remove all leading spaces | |
* | |
* @param string stringToTrim The input string to unindent | |
* | |
* @return string The input string with all leading space characters removed | |
*/ | |
function ltrim(stringToTrim) { | |
if(getCharacter() == " ") { | |
return stringToTrim.replace(/^\s+/,""); | |
} else { | |
pattern = new RegExp("^"+escapeRegExp(getCharacter())+"+"); | |
return stringToTrim.replace(pattern,""); | |
} | |
} | |
/** | |
* Escape special characters for use in regex patterns | |
* | |
* @param string string The input string to escape | |
* | |
* @return string The input string with all special characters escaped | |
*/ | |
function escapeRegExp(string){ | |
return string.replace(/([.*+?^${}()|\[\]\/\\])/g, "\\$1"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment