Javascript 没有FormPanel的Google应用程序脚本FileUpload()?

Javascript 没有FormPanel的Google应用程序脚本FileUpload()?,javascript,file-upload,google-apps-script,Javascript,File Upload,Google Apps Script,我正在构建我的第一个应用程序,使用谷歌应用程序脚本。我需要有一个表单,允许上传一个文件到谷歌文档列表。我目前使用的代码基于以下内容: 但是,我希望为Submit按钮指定自己的单击处理程序,如果可能的话,不要等待帖子。当我尝试将上面的代码更改为类似的内容时,对e.parameter.thefile的引用为null,并且不包含文件blob function doGet(e) { var app = UiApp.createApplication().setTitle("Upload CSV t

我正在构建我的第一个应用程序,使用谷歌应用程序脚本。我需要有一个表单,允许上传一个文件到谷歌文档列表。我目前使用的代码基于以下内容:

但是,我希望为Submit按钮指定自己的单击处理程序,如果可能的话,不要等待帖子。当我尝试将上面的代码更改为类似的内容时,对e.parameter.thefile的引用为null,并且不包含文件blob

function doGet(e) {

  var app = UiApp.createApplication().setTitle("Upload CSV to Sheet");
  var formContent = app.createVerticalPanel(); 
  var submitServerHandler = app.createServerClickHandler('submitHandler_');
  formContent.add(app.createFileUpload().setName('thefile'));
  submitServerHandler.addCallbackElement(formContent);  
  formContent.add(app.createButton('Submit').addClickHandler(submitServerHandler));  
  app.add(formContent);
  return app;
}

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

是否可以在没有FormPanel的情况下使用FileUpload控件?如果是,怎么做?谢谢

必须使用doPost才能进行文件上载。您可以向“提交”按钮添加更多的单击处理程序。如果您想在UI上获得快速响应,请使用客户端处理程序。表单面板上任何命名的内容都将在e.parameter中传递,您可以像在普通html表单中一样使用隐藏项


你想做什么?那会帮我给你一个更好的答案

必须使用doPost才能进行文件上载。您可以向“提交”按钮添加更多的单击处理程序。如果您想在UI上获得快速响应,请使用客户端处理程序。表单面板上任何命名的内容都将在e.parameter中传递,您可以像在普通html表单中一样使用隐藏项

function doGet(e) { 
  var app = UiApp.createApplication(); 
  var panel = app.createVerticalPanel().setId('panel');
  var fileUpload = app.createFileUpload().setName('theFile').setId('theFile');
  var handler = app.createServerChangeHandler('uploadfile');
  handler.addCallbackElement(panel);
  fileUpload.addChangeHandler(handler);
  panel.add(fileUpload);
  app.add(panel);
  return app;
} 

function uploadfile(e) 
{ 
// data returned which can be used to create a blob
// assuming mime-type to be a zip file in this example
  var fileBlob = Utilities.newBlob(e.parameter.thefile, "application/zip","myZippedFile.zip" );
  var doc = DocsList.createFile(fileBlob);
  var app = UiApp.getActiveApplication();
  app.getElementById('panel').add(app.createLabel('File Uploaded successfully'));
  return app;
}
你想做什么?那会帮我给你一个更好的答案

function doGet(e) { 
  var app = UiApp.createApplication(); 
  var panel = app.createVerticalPanel().setId('panel');
  var fileUpload = app.createFileUpload().setName('theFile').setId('theFile');
  var handler = app.createServerChangeHandler('uploadfile');
  handler.addCallbackElement(panel);
  fileUpload.addChangeHandler(handler);
  panel.add(fileUpload);
  app.add(panel);
  return app;
} 

function uploadfile(e) 
{ 
// data returned which can be used to create a blob
// assuming mime-type to be a zip file in this example
  var fileBlob = Utilities.newBlob(e.parameter.thefile, "application/zip","myZippedFile.zip" );
  var doc = DocsList.createFile(fileBlob);
  var app = UiApp.getActiveApplication();
  app.getElementById('panel').add(app.createLabel('File Uploaded successfully'));
  return app;
}


上传文件的另一种方法是使用createDocsListDialog()。这只允许从用户的Google驱动器中选择文件,但另一方面,您可以对这些文件进行大量处理(包括在不使用FormPanel的情况下使用它们)


(请参阅谷歌的文档。)

上传文件的另一种方法是使用createDocsListDialog()。这只允许从用户的Google驱动器中选择文件,但另一方面,您可以对这些文件进行大量处理(包括在不使用FormPanel的情况下使用它们)


(请参阅谷歌的文档。)

我在文件上传方面做了更多的工作,并编写了一些帮助函数,创建了一个“伪小部件”,可以与其他UI元素相同的方式使用

我已经将它添加到了的帮助函数集合中,以防它也可以帮助其他人

如果您从上面的站点在gash.gs中添加代码,您可以像这样管理文件上载:

function doGet() {
  var app = UiApp.createApplication();

  // We must have a handler set up before creating the file upload.
  var handler = app.createServerHandler('handlerCallback');
  app.add(gash.createFileUpload('myFile', handler));

  app.add(app.createButton('Do something with the file', handler));

  return app;
}

function handlerCallback(eventInfo) {
  // The ID of the file in Google Drive is stored in the parameters.
  var file = DocsList.getFileById(eventInfo.parameter.myFile);
  // The file was uploaded to the trash. Don't forget to un-trash it.
  file.setTrashed(false);

  // Do whatever you want with the file.
  var app = UiApp.getActiveApplication();
  app.add(app.createLabel('Your file size in bytes: ' + file.getSize()));
  return app;
}

后台仍然有一个FormPanel,但这样我就不必去碰它了。

我在文件上传方面做了更多的工作,我编写了一些帮助函数,创建了一个“伪小部件”,可以与其他UI元素一样使用

我已经将它添加到了的帮助函数集合中,以防它也可以帮助其他人

如果您从上面的站点在gash.gs中添加代码,您可以像这样管理文件上载:

function doGet() {
  var app = UiApp.createApplication();

  // We must have a handler set up before creating the file upload.
  var handler = app.createServerHandler('handlerCallback');
  app.add(gash.createFileUpload('myFile', handler));

  app.add(app.createButton('Do something with the file', handler));

  return app;
}

function handlerCallback(eventInfo) {
  // The ID of the file in Google Drive is stored in the parameters.
  var file = DocsList.getFileById(eventInfo.parameter.myFile);
  // The file was uploaded to the trash. Don't forget to un-trash it.
  file.setTrashed(false);

  // Do whatever you want with the file.
  var app = UiApp.getActiveApplication();
  app.add(app.createLabel('Your file size in bytes: ' + file.getSize()));
  return app;
}

背景中仍然有一个FormPanel,但这样我就不必触摸它了。

+1感谢您的回答。我的目标是将文件上传作为更大的数据输入表单的一部分-我希望当用户单击“上传”按钮时能够上传文件,而不提交表单上的其余数据。可能这在最初回答后有所改变-上传可以从
doGet()
进行。谢谢你的回答。我的目标是将文件上传作为更大的数据输入表单的一部分-我希望当用户单击“上传”按钮时能够上传文件,而不提交表单上的其余数据。可能这在最初回答后有所改变-上传可以从
doGet()
进行。(请参阅。)我尝试过这个,它确实创建了一个文件,但该文件是空的,零字节。文件的e.参数返回未定义的。我认为这个代码不起作用。请证明我错了,因为我想找到一种方法来做这件事。有一个输入错误(“thefile”而不是“thefile”),但创建的文件仍然是空的。我的结论是你必须使用FormPanel来上传文件。我已经尝试过了,它确实创建了一个文件,但是文件是空的,零字节。文件的e.参数返回未定义的。我认为这个代码不起作用。请证明我错了,因为我想找到一种方法来做这件事。有一个输入错误(“thefile”而不是“thefile”),但创建的文件仍然是空的。我的结论是,你必须使用FormPanel上传文件才能正常工作。问题明确地说“…这允许将文件上传到Google文档列表”。打开驱动器中已经存在的文件不是主题,因此您的答案无关紧要。很抱歉告诉你,没有人身攻击;-)这个问题特别提到“…允许将文件上传到谷歌文档列表”。打开驱动器中已经存在的文件不是主题,因此您的答案无关紧要。很抱歉告诉你,没有人身攻击;-)事实上,您提供的代码没有多大意义……您的助手函数必须包含在库中才能像您一样使用。那么,为什么不公开您的库并显示其密钥呢?或者至少解释一下,在使用它之前必须先这么做?你能澄清一下吗?还有:使用doGet/doPost经典结构有什么让人讨厌的?我使用它没有问题…:-)该库可以在我在评论中添加的链接、GPL许可证和所有内容中找到。(这个例子是为了说明它是如何被使用的,而不是解释它是如何工作的。)它不是一个共享GAS的库,但如果它足够成熟的话,它可能是。至少对我来说,doGet/doPost的一个恼人之处是,我必须以不同于其他输入(我可以使用标准处理程序来管理)的方式管理文件上传。此外,这个问题的标题涉及不使用FormPanel的文件上载。(我觉得有点被践踏了。我只是想分享我想出的解决方案。)不要觉得被践踏了。。。抱歉,如果我的评论令人困惑,那不是我的本意;-)我看了你的链接b