WebApp Google 收集数据和上传文件的脚本
WebApp Google Script to collect data and upload a file
我从这个 fourm 的另一个用户那里得到了一些代码,但是代码正在上传文件,但没有附加文本框提交的数据和上传的文件 link 到 sheet。
我只想上传文件和有关要在文本框中输入的文件的详细信息(当前代码用于名称和电子邮件 - 稍后可以修改以捕获其他数据)和文本框数据以及 link要附加到包含脚本的文件中的文件。
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下面是一个s:
/* 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
}
这方面请帮忙。我是脚本和 Web 应用程序的新手。
问题:
函数 doGet
包含一个 return 语句在上传文件之后和将数据附加到传播之前sheet:
return "File uploaded successfully " + file.getUrl();
这条语句结束了这个函数的执行,所以脚本永远不会到达追加数据部分。
解决方案:
假设您可以访问此价差sheet(变量 url
)并且此价差sheet 包含一个名为 Inward
的 sheet,您的只要 return 语句被注释 out/removed,数据就应该成功附加到传播 sheet,或者,如果您希望页面 return File uploaded successfully {fileUrl}
, 移动到你的 try
块的末尾:
ws.appendRow([namerecord,emailrecord,uploadURL]);
return "File uploaded successfully " + file.getUrl();
我从这个 fourm 的另一个用户那里得到了一些代码,但是代码正在上传文件,但没有附加文本框提交的数据和上传的文件 link 到 sheet。 我只想上传文件和有关要在文本框中输入的文件的详细信息(当前代码用于名称和电子邮件 - 稍后可以修改以捕获其他数据)和文本框数据以及 link要附加到包含脚本的文件中的文件。
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下面是一个s:
/* 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
}
这方面请帮忙。我是脚本和 Web 应用程序的新手。
问题:
函数 doGet
包含一个 return 语句在上传文件之后和将数据附加到传播之前sheet:
return "File uploaded successfully " + file.getUrl();
这条语句结束了这个函数的执行,所以脚本永远不会到达追加数据部分。
解决方案:
假设您可以访问此价差sheet(变量 url
)并且此价差sheet 包含一个名为 Inward
的 sheet,您的只要 return 语句被注释 out/removed,数据就应该成功附加到传播 sheet,或者,如果您希望页面 return File uploaded successfully {fileUrl}
, 移动到你的 try
块的末尾:
ws.appendRow([namerecord,emailrecord,uploadURL]);
return "File uploaded successfully " + file.getUrl();