簡體   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-2024 STACKOOM.COM