Uploading file using Google Apps Script using HtmlService

前端 未结 3 1026
长情又很酷
长情又很酷 2020-11-30 08:29

How can I upload files to google drive? I want to create a web app using google app script - htmlservice. I don\'t know how to point form in html to existing google app scr

相关标签:
3条回答
  • 2020-11-30 08:36

    try: return HtmlService.createTemplateFromFile('myPage').evaluate(); More: html service reference

    0 讨论(0)
  • 2020-11-30 08:36

    I found an answer for my question.

    Submit a Form using Google App Script's HtmlService

    The code in the Google App Script link below is:

    function doGet(e) {
      var template = HtmlService.createTemplateFromFile('Form.html');
      template.action = ScriptApp.getService().getUrl();
      return template.evaluate();
    }
    
    function doPost(e) {
      var template = HtmlService.createTemplateFromFile('Thanks.html');
      template.name = e.parameter.name;
      template.comment = e.parameter.comment;
      template.screenshot = e.parameter.screenshot;
      return template.evaluate();
    }
    

    https://script.google.com/d/1i65oG_ymE1lreHtB6WBGaPHi3oLD_-wPd5Ter1nsN7maFAWgUA9DbE4C/edit

    Thanks!

    0 讨论(0)
  • 2020-11-30 08:52

    Have the button run the server side function using google.script.run, passing in the entire form as the only parameter. (Inside the button's onClick, 'this' is the button, so 'this.parentNode' is the form.) Make sure to give the file input a name.

    <html>
    <body>
    <form>
       <input type="file" name="theFile">
       <input type="hidden" name="anExample">
       <input type="button" onclick="google.script.run.serverFunc(this.parentNode)">
    </form>
    </body>
    </html>
    

    On the server, have your form handling function take one parameter - the form itself. The HTML form from the client code will be transformed into an equivalent JavaScript object where all named fields are string properties, except for files which will be blobs.

    function doGet() {
      return HtmlService.createHtmlOutputFromFile('myPage');
    }
    
    function serverFunc(theForm) {
       var anExampleText = theForm.anExample;  // This is a string
       var fileBlob = theForm.theFile;         // This is a Blob.
       var adoc = DocsList.createFile(fileBlob);    
       return adoc.getUrl();
    }
    

    If you actually want to use that URL you are generating and returning, be sure to add a success handler to the google.script call. You can modify it like this:

    // Defined somewhere before the form
    function handler(url) {
      // Do something with the url.
    }
    
    <input type="button" onclick=
      "google.script.run.withSuccessHandler(handler).serverFunc(this.parentNode)">
    
    0 讨论(0)
提交回复
热议问题