繁体   English   中英

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

[英]I'd like to have a Google Docs script automatically load?

提示:本站提供中英文对照查看,鼠标放在中文字句上可显示英文原文。 若本文未解决您的问题,推荐您尝试使用帮您解决。

我有一个脚本,可以在打开时自动更新google文档标题中的日期。 我不想为每个文档单独安装此脚本。 我可以创建模板,但是每次创建新文档时,都必须转到“新建”>“ Google文档”>“来自模板”,然后选择我的模板。 我只想点击新建> Google文档,然后自动加载此模板。

您可以使用如下对话框:

码:

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>

该对话框如下所示:

在此处输入图片说明

输入文件名,然后单击按钮。 该脚本返回到您的新文件的链接。

这也摆脱了对话框:

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>

我可能会使其成为一个webapp,并将其放在书签中以便于访问。 因此,您只需要添加以下内容:

function doGet(){
  return HtmlService.createHtmlOutputFromFile('docwithdate');
}

并部署它。

我假设您可以将模板添加到此简单脚本中。

问题未解决?试试使用:帮您解决问题。
暂无
暂无

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

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM