0

I'm trying to create a script that has a datepicker on the right side. The code below will allow the user to create a file named by the date when he click a certain date in the date picker.. My problem is how do I open/show the file in the browser?

This is my google script code....

          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);

          }

This is my .html code...

              <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>
user3820909
  • 3
  • 2
  • 7
  • Is this script bound to a spreadsheet, document, etc? Is the script stand alone as it's own application? Do you want the document that just got created to appear in a different tab as a web page? Or you just want to open the document? The user could click a link to open the document. You would need to dynamically configure the link with the new file name. – Alan Wells Sep 25 '14 at 02:29
  • You can also see this StackOverflow question: [Stack Overflow Open a doc from Google Apps Script](http://stackoverflow.com/questions/16587781/is-there-a-way-to-open-a-new-document-from-a-google-docs-app-script?rq=1) – Alan Wells Sep 25 '14 at 02:31

1 Answers1

0

As stated elsewhere (Stack Overflow Open a doc from Google Apps Script), Apps Script does not allow script to automatically navigate to other URLs or open other files.

You can, however, use a script to acquire the URL for the file and present it to the user as a link so they can open it:

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
Community
  • 1
  • 1
Ryan Roth
  • 1,394
  • 9
  • 15