Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用html服务使用google apps脚本上载文件_Javascript_Google Apps Script - Fatal编程技术网

Javascript 使用html服务使用google apps脚本上载文件

Javascript 使用html服务使用google apps脚本上载文件,javascript,google-apps-script,Javascript,Google Apps Script,我想用GAS的HtmlService上传一个文件,但它不起作用。 我在这个主题中找到了一些信息,但不符合我的问题。 下面是一个描述我的问题的示例: Code.gs: function doGet() { return HtmlService.createTemplateFromFile("index").evaluate(); } //For some reasons, this function has to be in code.gs funci

我想用GAS的HtmlService上传一个文件,但它不起作用。 我在这个主题中找到了一些信息,但不符合我的问题。 下面是一个描述我的问题的示例:

Code.gs:

    function doGet() {

    return HtmlService.createTemplateFromFile("index").evaluate();

    }

    //For some reasons, this function has to be in code.gs
    funcion getHTML() {

        var html = "<form id='myform'>"+
                     "<input name='myfile'>"+
                   "</form>"+
                   "<div data-formname='myform'> Submit form </div>";

    return html;

    }

    function uploadFile(file) {

      DriveApp.createFile(file); *//Doesn't work :/*

    }
    $(document).on("click","div", function(e) {

    var idform = $(this).data("formname");

    if (idform) 
    sendForm(idform);


    });

function trigger() {

google.script.run.withsuccesshandler(setHTML).getHTML();

}

function setHTML(html) {

$("#myHTML").html(html);

}

function sendForm(idForm) {

var formElements = document.getElementById("idForm").elements;
var form = {};

for (var key in formElements) form[key] = formElements[key];

google.script.run.withsuccesshandler().uploadFile(form["myfile"]);

}
index.html

<body onload="trigger()">

<div id='myHTML'></div>

</body>

您的代码似乎有点过于复杂。。。试试这个简单的代码

代码.gs

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

function serverFunc(theForm) {
   var fileBlob = theForm.theFile;         // This is a Blob.
   var adoc = DocsList.createFile(fileBlob);    
   return adoc.getUrl();
}
index.html

<div>
<script>
function cliHandler(e){
  document.getElementById('button').value = "Document is downloadable at url "+e  
  }
</script> 

<form>
   <input type="file" name="theFile">
   <input type="button"  value="UpLoad" id="button" onclick="google.script.run.withSuccessHandler(cliHandler).serverFunc(this.parentNode)">
</form>
</div>

函数cliHandler(e){
document.getElementById('button').value=“可以在url+e下载文档”
}

以下示例可能很有用: