HTML表单不会使用doPost提交到电子表格

时间:2016-03-19 05:09:40

标签: javascript html google-apps-script

尝试在现有线程中找到一些有用的答案,但没有什么能真正与我的问题相匹配。我想有一个快速修复它,我只是看不到它。

我有一个HTML表单,想要将文件上传到我的google驱动器(效果很好)并将文本字段保存到我的电子表格中(根本不起作用)。

我只是找不到这两个功能之间的任何重大区别,令人沮丧!

在我的代码下面,这里也是[我的公共脚本的链接] [1]。

形式:

<!DOCTYPE html>

<form id="myForm">
    <input type="text" name="myName" placeholder="Your name..">
    <input type="file" name="myFile">
<input type="submit" value="Upload File" 
       onclick="this.value='Uploading..';
                google.script.run.withSuccessHandler(fileUploaded)
                .uploadFiles(this.parentNode);
                return false;
                google.script.run.withSuccessHandler(fileUploaded)
                .doPost(this.parentNode);">
</form>

<div id="output"></div>

<script>
    function fileUploaded(status) {
        document.getElementById('myForm').style.display = 'none';
        document.getElementById('output').innerHTML = status;
    }
</script>

<style>
 input { display:block; margin: 20px; }
</style>



  [1]: https://script.google.com/d/1K9jGl7ALHCZ93rz8GoeV8t7PE_7vgbNZlLJed1h6ZWyuoon11gIbik24/edit?usp=sharing

server.gs:

function doGet(e) {
  var html = HtmlService.createTemplateFromFile("form");
  html = html.evaluate();
  html.setSandboxMode(HtmlService.SandboxMode.IFRAME);
  return html;  
}

function uploadFiles(form) {

  try {

    var dropbox = "Customer Shapes";
    var folder, folders = DriveApp.getFoldersByName(dropbox);

    if (folders.hasNext()) {
      folder = folders.next();
    } else {
      folder = DriveApp.createFolder(dropbox);
    }

    var blob = form.myFile;    
    var file = folder.createFile(blob);    
    file.setDescription("Uploaded by " + form.myName);

    return "File uploaded successfully " + file.getUrl();

  } catch (error) {

    return error.toString();
  }
}

function doPost(form) { // change to doPost(e) if you are recieving POST data
  html.setSandboxMode(HtmlService.SandboxMode.EMULATED);
  var name = form.myName;
  var url="url...";
  var message = 'Ram';
  var submitSSKey = '...kHI';
  var sheet = SpreadsheetApp.openById(submitSSKey).getActiveSheet();
  var lastRow = sheet.getLastRow();
  var targetRange = sheet.getRange(lastRow+1, 1, 2, 2).setValues([[name,url]]);
}

感谢您的帮助!

马丁

1 个答案:

答案 0 :(得分:0)

如果您要使用google.script.run,那么拥有一个名为doPost()的函数毫无意义。拥有提交类型输入标记也是没有意义的:type="submit"

如果要运行withSuccessHandler(fileUploaded)来更改显示,则表单内不能有按钮。这将导致显示屏变为空白。

您不需要两次google.script.run来电。您需要进行多项更改和改进:

form.html

<form id="myForm">
  <input type="text" name="myName" placeholder="Your name..">
  <input type="file" name="myFile">
</form>

  <input type="button" value="Upload File" 
       onclick="this.value='Uploading..';
                var theForm = document.getElementById('myForm');
                google.script.run.withSuccessHandler(fileUploaded)
                .processFormData(theForm);">

<div id="output"></div>

<script>
    function fileUploaded(status) {
        document.getElementById('myForm').style.display = 'none';
        document.getElementById('output').innerHTML = status;
    }
</script>

<style>
 input { display:block; margin: 20px; }
</style>

server.gs

function processFormData(form) {
  uploadFiles(form.myFile);//Call first function to process the file

  var innerArray = [];
  var outerArray = [];

  innerArray.push(form.myName);
  innerArray.push(form.myFile);

  outerArray.push(innerArray);//Call second function to write data to SS
  writeDataToSS(outerArray);
};

function uploadFiles(theFile) {

  try {

    var dropbox = "Customer Shapes";
    var folder, folders = DriveApp.getFoldersByName(dropbox);

    if (folders.hasNext()) {
      folder = folders.next();
    } else {
      folder = DriveApp.createFolder(dropbox);
    }

    var blob = theFile;    
    var file = folder.createFile(blob);    
    file.setDescription("Uploaded by " + theFile);

    return "File uploaded successfully " + file.getUrl();

  } catch (error) {

    return error.toString();
  }
}

function writeDataToSS(values) {
  //html.setSandboxMode(HtmlService.SandboxMode.EMULATED);
  var url="url...";
  var message = 'Ram';
  var submitSSKey = '...kHI';
  var sheet = SpreadsheetApp.openById(submitSSKey).getActiveSheet();
  var lastRow = sheet.getLastRow();
  var targetRange = sheet.getRange(lastRow+1, 1, 1, 2).setValues(values);
}

html文件不是模板,它没有scriptlet。只需使用createHtmlOutputFromFile()

function doGet(e) {
  var html = HtmlService.createHtmlOutputFromFile("form");
  html.setSandboxMode(HtmlService.SandboxMode.IFRAME);
  return html;  
}