如何在谷歌脚本中点击后自动显示谷歌文档?

时间:2014-09-23 16:54:41

标签: google-apps-script

我正在尝试创建一个右侧有一个日期选择器的脚本。下面的代码将允许用户创建一个文件,该文件以日期选择器中的某个日期点击日期命名。我的问题是如何在浏览器中打开/显示该文件?

这是我的google脚本代码....

          function onOpen(e) {
            DocumentApp.getUi().createAddonMenu()
            .addItem('Start', 'showSidebar')
               .addToUi();

              }



            function creatediary(x) {



           //var doc = DocumentApp.create(x);
            //var retrievedID = DocumentApp.getID(doc);
           //var doc = DocumentApp.openById(doc);

        }

           function listFiles(z) {
          var files = DriveApp.getFiles();
           var doc = DocumentApp.create(z);
        while ( files.hasNext() ) {
            var file = files.next();
           if(file.getName() === z){
            var retrievedID = file.getId();
            var retrievedurl = file.getUrl();
          //var doc = DocumentApp.create(retrievedurl);
           //var doc = DocumentApp.create(retrievedID);
         var doc = DocumentApp.openById(retrievedID); 
          //showURL('Open document named "'+z+'"',retrievedurl);


         }
         ;

            }

         } 


     function onInstall(e) {
         onOpen(e);
        }


        function showSidebar() {

        var ui = HtmlService.createHtmlOutputFromFile('Sidebar')
           .setTitle('Diary');
         DocumentApp.getUi().showSidebar(ui);

          }

这是我的.html代码......

              <html lang="en">
             <head>


             </head>
             <style>
             div.ui-datepicker{
             font-size:10px;
                }

             </style>



               <div>
               <meta charset="utf-8">
                <title>jQuery UI Datepicker - Default functionality</title>
                <link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
                <script src="//code.jquery.com/jquery-1.10.2.js"></script>
                <script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
                <link rel="stylesheet" href="/resources/demos/style.css">

                </div>
                <script>
               $(function() {
                 $("#datepicker").datepicker(
                        {
                            onSelect: function()
                             { 

                         var dateObject = $(this).datepicker('getDate'); 
                         document.getElementById("demo").innerHTML = dateObject;
                       //var doc = DocumentApp.create("sample");
                      //DriveApp.createFile(dateObject, 'Diary');

                     google.script.run.creatediary(document.getElementById("demo").innerHTML);
                     google.script.run.listFiles(document.getElementById("demo").innerHTML);



                         }
                     });
                      });






                      </script>

                      <body>
                     <p id="demo"></p>
                        <p>Date: <input type="text" id="datepicker"></p>

                        <iframe src="https://www.google.com/calendar/embed?                               
                      src=gttvlnnqitf8atif2rreplcdl0%40group.calendar.google.com&ctz=Asia/Manila"      style="border: 0"   width="800" height="600" frameborder="0" scrolling="no"></iframe>





                     </body>

                         </html>

1 个答案:

答案 0 :(得分:0)

如其他地方所述(Stack Overflow Open a doc from Google Apps Script),Apps脚本不允许脚本自动导航到其他URL或打开其他文件。

但是,您可以使用脚本获取文件的URL并将其作为链接呈现给用户,以便他们可以打开它:

var doc = DocumentApp.create(fileName);
var url = doc.getUrl();
// Give the url to the user via a dialog, link in a doc, link in an email or
//  another method