Skip to content

Instantly share code, notes, and snippets.

@rachelslurs
Created March 11, 2014 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rachelslurs/9496821 to your computer and use it in GitHub Desktop.
Save rachelslurs/9496821 to your computer and use it in GitHub Desktop.
/**
* The onOpen function runs automatically when the Google Docs document is
* opened. Use it to add custom menus to Google Docs that allow the user to run
* custom scripts. For more information, please consult the following two
* resources.
*
* Extending Google Docs developer guide:
* https://developers.google.com/apps-script/guides/docs
*
* Document service reference documentation:
* https://developers.google.com/apps-script/reference/document/
*/
function onOpen() {
DocumentApp.getUi().createMenu('Template Tags')
.addItem('Replace Me!', 'replaceMe')
.addToUi();
}
function replaceMe () {
var result = DocumentApp.getUi().prompt('What do you want to change {{REPLACE_ME}} to?', DocumentApp.getUi().ButtonSet.OK_CANCEL);
var responseText = null;
if(result.getSelectedButton() == DocumentApp.getUi().Button.OK){
responseText = result.getResponseText();
if (responseText.length > 0) {
var bodyElement = DocumentApp.getActiveDocument().getBody();
bodyElement.replaceText('{{REPLACE_ME}}', responseText);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment