我有这个脚本工作,但几周前它突然停止了 - 我以为我做了一些编程错误,但我已经无法调试这几周了。
我想要一个google网站上的脚本,允许某人上传文件(以及其他一些信息,如名称),并让脚本将其附加到新的文件柜页面,然后使用新页面更新列表页面&# 39;信息。
我使用旧论坛帖子的部分代码,更新我需要的地方。如果有人知道更简单的方法,我不反对重写。
谢谢!
编辑:问题似乎是在doPost(e)函数中 - 在点击提交后,没有任何反应,小工具变为空白,我收到错误消息:&#34;发生意外错误。&#34; < / p>function doGet(e){
return getUi(e);
}
function getUi(e) {
//create user interface for uploading information
var app = UiApp.createApplication().setTitle('File Upload');
var form = app.createFormPanel().setId('form').setEncoding('multipart/form-data');
//create grid to store information
var formContent = app.createGrid().resize(6,2);
formContent.setText(0,0,'File Name: ');
var fileName = app.createTextBox().setName('fileName').setId('fileName');
formContent.setWidget(0,1,fileName);
var fileUpload = app.createFileUpload().setName('fileUpload').setId('fileUpload');
formContent.setWidget(1,1,fileUpload);
formContent.setWidget(2,0,app.createLabel('Short Description: '));
var fileDescription = app.createTextBox().setName('fileDescription').setId('fileDescription');
formContent.setWidget(2,1,fileDescription);
formContent.setText(3,0,'Level: ');
var level = app.createListBox().setName('level').setId('level').addItem('Introductory').addItem('Intermediate').addItem('Advanced');
formContent.setWidget(3,1,level);
formContent.setText(4,0,'Subject: ');
var subject = app.createListBox().setName('subject').setId('subject').addItem('Fundamentals').addItem('Observing').addItem('Solar System').addItem('Stars').addItem('Stellar Evolution').addItem('Milky Way').addItem('Galaxies').addItem('Cosmology')
formContent.setWidget(4,1,subject);
var submitButton = app.createSubmitButton("Submit").setId("submitButton");
formContent.setWidget(5,1,submitButton);
//var handler = app.createServerHandler("pauseUi");
//submitButton.addClickHandler(handler);
form.add(formContent);
app.add(form);
var resultTextArea = app.createTextArea().setName("resultArea").setId("resultArea");
resultTextArea.setVisibleLines(5);
resultTextArea.setSize("80%","80%");
app.add(resultTextArea);
var ogga = 'good god';
Logger.log(ogga);
return app;
}
function pauseUi(e){
var app = UiApp.getActiveApplication();
app.getElementById("file").setEnabled(false);
app.getElementById("fileName").setEnabled(false);
app.getElementById("fileDescription").setEnabled(false);
app.getElementById("submitButton").setEnabled(false);
app.getElementById("level").setEnabled(false);
app.getElementById("subject").setEnabled(false);
app.getElementById("resultArea").setEnabled(false);
app.getElementById("resultArea").setText('Please wait...');
return app;
}
function resetUi(app){
app.getElementById("file").setEnabled(true);
app.getElementById("fileName").setEnabled(true);
app.getElementById("fileDescription").setEnabled(true);
app.getElementById("submitButton").setEnabled(true);
app.getElementById("level").setEnabled(true);
app.getElementById("subject").setEnabled(true);
app.getElementById("resultArea").setEnabled(true);
}
function doPost(e){
var app = UiApp.getActiveApplication();
Logger.log(e.parameter);
//create new page for uploaded file
var msg = createNewPage(e.parameter.fileName,e.parameter.fileUpload,e.parameter.fileDescription,e.parameter.level,e.parameter.subject);
//reset upload page
resetUi(app);
//notify user of success or failure
app.getElementById("resultArea").setText(msg.join('\n'));
return app;
}
function createNewPage(fileName,uploadedFile,fileDescription,level,subject){
//site information
var mysite = SitesApp.getSite("uw.edu","introductory-astronomy-clearinghouse");
var labsPage = mysite.getChildByName("labs-exercises");
Logger.log('url for lab page is: '+labsPage.getUrl());
var msg = [];
//create new page
var page = findPage(fileName);
if (page == null){
var page = labsPage.createFileCabinetPage(fileName, normalizeName(fileName), '');
}
try{
//add file to file cabinet
var attachment = page.addHostedAttachment(uploadedFile);
//see if already listed on list
var listItem = getFileListItem(fileName);
if (listItem == null) {
var fileUrl = page.getUrl();
var newValues = [repairUrlValue(fileUrl, fileName), fileDescription, level, subject];
labsPage.addListItem(newValues);
}
msg.push('Refresh page to see your item added or edit your file at: '+fileUrl);
} catch(e){
msg.push('Failed to upload the '+ fileName +' attachment, possibly due to size constraints or time out issues.');
}
return msg;
}
function getFileListItem(fileName){
var mysite = SitesApp.getSite("uw.edu","introductory-astronomy-clearinghouse");
var labPage = mysite.getChildByName("labs-exercises");
var i = getListItemIndex(labPage,fileName,0);
var items = labPage.getListItems();
Logger.log('labPage list item'+items[i].getValueByIndex(0));
return (i>-1)? labPage.getListItems()[i]:null;
}
//get the index of your item
function getListItemIndex(listPage,propOneValue,propOneIndex){
//for column propOneIndex, returns index (row) that equals propOneValue, or -1.
var listItems = listPage.getListItems();
Logger.log('listItems '+listItems.length);
var r = -1;
for (var i = 0;i<listItems.length && r<0;i++){
if ((listItems[i].getValueByIndex(propOneIndex) == propOneValue)){r = i;}
}
return r;
}
//repairs URL
function repairUrlValue(url,text){
Logger.log('buildAnchor for'+text+'->'+url);
var a = '<a href="'+url+'">"'+text+'</a>';
Logger.log('...built '+a);
return a;
}
//find name of page with normalized name
function findPage(name){
var mysite = SitesApp.getSite("uw.edu","introductory-astronomy-clearinghouse");
var parentPage = mysite.getChildByName("labs-exercises");
var norm = normalizeName(name);
var page = parentPage.getChildByName(norm);
Logger.log('findPage '+page.getUrl());
return page;
}
//get size of attached file in KB
function getBlobSize(blob) {
return Math.round(blob.getBytes().length/1024)+" KB";
}
//put the name into reasonable form
function normalizeName(header){
var key = "";
for (var i = 0; i < header.length; ++i){
var letter = header[i];
if (letter == " " && key.length > 0){
key +='-';
continue;
}
if (!isAlnum(letter)){
continue;
}
if (key.length == 0 && isDigit(letter)){
continue; //first character must be a letter
}
key += letter.toLowerCase();
}
Logger.log("normalizeName from '"+header+"' to '"+key+"'");
return key;
}
/*
i s A l n u m
Returns true if the character char is alphabetical, false otherwise
*/
function isAlnum(char){
return char >= 'A' && char <= 'Z' ||
char >='a' && char <= 'z' ||
isDigit(char);
}
/* i s D i g i t
Returns true if the character is a digit, false otherwise.
*/
function isDigit(char){
return char >='0' && char <= '9';
}
答案 0 :(得分:1)
当您尝试按ID无效的ID访问UI元素时,通常会发生意外错误。
在您的情况下,没有诸如
之类的小部件app.getElementById("file").setEnabled(false);