Google Apps脚本找不到方法createFile((class))

时间:2014-03-27 06:28:49

标签: file-upload google-apps-script

我刚刚开始编写我的第一个Google应用程序脚本,我似乎无法通过此错误。 我的脚本的作用是创建一个允许用户将数据输入谷歌电子表格的表单。 并将文件上传到文件夹BIO。 请帮帮我! 这是代码。

function doGet(e) {
  var app = UiApp.createApplication().setTitle('New app');
 var doc = SpreadsheetApp.openById('0AvOeZM3IzF-GdDRyV3NiTjBreC1ONXh0cHdDMlFhRGc');
  var grid = app.createGrid(5, 2);
  grid.setWidget(0, 0, app.createLabel('Code:'));
  grid.setWidget(0, 1, app.createTextBox().setName('codeName'));
  grid.setWidget(1, 0, app.createLabel('Uploaded Date'));
  grid.setWidget(1, 1, app.createTextBox().setName('date'));
  grid.setWidget(2, 0, app.createLabel("maid's Name"));
  grid.setWidget(2, 1, app.createTextBox().setName('maidName'));

//creates vertical panel and declare as panel
  var panel = app.createVerticalPanel();
  panel.add(grid);


  var form = app.createFormPanel().setId('frm').setEncoding('multipart/form-data');
   panel.add(app.createFileUpload().setName('thefile'));
  form.add(panel);

//creates submit button and declare as button
  var button = app.createButton('submit');

//notsure
  var handler = app.createServerHandler('b');

//notsure
  handler.addCallbackElement(grid);

//notsure
  button.addClickHandler(handler);

//add submit button to panel.
  panel.add(button);

//addpanel to application
  app.add(panel);

//display application


  app.add(form);

  return app;
}

function b(e) {

 var doc = SpreadsheetApp.openById('0AvOeZM3IzF-GdDRyV3NiTjBreC1ONXh0cHdDMlFhRGc');
 var lastRow = doc.getLastRow(); //Find the last row
 var cell = doc.getRange('a1').offset(lastRow, 0); //finds the next empty cell in column A

cell.setValue(e.parameter.codeName);//i can access paremeter codeName because i setname('codeName') just now.
cell.offset(0, 1).setValue(e.parameter.date);
cell.offset(0, 2).setValue(e.parameter.maidName);

  var Blob = e.parameter.thefile;
  var folder = DocsList.getFolder('BIO');
  folder.createFile(Blob);



var app = UiApp.getActiveApplication(); 
  var label = app.createLabel('File Upload Sucess')
  app.close(); //close widget
  return app; //close widget


}

提前谢谢你们! 碧玉。

1 个答案:

答案 0 :(得分:0)

要在表单中上传文件,您必须使用doGet()/ doPost()结构并使用submitButton而不是' normal'你正在使用的按钮。

请注意,此配置不需要添加callBackElement。

另请注意,表单必须只包含一个元素,并且fileUpload小部件必须是表单的子级。我改变了你的Ui创作的组织,使它(希望)更加清晰。

您的代码应如下(没有检查拼写错误):

function doGet() {
  var app = UiApp.createApplication().setTitle('New app');
  var doc = SpreadsheetApp.openById('0AnqSFd3iikE3dFBub2t1Ry1PaXJUMUVkSVVSempCenc');
  var form = app.createFormPanel();
  var panel = app.createVerticalPanel();
  var grid = app.createGrid(5, 2);
  grid.setWidget(0, 0, app.createLabel('Code:'));
  grid.setWidget(0, 1, app.createTextBox().setName('codeName'));
  grid.setWidget(1, 0, app.createLabel('Uploaded Date'));
  grid.setWidget(1, 1, app.createTextBox().setName('date'));
  grid.setWidget(2, 0, app.createLabel("maid's Name"));
  grid.setWidget(2, 1, app.createTextBox().setName('maidName'));
  //creates vertical panel and declare as panel
  panel.add(grid);
  panel.add(app.createFileUpload().setName('thefile'));
  form.add(panel);
  app.add(form);
  var button = app.createSubmitButton('submit');
  panel.add(button);
  return app;
}



function doPost(e) {
  var doc = SpreadsheetApp.openById('0AvOeZM3IzF-GdDRyV3NiTjBreC1ONXh0cHdDMlFhRGc');
  var lastRow = doc.getLastRow(); //Find the last row
  var cell = doc.getRange('a1').offset(lastRow, 0); //finds the next empty cell in column A
  cell.setValue(e.parameter.codeName);//i can access paremeter codeName because i setname('codeName') just now.
  cell.offset(0, 1).setValue(e.parameter.date);
  cell.offset(0, 2).setValue(e.parameter.maidName);
  var Blob = e.parameter.thefile;
  var folder = DocsList.getFolder('BIO');
  folder.createFile(Blob);
  var app = UiApp.getActiveApplication(); 
  var label = app.createLabel('File Upload Success')
  app.add(label);
  return app; //close widget
}

编辑后发表评论:

要获取刚刚上传的文件的网址,请更改以下代码:

  ...
  var folder = DocsList.getFolder('BIO');
  var url = folder.createFile(Blob).getUrl();
  Logger.log(url);
  ...