[英]Google Sheets App Script Mysterious Error
I'm teaching a class and for my class I keep all of my student's marks on a google spreadsheet. 我正在教一门课程,对于我的课程,我将所有学生的成绩保存在Google电子表格中。 On my website I would like to present information to students on an individual basis.
我想在我的网站上向学生个人介绍信息。 I've created an app where it presents them with a password text box.
我创建了一个应用程序,其中向他们显示了密码文本框。 They type in their password and then it retrieves information from the spreadsheet that is unique to them and presents it to them in a label.
他们输入密码,然后从电子表格中检索唯一的信息,并以标签的形式显示给他们。 I've been trying to hack this all together, but it's just not working properly and I'm getting an error that I cannot diagnose.
我一直在尝试将所有内容全部破解,但是它无法正常工作,并且出现了我无法诊断的错误。 If I print out the information using Browser.msgBox() it outputs the info, but otherwise it generates an error.
如果我使用Browser.msgBox()打印出该信息,则会输出该信息,否则会生成错误。 Why is this happening and what is the fix?
为什么会发生这种情况,解决方法是什么? Here's the code:
这是代码:
var pointsSheet = SpreadsheetApp.openById('1o8_f063j1jYZjFEnI_P7uAztpnEAvQ6mc3Z1_Owa69Y');
//creates and shows an app with a label and password text box
function doGet() {
var app = UiApp.createApplication().setTitle('Incomplete Challenges');
var mygrid = app.createGrid(1, 2);
mygrid.setWidget(0, 0, app.createLabel('Password:'));
mygrid.setWidget(0, 1, app.createPasswordTextBox().setName("text"));
var mybutton = app.createButton('Submit');
var submitHandler = app.createServerClickHandler('getResults');
submitHandler.addCallbackElement(mygrid);
mybutton.addClickHandler(submitHandler);
var mypanel = app.createVerticalPanel();
mypanel.add(mygrid);
mypanel.add(mybutton);
app.add(mypanel);
SpreadsheetApp.getActiveSpreadsheet().show(app);
//return app; //UNCOMMENT WHEN DEPLOYING APP
}
//obtains data based on password entered by user and outputs their info
function getResults(eventInfo) {
var app = UiApp.createApplication().setTitle('Incomplete Challenges');
var password = eventInfo.parameter.text;
var passwordCheckRange = pointsSheet.getRange("B34:C34").getValues();
if (passwordCheckRange == null) {
Browser.msgBox("Error: Range is null");
return app;
}
var name;
for(var i = 0; i < passwordCheckRange.length; i++) {
if(passwordCheckRange[i][1] == password) {
name = passwordCheckRange[i][0];
break;
}
}
var studentRecordRange = pointsSheet.getRange("B3:AY29").getValues();
var headingRange = pointsSheet.getRange("B1:AY2").getValues();
if (studentRecordRange == null) {
Browser.msgBox("Error: Range is null");
return app;
}
var requestedRecord;
for(var i = 0; i < studentRecordRange.length; i++) {
if(studentRecordRange[i][0] == name)
requestedRecord = studentRecordRange[i];
}
var stringRecord = "";
for(var i = headingRange[1].length-1; i >= 7; i--) {
if (requestedRecord[i] == "")
stringRecord += headingRange[1][i] + ": " + headingRange[0][i] + "XP" + "\\n";
}
var mygrid = app.createGrid(2, 1);
mygrid.setWidget(0, 0, app.createLabel('INCOMPLETE CHALLENGES'));
mygrid.setWidget(1, 0, app.createLabel(stringRecord));
var mypanel = app.createVerticalPanel();
mypanel.add(mygrid);
app.add(mypanel);
//Browser.msgBox(stringRecord);
return app;
}
The error that I experience is: Error encountered: An unexpected error occurred. 我遇到的错误是: 遇到的错误:发生意外的错误。
As you can see it's very helpful. 如您所见,它非常有用。
Line 28 it should be getActiveApplication () and not createApplication() . 第28行应为getActiveApplication ()而不是createApplication() 。 You cant create an application on another application.
您不能在另一个应用程序上创建一个应用程序。 :)
:)
Also I think line 63 it should be "<br>";
我也认为第63行应该是
"<br>";
instead "\\n"; 改为“ \\ n”; along with line 68 it should be createHTML instead of createLabel
与第68行一起应该是createHTML而不是createLabel
I also think that you have apply few styling css so that your app looks good. 我还认为您已经应用了少量样式CSS,因此您的应用看起来不错。 check on .setStyleAttributes in UiApp.
检查UiApp中的.setStyleAttributes。
There are a few errors in this code, the first one -that generates the error you get - is (as mentioned in the other answer) the UiApp.createApplication()
in the handler function. 这段代码中有一些错误,第一个-生成您得到的错误-是(如其他答案所述
UiApp.createApplication()
处理程序函数中的UiApp.createApplication()
。
You can't create an UiApp instance in a handler function, you should instead get the active instance and eventually add elements to it (using UiApp.getActiveApplication()
). 您无法在处理函数中创建UiApp实例,而应该获取活动实例并最终向其中添加元素(使用
UiApp.getActiveApplication()
)。
You can't neither change the title of this instance. 您既不能更改该实例的标题。 Btw, it doesn't make sense since this title will not appear as a "title" when you will be deploying this app as a webapp.
顺便说一句,这没有意义,因为当您将该应用程序部署为Webapp时,该标题不会显示为“ title”。 It will simply show up at the top of your browser window (as a page title) as your app will occupy the whole screen and not a modal popup anymore.
它会简单地显示在浏览器窗口的顶部(作为页面标题),因为您的应用将占据整个屏幕,而不再是模式弹出窗口。 So if you want a title to appear in your Ui, simply add it as an HTML widget where you can choose the font size and weight (and any other CSS styles).
因此,如果您希望标题出现在Ui中,只需将其添加为HTML小部件,即可在其中选择字体大小和粗细(以及其他任何CSS样式)。
The other error is in the password check, you are using Browser.msgBox("Error: Range is null");
另一个错误是密码检查,您正在使用
Browser.msgBox("Error: Range is null");
but Browser class won't work in UiApp. 但浏览器类无法在UiApp中使用。 You should only use UiApp elements, not spreadSheetApp elements.
您应仅使用UiApp元素,而不应使用spreadSheetApp元素。
And, as a more general comment, I suggest you test your app directly using the .dev url (last saved version) of the app (after saving a beta version and having deployed it) so that you are in the "real" use condition and have a pertinent pov on the result. 并且,作为更一般的评论,我建议您使用应用程序的.dev URL(最后保存的版本)直接测试您的应用程序(保存测试版并部署之后),以使您处于“实际”使用状态并对结果进行适当的评估。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.