【问题标题】:WebApp Google Script to collect data and upload a file用于收集数据和上传文件的 WebApp Google 脚本
【发布时间】:2020-11-25 04:54:57
【问题描述】:

我从另一个用户那里得到了一些代码,但该代码正在上传文件,但没有将文本框字段数据和上传的文件链接附加到工作表。 我只想将文件上传和有关要在文本框中输入的文件的详细信息(当前代码用于名称和电子邮件-稍后可以修改以捕获其他数据)和文本框数据以及文件的链接上传到包含脚本的文件中。

form.html 如下:

<!-- Include the Google CSS package -->
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">

<!-- You can also include your own CSS styles -->
<style>
 form { margin: 40px auto; }
 input { display:inline-block; margin: 20px; }
 </style>

 <script>

 // The function will be called after the form is submitted
  function uploadFile() {
document.getElementById('uploadFileButton').value = "Uploading File..";
google.script.run
   .withSuccessHandler(fileUploaded)
   .uploadFiles(document.getElementById("labnol"));
return false;
}

// This function will be called after the Google Script has executed
 function fileUploaded(status) {
document.getElementById('labnol').style.display = 'none';
document.getElementById('output').innerHTML = status;
}

</script>

<!-- This is the HTML form -->
<form id="labnol">

 <!-- Text input fields -->
 <input type="text" id="nameField" name="myName" placeholder="Your name..">
 <input type="email" id="emailField" name="myEmail" placeholder="Your email..">

 <!-- File input filed -->
 <input type="file" name="myFile">

 <!-- The submit button. It calls the server side function uploadfiles() on click -->
 <input type="submit" id="uploadFileButton" value="Upload File"
     onclick="this.value='Uploading..';uploadFile();return false;">

</form>

<!-- Here the results of the form submission will be displayed -->
<div id="output"></div>

code.gs 如下:

/* The script is deployed as a web app and renders the form */
 function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('form.html')
        .setSandboxMode(HtmlService.SandboxMode.NATIVE);
    // This is important as file upload fail in IFRAME Sandbox mode.
   }

   /* This function will process the submitted form */
 function uploadFiles(form) {

 try {

/* Name of the Drive folder where the files should be saved */
var DMS = "Test Form Submissions";
var folder, folders = DriveApp.getFoldersByName(DMS);

/* Find the folder, create if the folder does not exist */
if (folders.hasNext()) {
  folder = folders.next();
} else {
  folder = DriveApp.createFolder(DMS);
} 

/* Get the file uploaded though the form as a blob */
var blob = form.myFile;
var file = folder.createFile(blob);

//Allocate variables for submissions in sheet
var namerecord = form.myName;
var emailrecord = form.myEmail;

/* Set the file description as the name of the uploader */
file.setDescription("Uploaded by " + form.myName);

/* Return the download URL of the file once its on Google Drive */
return "File uploaded successfully " + file.getUrl();

var uploadURL = file.getUrl();

 var url = "https://docs.google.com/spreadsheets/d/1Kk8uvMHC506UVuhjzsvKka-RTp5OEjWV-yeoWxeV5b4/edit#gid=0";
 var ss = SpreadsheetApp.openByUrl(url);
 var ws = ss.getSheetByName("Inward");

 ws.appendRow([namerecord,emailrecord,uploadURL]);


 /*var googsheet = 



SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1N80542YrEAT6Llq6k2vzDdkSO938csVv28UtUCNlpFU/edit?usp=sharing');
  return "started";
  var sheet = googsheet.getSheetName('Inward');
  sheet.appendRow([namerecord,emailrecord,uploadURL]);
  return "completed";
*/
  //

  } catch (error) {

    /* If there's an error, show the error message */
    return error.toString();
  }

        //Open spreadsheet based on URL
    
}

请在这方面提供帮助。我是脚本和网络应用的新手。

【问题讨论】:

    标签: forms google-apps-script file-upload web-applications data-collection


    【解决方案1】:

    问题:

    函数doGet在上传文件之后和将数据附加到电子表格之前包含一个return语句:

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

    此语句结束此函数执行,因此脚本永远不会到达附加数据部分。

    解决办法:

    假设您有权访问此电子表格(变量 url)并且此电子表格包含名为 Inward 的工作表,只要 注释掉返回语句,您的数据就应该成功附加到电子表格/removed,或者,如果您希望页面返回 File uploaded successfully {fileUrl}移动到 try 块的末尾

    ws.appendRow([namerecord,emailrecord,uploadURL]);
    return "File uploaded successfully " + file.getUrl();
    

    【讨论】:

    • @Raghavender 不客气。请考虑accepting this answer,如果它帮助您解决了这个问题。
    • 又出现一个问题,上传pdf文件时,上传的文件都是空白页,请帮帮我。
    • @Raghavender 请考虑发布另一个问题以解决此问题。
    猜你喜欢
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多