Google Apps脚本找不到方法createFile((class)) [英] Google Apps Script Cannot find method createFile((class))
问题描述
我刚刚开始编写第一个Google App脚本,但似乎无法通过此错误. 我的脚本所做的是,它创建了一个表格,允许用户将数据输入到Google电子表格中. 并将文件上传到文件夹BIO中. 请帮帮我! 这是代码.
I just started scripting my first Google App and i can't seem to get pass this error. What my script does is that it creates a form that allows users to enter data into a google spreadsheet. And upload a file into the folder BIO. Please help me out! This is the code.
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
}
谢谢大家!
碧玉.
Thank you guys in advance!
Jasper.
推荐答案
要以表单形式上传文件,您必须使用doGet()/doPost()结构并使用SubmitButton而不是"normal"按钮您正在使用.
In order to upload a file in a form you have to use a doGet() / doPost() structure and use a submitButton instead of the 'normal' button you are using.
请注意,此配置不需要添加callBackElement.
Note that this configuration does not require to add a callBackElement.
还请注意,该表单必须仅包含一个元素,并且fileUpload小部件必须是该表单的子级.我对您的Ui创作的组织进行了一些更改,以使其(希望如此)更加清晰.
Note also that the form must contain only one element and that the fileUpload widget must be a child of the form. I changed a bit the organization of your Ui creation to make it (hopefully) more clear.
您的代码应如下所示(不检查拼写错误):
You code should be as follows (didn't check for typos) :
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
}
编辑您的评论:
EDIT following your comment :
要获取刚刚上传的文件的网址,请更改代码,如下所示:
to get the url of the file you just uploaded, change the code like this :
...
var folder = DocsList.getFolder('BIO');
var url = folder.createFile(Blob).getUrl();
Logger.log(url);
...
这篇关于Google Apps脚本找不到方法createFile((class))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!