Reputation: 11
I have a script that automatically updates a date in the header of a google document upon open. I'd like to not have to install this script individually for each document. I can create a template, but then everytime I create a new document, I have to go to New > Google Docs > From a template, and then select my template. I'd like to just click New > Google Docs, and have this template automatically load.
Upvotes: 1
Views: 104
Reputation: 64062
You could use a dialog like this:
Code:
function onOpen(){
SpreadsheetApp.getUi().createMenu('My Menu')
.addItem("Open Templated Google Doc", 'showMyDialog')
.addToUi()
}
function createTemplatedGoogleDoc(name){
var doc=DocumentApp.create(name);
doc.addHeader().appendParagraph(Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "E MMM dd, yyyy"))
//doc.getBody().appendParagraph(Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "E MMM dd, yyyy"));
//doc.getBody().getChild(0).removeFromParent();
doc.saveAndClose()
return doc.getUrl();
}
function showMyDialog(){
var ui=HtmlService.createHtmlOutputFromFile('docwithdate');
SpreadsheetApp.getUi().showModelessDialog(ui, 'My Doc with Date');
}
docwithdate.html:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<script>
function createFile(){
var name=document.getElementById('filename').value;
google.script.run
.withSuccessHandler(function(url){
var html='<a href="'+ url +'">Go To File</a>';
$(html).appendTo("body");
})
. createTemplatedGoogleDoc(name);
}
</script>
<body>
<input type="text" id="filename" />
<input type="button" value="Create File" onClick="createFile()" />
</body>
</html>
The dialog looks like this:
You type in the filename and click the button. The script returns a link to your new file.
This gets rid of the dialog also:
function onOpen(){
SpreadsheetApp.getUi().createMenu('My Menu')
.addItem("Open Templated Google Doc", 'showMyDialog')
.addToUi()
}
function createTemplatedGoogleDoc(name){
var doc=DocumentApp.create(name);
doc.addHeader().appendParagraph(Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "E MMM dd, yyyy"))
//doc.getBody().appendParagraph(Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "E MMM dd, yyyy"));
//doc.getBody().getChild(0).removeFromParent();
doc.saveAndClose()
var rObj={url:doc.getUrl(),filename:doc.getName()}
return rObj;
}
function showMyDialog(){
var ui=HtmlService.createHtmlOutputFromFile('docwithdate');
SpreadsheetApp.getUi().showModelessDialog(ui, 'My Doc with Date');
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<script>
function createFile(){
var name=document.getElementById('filename').value;
google.script.run
.withSuccessHandler(function(rObj){
var html='<br /><a href="'+ rObj.url +'" onClick="google.script.host.close();">Go To File:' + rObj.filename + '</a>';
$(html).appendTo("body");
})
. createTemplatedGoogleDoc(name);
}
</script>
<body>
<input type="text" id="filename" />
<input type="button" value="Create File" onClick="createFile()" />
</body>
</html>
I would probably make it a webapp and put it in a bookmark for easy access. So you'll just have to add this:
function doGet(){
return HtmlService.createHtmlOutputFromFile('docwithdate');
}
and deploy it.
I assume that you can add the template to this simple script.
Upvotes: 0