使用发送电子邮件地址上传文件的表单

时间:2015-03-29 20:09:08

标签: google-apps-script

我使用此脚本创建了一个上传文件的表单:

JS:

var dropBoxId = "XXXXXXXXXXX"; // Drive ID of 'dropbox' folder
var logSheetId = "XXXXXXXXXXXX"; // Drive ID of log spreadsheet

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

function uploadFiles(formObject) {
  try {
    // Create a file in Drive from the one provided in the form
    var folder = DriveApp.getFolderById(dropBoxId);
    var blob = formObject.myFile;    
    var file = folder.createFile(blob);    
    file.setDescription("Uploaded by " + formObject.myName);

    // Open the log and record the new file name, URL and name from form
    var ss = SpreadsheetApp.openById(logSheetId);
    var sheet = ss.getSheets()[0];
    sheet.appendRow([file.getName(), file.getUrl(), formObject.myName]);

    // Return the new file Drive URL so it can be put in the web app output
    return file.getUrl();
  } catch (error) {
    return error.toString();
  }
}

HTML:

<form id="myForm">
  <input type="text" name="myName" placeholder="Your full name..."/>
  <input name="myFile" type="file" />
  <input type="button" value="Submit"
      onclick="google.script.run
          .withSuccessHandler(updateUrl)
          .withFailureHandler(onFailure)
          .uploadFiles(this.parentNode)" />
</form>

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

<script>
    function updateUrl(url) {
        var div = document.getElementById('output');
        div.innerHTML = '<a href="' + url + '">Got it!</a>';
    }
    function onFailure(error) {
        alert(error.message);
    }
</script>

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

我希望我的用户也可以通过表单发送他们的电子邮件地址。我该如何更改代码?

1 个答案:

答案 0 :(得分:0)

如果您希望用户在表单中提供他们的电子邮件,请添加以下字段:

<input type="text" name="myEmail" placeholder="Your Email..."/>

在脚本中获取formObject的电子邮件值:

var userEmail = formObject.myEmail;

HTML:

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

JS:

function uploadFiles(formObject) {
  try {
    // Create a file in Drive from the one provided in the form
    var folder = DriveApp.getFolderById(dropBoxId);
    var blob = formObject.myFile;

    var userEmail = formObject.myEmail;

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