[英]I'd like to have a Google Docs script automatically load?
I have a script that automatically updates a date in the header of a google document upon open. 我有一个脚本,可以在打开时自动更新google文档标题中的日期。 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.
我可以创建模板,但是每次创建新文档时,都必须转到“新建”>“ Google文档”>“来自模板”,然后选择我的模板。 I'd like to just click New > Google Docs, and have this template automatically load.
我只想点击新建> Google文档,然后自动加载此模板。
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: 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. 我可能会使其成为一个webapp,并将其放在书签中以便于访问。 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.
我假设您可以将模板添加到此简单脚本中。
问题未解决?试试以下方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.