简体   繁体   English

我想自动加载Google文档脚本吗?

[英]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. 我假设您可以将模板添加到此简单脚本中。

问题未解决?试试以下方法:

我想自动加载Google文档脚本吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试自动发送通过应用程序脚本创建的 Google 文档的电子邮件 - Trying to send email automatically of a Google Docs created through apps script 我必须使用谷歌应用程序脚本,我希望自动对数据进行排序 - I have to google apps script that I want to have the data sorted automatically 我可以自动将标题文本添加到 Google 文档文件的页脚吗? - Can I add Heading text to the footer in a Google Docs file automatically? Excel to Google Docs-脚本 - Excel to Google Docs - Script 我希望我在列的单元格内输入的值自动转换为负谷歌应用脚本 - I would like the values I enter inside the cells of the column to be automatically transformed into negative google app script Google 脚本 - 在 Google 文档上运行 - Google Script - Running on Google Docs 如何使用脚本在Google文档中获取修订历史记录? - How can I get the revision history in Google Docs using a script? 是否可以从谷歌应用程序脚本在谷歌文档中编写方程(如积分或函数限制)? - Is it possible to write equations in google docs (like integrals or limits of functions) from google apps script? 在脚本控制下的 Google Docs 中,插入表格后自动插入的段落可以通过编程方式解决吗? - In Google Docs under script control, can a paragraph inserted automatically after the insertion of a table be addressed programmatically? Google文档自定义刷新脚本 - Google Docs Custom Refresh Script
 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM