【问题标题】:How do I export Google Script form data to Google Drive?如何将 Google Script 表单数据导出到 Google Drive?
【发布时间】:2016-07-15 17:49:42
【问题描述】:

我在谷歌脚本中构建了一个非常简单的表单。文件上传功能很实用:它将文件上传到我的谷歌驱动器。我不知道如何获取表单信息(名称、组织、内容类型),将其保存到电子表格中,然后将其上传到我的谷歌驱动器。

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

!!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();
  }
}

【问题讨论】:

  • 可以将表单数据(文本字段)放入文本文件,并将文本文件保存到您的 Google Drive。或者可以将每组表单数据保存到电子表格的一行中。或者您是否希望将数据附加到上传的文件并作为元数据保存?
  • 我希望将数据保存在电子表格中...我以为我在原始帖子中包含了该详细信息 :) 谢谢!
  • 您需要使用SpreadsheetApp 服务。我不认为doPost() 函数被触发了,你也不需要它。您应该检查使用Logger.log() 语句传递的数据。 Logger.log('the name: ' + form.myName) 你可能想要使用 appendRow() Apps Script documentation
  • 谢谢桑迪。我会试一试。

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


【解决方案1】:

代码可能如下所示:

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

【讨论】:

  • 查看我更新的代码。我收到“TypeError:无法调用 null 的方法“appendRow”。”
  • 电子表格对象为空。您正在尝试获取活动电子表格,但它可能未打开。您可能需要按 ID 打开电子表格。
  • 关于我应该使用什么函数来打开电子表格的任何见解?
  • 看看这个文档:Link to Apps Script Documentation
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 2019-05-22
  • 2014-09-08
  • 1970-01-01
相关资源
最近更新 更多