我刚开始使用 gApps 脚本,在尝试设置我的 UI 以在处理任务时禁用提交按钮时,我的代码(如下)遇到了两个问题。
尝试加载脚本时,我偶尔会收到以下错误:“遇到错误:发生意外错误”。这个错误是不一致的,因为我需要做的就是刷新脚本,然后它会为我加载。当我根本没有更改代码时,它有时有效,有时无效,这真的让我很困扰。
当最后第三行
//button_submit.setEnabled(true);
未注释时,每次我尝试加载脚本时都会发生错误。
在试图找出问题所在时,我注意到 getElementById() 似乎没有为提交按钮分配正确的 UI 类型。我不确定这是否是问题的原因,但我怀疑这与我在测试脚本中看到的相同行为不同,我为查看是否可以从更简单的基础复制问题。
如果您对导致此问题的原因提供任何反馈,我将不胜感激。
谢谢
克里斯·BP
Logging output
populate_assign_list spinner.getId():spinner
populate_assign_list spinner.getType():Image
populate_assign_list button_submit.getId():button_submit
populate_assign_list button_submit.getType():Generic
doGet button_submit.getId():spinner
doGet button_submit.getType():Image
doGet button_submit.getId():button_submit
doGet button_submit.getType():SubmitButton
.
//define global script property variables
var prop_home_folder_id; //String - Contains the home folder's ID string
var prop_subject_folder_names; //Array of Strings - Listing of folder names
var prop_subject_folder_ids; //Array of Strings - Listing of folder IDs associated with folder names
var prop_submisison_ss_ids; //Array of Strings - Losting of sumission spreadsheet IDs
var prop_allow_anon_submit; //Flag - Allow manual entry of email address (ie. non domain access) Default to FALSE
function save_script_prop_(home_folder_id, subject_folder_names, subject_folder_ids, submisison_ss_ids, allow_anon_submit) {
//array values must be sent to this function as comma separated strings ie <array>.join(',')
if (home_folder_id != null) ScriptProperties.setProperty("home_folder_id", home_folder_id);
if (subject_folder_names != null) ScriptProperties.setProperty("subject_folder_names", subject_folder_names);
if (subject_folder_ids != null) ScriptProperties.setProperty("subject_folder_ids", subject_folder_ids);
if (submisison_ss_ids != null) ScriptProperties.setProperty("submisison_ss_ids", submisison_ss_ids);
if (allow_anon_submit != null) ScriptProperties.setProperty("allow_anon_submit", allow_anon_submit);
}
function load_script_prop_() {
/*
prop_home_folder_id = ScriptProperties.getProperty("home_folder_id");
prop_subject_folder_names = ScriptProperties.getProperty("subject_folder_names").split(",");
prop_subject_folder_ids = ScriptProperties.getProperty("subject_folder_ids").split(",");
prop_submisison_ss_ids = ScriptProperties.getProperty("submisison_ss_ids").split(",");
prop_allow_anon_submit = ScriptProperties.getProperty("allow_anon_submit");
//*/
prop_home_folder_id = "";
prop_subject_folder_names = ["1","2","3"];
prop_subject_folder_ids = ["1","2","3"];
prop_submisison_ss_ids = ["a","b","c"];
prop_allow_anon_submit = false;
}
function doPost(e) {
var app = UiApp.getActiveApplication();
app.add(app.createLabel("Form submitted"));
return app;
}
function doGet() {
load_script_prop_();
var testing_message = "";
//create ui app and panels
var app = UiApp.createApplication().setTitle("Assignment Submission");
var v_panel = app.createVerticalPanel();
v_panel.setSpacing(20);
var layout_grid = app.createGrid(5,2);
var form_panel = app.createFormPanel();
var tab_panel_upload = app.createTabPanel().setId("tab_panel_upload");
var v_panel_upload_file = app.createVerticalPanel().setTag(0); //Tag refrenced the number of upload elements currently in the tab
var v_panel_upload_gdoc = app.createVerticalPanel().setTag(0); //Tag refrenced the number of gdoc elements currently in the tab
//create menu objects
var spinner = app.createImage("http://www.worldmsday.org/1000-faces/images/whatami/spinner.gif").setVisible(false).setId("spinner");
var datebox_submission = app.createDateBox().setId("submission_datebox").setValue(new Date()).setEnabled(false);
var textbox_user = app.createTextBox().setId("textbox_user").setName("textbox_user").setValue(Session.getUser().getEmail()).setEnabled(false);
var lb_subject_folder = app.createListBox().setId("lb_subject_folder").setName("lb_subject_folder");
lb_subject_folder.setVisibleItemCount(1);
for (var i in prop_subject_folder_names) lb_subject_folder.addItem(prop_subject_folder_names[i], i);
lb_subject_folder.setSelectedIndex(0);
var lb_assign_list = app.createListBox().setId("lb_assign_list").setName("lb_assign_list");
lb_assign_list.setVisibleItemCount(1);
populate_assign_list();
lb_assign_list.setSelectedIndex(0);
var button_submit = app.createSubmitButton("Submit").setId("button_submit");
Logger.log("doGet button_submit.getId():"+spinner.getId());
Logger.log("doGet button_submit.getType():"+spinner.getType());
Logger.log("doGet button_submit.getId():"+button_submit.getId());
Logger.log("doGet button_submit.getType():"+button_submit.getType());
//button_submit.setEnabled(false);
//button_submit.setEnabled(true);
var upload_1 = app.createFileUpload().setName("upload_1");
var gdoc_1 = app.createLabel("Not Yet Implemented");
//create menu handlers
var handler_lb_subject_folder_spinner = app.createClientHandler().forTargets(spinner).setVisible(true);
handler_lb_subject_folder_spinner.forTargets([lb_assign_list, tab_panel_upload]).setVisible(false);
handler_lb_subject_folder_spinner.forTargets(button_submit).setEnabled(false);
var handler_lb_subject_folder = app.createServerHandler("populate_assign_list").addCallbackElement(v_panel);
lb_subject_folder.addChangeHandler(handler_lb_subject_folder_spinner);
lb_subject_folder.addChangeHandler(handler_lb_subject_folder);
//need to add an onchange handler for subject folder listbox which updates the assignment selection listbox.
//add ui objects to ui
layout_grid.setText(0,0, "Date: ");
layout_grid.setWidget(0, 1, datebox_submission);
layout_grid.setText(1,0, "Name: ");
//layout_grid.setWidget(1, 1, "");
layout_grid.setText(2,0, "Email Address: ");
layout_grid.setWidget(2, 1, textbox_user);
layout_grid.setText(3,0, "Course: ");
layout_grid.setWidget(3, 1, lb_subject_folder);
layout_grid.setText(4,0, "Assignment: ");
layout_grid.setWidget(4, 1, lb_assign_list);
v_panel.add(layout_grid);
v_panel.add(spinner);
v_panel_upload_file.add(upload_1);
tab_panel_upload.add(v_panel_upload_file, "Upload File");
v_panel_upload_file.add(gdoc_1);
tab_panel_upload.add(v_panel_upload_gdoc, "Share gDoc");
tab_panel_upload.selectTab(0);
v_panel.add(tab_panel_upload);
v_panel.add(button_submit);
form_panel.add(v_panel);
app.add(form_panel);
var testing_label = app.createLabel("No Test Output").setStyleAttribute("textAlign", "center");
if (testing_message != "") debug_label.setText(testing_label);
app.add(testing_label);
return(app);
}
function populate_assign_list(e) {
load_script_prop_();
if (e == null) subject_id=0;
else subject_id = e.parameter.lb_subject_folder;
var app = UiApp.getActiveApplication();
var lb_assign_list = app.getElementById("lb_assign_list");
lb_assign_list.clear();
var assign_list = ["x","y","z" ];
for (var i = 1; i < assign_list.length; i++) {
lb_assign_list.addItem(assign_list[i] );
}
lb_assign_list.setVisible(true);
var spinner = app.getElementById("spinner").setVisible(false);
Logger.log("populate_assign_list spinner.getId():" + spinner.getId());
Logger.log("populate_assign_list spinner.getType():" + spinner.getType());
app.getElementById("tab_panel_upload").setVisible(true);
var button_submit = app.getElementById("button_submit");
Logger.log("populate_assign_list button_submit.getId():"+button_submit.getId());
Logger.log("populate_assign_list button_submit.getType():"+button_submit.getType());
//button_submit.setEnabled(true);
return app;
}