如何将Google Script表单数据导出到Google云端硬盘?

时间:2016-07-15 17:49:42

标签: javascript html google-apps-script webforms google-apps

我在谷歌脚本中构建了一个非常简单的表单。文件上传功能正常运行:它将文件上传到我的谷歌硬盘。我无法弄清楚如何获取表单信息(名称,组织,内容类型),将其保存到电子表格中,然后将其上传到我的Google驱动器。

如何将表单数据(文本字段)上传到我的驱动器?

!!更新7月19日!! 使用电子表格应用程序代码更新代码。和改进的HTML。 CSS不包括在内,因为我认为它与此问题无关。

!!更新7/20 !! 代码正在运作。已更新以反映完整功能。感谢桑迪的帮助。

form.html

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat:400">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">

<div id="main">
 <div class="form">
  <form id='myForm'>
      <div class="header">
        <h1>Information Submission Form</h1>
        <h2>Center For Alternative Fuels</h2>
      </div>

      <div class="row">
       <div class="col-md-6">
         <input id='name' type="text" name="name" placeholder="Full name" class="full item" spellcheck="false">
      </div>

      <div class="col-md-6">
         <input id='email' type="text" name="email" placeholder="Email" class="full item" spellcheck="false">
      </div>
     </div>

     <div class="row indent item">
      <input id='organization' type="text" name="organization" placeholder="Organization" class="full" spellcheck="false">
     </div>

     <div class="row indent item">
       <textarea id='type' name="type" class="full" placeholder="What are you submitting? (Presentation, Educational Content,...)" rows='2'></textarea>
     </div>

     <div id='success'>
     </div>


     <div class="row indent item">
       <textarea id='info' name="info" class="full" placeholder="Describe the content you are submitting" rows='8'></textarea>
     </div>


     <input type="file" name="myFile">
     <input type="submit" value="Submit" 
       onclick="this.value='Uploading..';
            google.script.run.withSuccessHandler(fileUploaded)
            .uploadFiles(this.parentNode);
            return false;" style="margin-top: 20px">
  </form>
 </div>
</div>

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

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

server.gs

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('form.html');
}

var url;

function uploadToSheet(form){

 var fullName, emailAdd, organization, fileType, fileInfo;

 var ss = SpreadsheetApp.openById("INSERT SHEET ID HERE");

 var dataArray = [];

 fullName = form.name;
 emailAdd = form.email;
 organization = form.organization;
 fileType = form.type;
 fileInfo = form.info;

 dataArray.push(fullName);
 dataArray.push(emailAdd);
 dataArray.push(organization);
 dataArray.push(fileType);
 dataArray.push(fileInfo);

 ss.appendRow('dataArray');

}

function uploadFiles(form) {

try {

  var dropbox = "Form Files";
  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);
  url=file.getUrl();

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

catch (error) {
  return error.toString();
  }
}

1 个答案:

答案 0 :(得分:0)

代码可能如下所示:

var dataArray,org,ss,userName;

ss = SpreadsheetApp.openById("spreadsheet_File_ID");
dataArray = [];

userName = form.myName;
org = form.myOrganization;

dataArray.push(userName);
dataArray.push(org);

ss.appendRow(dataArray);