Skip to content

Instantly share code, notes, and snippets.

@amorri40
Last active August 29, 2015 13:56
Show Gist options
  • Save amorri40/9016722 to your computer and use it in GitHub Desktop.
Save amorri40/9016722 to your computer and use it in GitHub Desktop.
/**
* Inserts a warning box under the current cursor location
*/
function insertWarningTableAtCursor() {
if (!doesTheUserWantToContinue('This will insert a warning Box at your current cursor location, are you sure you want to do this?')) return;
var cursor = DocumentApp.getActiveDocument().getCursor();
if (!cursor) {showCursorError(); return;}
/*
Setup the style for the whole box (yellow for warning)
*/
var style = setupStyle('#fff2cc','#000000', true);
/*
Create the main table
*/
var cells = [['', 'Warning: Warning Text goes here']];
var warningTable = insertTableAtCursor(cursor,cells,style);
warningTable.setColumnWidth(0, 64) //the first column should be small (just a picture of a warning sign)
/*
Setup the picture Cell
*/
var pictureCell = warningTable.getCell(0, 0);
pictureCell.setAttributes(style);
pictureCell.clear();
var img = getImage('0B1d2AYWnsGhXVWYtbnAxemFXRmc'); //replace with your own image id
var warningInlineImage = appendImageToElement(pictureCell, img, 64,64)
/*
Setup the content Cell style
*/
styleACell(warningTable,0,1,style)
}
function insertTerminalAtCursor() {
if (!doesTheUserWantToContinue('This will insert a Terminal Command Box at your current cursor location, are you sure you want to do this?')) return;
var cursor = DocumentApp.getActiveDocument().getCursor();
if (!cursor) {showCursorError(); return;}
var cells = [['./ccadmin.sh']];
var style={}
style[DocumentApp.Attribute.BORDER_WIDTH] = 3;
style[DocumentApp.Attribute.BORDER_COLOR] = '#999999';
var terminalTable = insertTableAtCursor(cursor,cells,style);
var cellStyle = setupStyle('#000000', '#FFFFFF', true);
styleACell(terminalTable,0,0,cellStyle)
}
function insertCodeSnippetAtCursor() {
var cursor = DocumentApp.getActiveDocument().getCursor();
if (!cursor) {showCursorError(); return;}
var response = UrlFetchApp.fetch("http://pygments.appspot.com/")
}
/*
LIBRARY FUNCTIONS
*/
function appendImageToElement(element, img, width,height) {
var inlineImage = element.appendImage(img);
inlineImage.setHeight(height);
inlineImage.setWidth(width);
return inlineImage;
}
function getImage(fileId) {
return DriveApp.getFileById(fileId).getBlob();
}
function styleACell(table,x,y,style) {
var mainCell = table.getCell(x, y);
mainCell.setAttributes(style);
}
function insertTableAtCursor(cursor,cells,style) {
var currentElement = cursor.getElement();
var elementIndex = currentElement.getParent().getChildIndex(currentElement);
var newTable = currentElement.getParent().asBody().insertTable(elementIndex+1,cells)
if (style)
newTable.setAttributes(style);
return newTable;
}
function setupStyle(back_colour, fore_colour ,bold) {
var style = {};
style[DocumentApp.Attribute.BACKGROUND_COLOR] = back_colour;
style[DocumentApp.Attribute.FOREGROUND_COLOR] = fore_colour;
style[DocumentApp.Attribute.BOLD] = bold;
return style;
}
function showCursorError() {
DocumentApp.getUi().alert('Cannot find a cursor in the document.');
}
function doesTheUserWantToContinue(message) {
var result = DocumentApp.getUi().alert(
'NSN Plugin',
message,
DocumentApp.getUi().ButtonSet.YES_NO);
if (result == DocumentApp.getUi().Button.YES) {
return true;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment