【问题标题】:I'd like to have a Google Docs script automatically load?我想让 Google Docs 脚本自动加载?
【发布时间】:2018-11-15 03:59:52
【问题描述】:

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

【问题讨论】:

    标签: google-apps-script google-docs


    【解决方案1】:

    你可以使用这样的对话框:

    代码:

    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>
    

    我可能会将它做成一个网络应用程序并将其放入书签中以便于访问。 所以你只需要添加这个:

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

    并部署它。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多