简体   繁体   English

Google Apps脚本创建带有文件上传的表单

[英]Google Apps Script Create form with file upload

I'm in the process of creating a basic form that takes user input via text boxes and drop down list and upon submit will log into a spreadsheet. 我正在创建一个基本表单,该表单通过文本框和下拉列表接受用户输入,提交后将登录到电子表格。 So far this works fine. 到目前为止,这很好。 What I would like to add to the form is a "file upload" feature, that allows someone to select the file and upon submitting the form the file is uploaded as the data values from text boxes and drop downs are logged into the spreadsheet. 我想添加到表单中的是“文件上传”功能,该功能允许某人选择文件,并在提交表单后将文件上传,因为来自文本框和下拉列表的数据值被记录到电子表格中。 I've reviewed the following link https://developers.google.com/apps-script/class_fileupload but i'm having a hard time inserting / adding the example into the existing doGet function.. Anyone able to help or provide suggestions? 我已经查看了以下链接https://developers.google.com/apps-script/class_fileupload,但是我很难在现有的doGet函数中插入/添加示例。有人能提供帮助或建议吗? Here is the Google App Script code so far. 这是到目前为止的Google App脚本代码。

* used the following link to base the form on: https://sites.google.com/site/appsscripttutorial/advanced-examples/insert-data-in-sheet-using-ui-forms * 使用以下链接将表单作为基础: https : //sites.google.com/site/appsscripttutorial/advanced-examples/insert-data-in-sheet-using-ui-forms

// Script-as-app template.
 var submissionSSKey = 'Spreadsheet Key goes Here';

 function doGet() {
   var app = UiApp.createApplication().setTitle('Loan Registration Processing');
   var panel = app.createVerticalPanel();
   var grid = app.createGrid(8,2).setId('loanGrid');
   var loanTypeLabel = app.createLabel('Loan Type');
   var loanList = app.createListBox().setName('Loan List').setWidth('120px').setName('LoanType');
   loanList.addItem('Select Option');    
   loanList.addItem('FHA');
  loanList.addItem('Convential');  
  loanList.addItem('VA');
  loanList.addItem('Reverse');
  loanList.addItem('HELOC');
   var borrowerNameLabel = app.createLabel("Borrower's Name");
   var borrowerTextbox = app.createTextBox().setWidth('150px').setName('borrower');
   var loanAmountLabel = app.createLabel('Loan Amount');
   var loanAmountTextbox = app.createTextBox().setWidth('150px').setName('amount');
   var appDateLabel = app.createLabel('Loan Date');
   var appDateTextbox = app.createTextBox().setWidth('150px').setName('date');
   var lienPostition = app.createLabel('Lien Position');
   var lienPos = app.createListBox().setName('Lien Position').setWidth('150px').setName('LienPosition');
  lienPos.addItem('Select Option');     
  lienPos.addItem('1st');
  lienPos.addItem('2nd');
   var propertyType = app.createLabel('Property Type');
   var propType = app.createListBox().setName('Property Type').setWidth('150px').setName('PropertyType');
  propType.addItem('Select Option');
  propType.addItem('1-4');
  propType.addItem('Manufactured');
   var submitButton = app.createButton('Submit'); 

   //Grid layout of items on form
   grid.setWidget(0, 0, loanTypeLabel)
  .setWidget(0, 1, loanList)
  .setWidget(1, 0, borrowerNameLabel)
  .setWidget(1, 1, borrowerTextbox)
  .setWidget(2, 0, loanAmountLabel)
  .setWidget(2, 1, loanAmountTextbox)
  .setWidget(3, 0, appDateLabel)
  .setWidget(3, 1, appDateTextbox)
  .setWidget(4, 0, lienPostition)
  .setWidget(4, 1, lienPos)
  .setWidget(5, 0, propertyType)
  .setWidget(5, 1, propType)
  .setWidget(6, 0, submitButton)

   //Event Handler
   var handler = app.createServerClickHandler('insertInSS');
   handler.addCallbackElement(panel);
   submitButton.addClickHandler(handler);  
   panel.add(grid);
   app.add(panel);
   return app;

 }
 //Function to insert data in the sheet on clicking the submit button
 function insertInSS(e){
   var app = UiApp.getActiveApplication();
   var LoanType = e.parameter.LoanType;
   var borrower = e.parameter.borrower;
   var amount = e.parameter.amount;
   var date = e.parameter.date;
   var LienPosition = e.parameter.LienPosition;
   var PropertyType = e.parameter.PropertyType;
   //app.getElementById('info').setVisible(true).setStyleAttribute('color','red');

   var sheet = SpreadsheetApp.openById(submissionSSKey).getActiveSheet();
   var lastRow = sheet.getLastRow();
   var targetRange = sheet.getRange(lastRow+1, 1, 1, 6).setValues([[LoanType,borrower,amount,date,LienPosition,PropertyType]]);
   return app;
 }

here is the code properly formatted : 这是正确格式化的代码:

// Script-as-app template.
var submissionSSKey = '*************';

function doGet(e) {
  var app = UiApp.createApplication().setTitle('Loan Registration Processing');
  var panel = app.createFormPanel();
  var grid = app.createGrid(8,2).setId('loanGrid');
  var loanTypeLabel = app.createLabel('Loan Type');
  var loanList = app.createListBox().setName('Loan List').setWidth('120px').setName('LoanType');
      loanList.addItem('Select Option');    
      loanList.addItem('FHA');
      loanList.addItem('Convential');  
      loanList.addItem('VA');
      loanList.addItem('Reverse');
      loanList.addItem('HELOC');
  var borrowerNameLabel = app.createLabel("Borrower's Name");
  var borrowerTextbox = app.createTextBox().setWidth('150px').setName('borrower');
  var loanAmountLabel = app.createLabel('Loan Amount');
  var loanAmountTextbox = app.createTextBox().setWidth('150px').setName('amount');
  var appDateLabel = app.createLabel('Loan Date');
  var appDateTextbox = app.createDateBox().setWidth('150px').setName('date');
  var lienPostition = app.createLabel('Lien Position');
  var lienPos = app.createListBox().setName('Lien Position').setWidth('150px').setName('LienPosition');
      lienPos.addItem('Select Option');     
      lienPos.addItem('1st');
      lienPos.addItem('2nd');
  var propertyType = app.createLabel('Property Type');
  var propType = app.createListBox().setName('Property Type').setWidth('150px').setName('PropertyType');
      propType.addItem('Select Option');
      propType.addItem('1-4');
      propType.addItem('Manufactured');
  var submitButton = app.createSubmitButton('<B>Submit</B>'); 
  var warning = app.createHTML('<B>PLEASE WAIT WHILE DATA IS UPLOADING<B>').setStyleAttribute('background','yellow').setVisible(false)
  //file upload
  var upLoadTypeLabel = app.createLabel('File Upload');
  var upLoad = (app.createFileUpload().setName('thefile'));

  //Grid layout of items on form
  grid.setWidget(0, 0, loanTypeLabel)
      .setWidget(0, 1, loanList)
      .setWidget(1, 0, borrowerNameLabel)
      .setWidget(1, 1, borrowerTextbox)
      .setWidget(2, 0, loanAmountLabel)
      .setWidget(2, 1, loanAmountTextbox)
      .setWidget(3, 0, appDateLabel)
      .setWidget(3, 1, appDateTextbox)
      .setWidget(4, 0, lienPostition)
      .setWidget(4, 1, lienPos)
      .setWidget(5, 0, propertyType)
      .setWidget(5, 1, propType)
      .setWidget(6, 0, upLoadTypeLabel)
      .setWidget(6, 1, upLoad)
      .setWidget(7, 0, submitButton)
      .setWidget(7, 1, warning)

  var cliHandler = app.createClientHandler().forTargets(warning).setVisible(true)
  submitButton.addClickHandler(cliHandler);  
  panel.add(grid);
  app.add(panel);
  return app;

}

 function doPost(e) {
  var app = UiApp.getActiveApplication();
  var LoanType = e.parameter.LoanType;
  var borrower = e.parameter.borrower;
  var amount = e.parameter.amount;
  var date = e.parameter.date;
  var LienPosition = e.parameter.LienPosition;
  var PropertyType = e.parameter.PropertyType;
  //app.getElementById('info').setVisible(true).setStyleAttribute('color','red');

  var sheet = SpreadsheetApp.openById(submissionSSKey).getActiveSheet();
  var lastRow = sheet.getLastRow();
  var targetRange = sheet.getRange(lastRow+1, 1, 1, 6).setValues([[LoanType,borrower,amount,date,LienPosition,PropertyType]]);
   // data returned is a blob for FileUpload widget
   var fileBlob = e.parameter.thefile;
   var doc = DocsList.createFile(fileBlob);
   return app
 }

You have to put you panel into a formPanel, use a submitButton and get all your results in a single doPost function. 您必须将面板放入formPanel中,使用SubmitButton并通过单个doPost函数获得所有结果。 No need to define a handler nor any callbackElement in this context so you should remove them to avoid conflicts. 无需在此上下文中定义处理程序或任何callbackElement,因此应删除它们以避免冲突。

If you look at the simple example you showed as reference you'll see the structure that has to be adopted for form submission. 如果查看作为参考显示的简单示例,您将看到表单提交必须采用的结构。 I know it works the way you did it too but not for file upload... 我知道它的工作方式与您一样,但不适用于文件上传...

Here is you working code : I made a few changes... testable here 这是您的工作代码:我做了一些更改... 可在此处测试

  1. changed the textBox for date into a dateBox 将日期的文本框更改为dateBox
  2. added an client handler to show a warning while the file is uploading since this can take a while if the file is big ;-) 添加了一个客户端处理程序以在文件上传时显示警告,因为如果文件很大,这可能需要一段时间;-)

Sorry, the system is buggy, I cannot format the code properly, I'll make another answer... 抱歉,系统有错误,我无法正确设置代码格式,我将给出另一个答案...

I've figured out how to get the file upload to attach to the grid and display, allow file selection and submittal of data values to the spreadsheet. 我已经弄清楚了如何使文件上传附加到网格并显示,允许选择文件以及将数据值提交到电子表格。 It doesn't upload the select file to my drive. 它不会将选择的文件上传到我的驱动器。

I'm posting the updated code below. 我在下面发布更新的代码。 Any thoughts on why its not posting the file to my drive?? 关于为什么不将文件发布到驱动器的任何想法?

// Script-as-app template.
var submissionSSKey = 'Spreadsheet Key';

function doGet(e) {
  var app = UiApp.createApp ication().setTitle('Loan Registration Processing');
  var panel = app.createVerticalPanel();
  var grid = app.createGrid(8,2).setId('loanGrid');
  var loanTypeLabel = app.createLabel('Loan Type');
  var loanList = app.createListBox().setName('Loan List').setWidth('120px').setName('LoanType');
      loanList.addItem('Select Option');    
      loanList.addItem('FHA');
      loanList.addItem('Convential');  
      loanList.addItem('VA');
      loanList.addItem('Reverse');
      loanList.addItem('HELOC');
  var borrowerNameLabel = app.createLabel("Borrower's Name");
  var borrowerTextbox = app.createTextBox().setWidth('150px').setName('borrower');
  var loanAmountLabel = app.createLabel('Loan Amount');
  var loanAmountTextbox = app.createTextBox().setWidth('150px').setName('amount');
  var appDateLabel = app.createLabel('Loan Date');
  var appDateTextbox = app.createTextBox().setWidth('150px').setName('date');
  var lienPostition = app.createLabel('Lien Position');
  var lienPos = app.createListBox().setName('Lien Position').setWidth('150px').setName('LienPosition');
      lienPos.addItem('Select Option');     
      lienPos.addItem('1st');
      lienPos.addItem('2nd');
  var propertyType = app.createLabel('Property Type');
  var propType = app.createListBox().setName('Property Type').setWidth('150px').setName('PropertyType');
      propType.addItem('Select Option');
      propType.addItem('1-4');
      propType.addItem('Manufactured');
  var submitButton = app.createButton('Submit'); 

  //file upload
  var upLoadTypeLabel = app.createLabel('File Upload');
  var upLoad = (app.createFileUpload().setName('thefile'));

  //Grid layout of items on form
  grid.setWidget(0, 0, loanTypeLabel)
      .setWidget(0, 1, loanList)
      .setWidget(1, 0, borrowerNameLabel)
      .setWidget(1, 1, borrowerTextbox)
      .setWidget(2, 0, loanAmountLabel)
      .setWidget(2, 1, loanAmountTextbox)
      .setWidget(3, 0, appDateLabel)
      .setWidget(3, 1, appDateTextbox)
      .setWidget(4, 0, lienPostition)
      .setWidget(4, 1, lienPos)
      .setWidget(5, 0, propertyType)
      .setWidget(5, 1, propType)
      .setWidget(6, 0, upLoadTypeLabel)
      .setWidget(6, 1, upLoad)
      .setWidget(7, 0, submitButton)

  //Event Handler
  var handler = app.createServerClickHandler('insertInSS');
  handler.addCallbackElement(panel);
  submitButton.addClickHandler(handler);  
  panel.add(grid);
  app.add(panel);
  return app;

}
//Function to insert data in the sheet on clicking the submit button
function insertInSS(e){
  var app = UiApp.getActiveApplication();
  var LoanType = e.parameter.LoanType;
  var borrower = e.parameter.borrower;
  var amount = e.parameter.amount;
  var date = e.parameter.date;
  var LienPosition = e.parameter.LienPosition;
  var PropertyType = e.parameter.PropertyType;
  //app.getElementById('info').setVisible(true).setStyleAttribute('color','red');

  var sheet = SpreadsheetApp.openById(submissionSSKey).getActiveSheet();
  var lastRow = sheet.getLastRow();
  var targetRange = sheet.getRange(lastRow+1, 1, 1, 6).setValues([[LoanType,borrower,amount,date,LienPosition,PropertyType]]);
  return app;
}

 function doPost(e) {
   // data returned is a blob for FileUpload widget
   var fileBlob = e.parameter.thefile;
   var doc = DocsList.createFile(fileBlob);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Google Apps 脚本将文件上传到我的 google 驱动器(在 GOOGLE 中没有表格) - Upload file to my google drive with Google Apps Script (NO FORM IN GOOGLE) Google Apps脚本:文件上传进度指示器 - Google Apps Script: file upload progress indicator 如果未指定文件上传,则Google Apps脚本中将发生错误 - error in google apps script if no file upload is specified HTTP POST和Google Apps脚本文件上传 - HTTP POST and Google Apps Script file upload Google Apps脚本文件上传(通过HTML表单)仅在脚本被“绑定”时才有效(测试独立脚本时,HTTP 403错误) - Google Apps Script file upload (by HTML form) only works when script is “bound” (when standalone script tested, HTTP 403 error) 在Google Apps脚本中,如何按字节创建文件? - in Google Apps Script, how to create a file by bytes? 使用 Google 应用脚本在 Google 表格中上传 PDF 文件 - Upload PDF file in Google sheet using Google apps script 使用Google Apps脚本将文本文件上传到Google云端硬盘 - Upload text file to Google Drive with Google Apps Script 使用html服务使用google apps脚本上传文件 - Upload file with google apps script using html service 根据Google Apps脚本中的答案创建表单问题 - Create form questions according to answer in Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM