是否可以在处理函数中清除 DateBox 的值?您可以简单地清除 TextBox 的值,并且可以通过包含然后将 ListBox 设置为该项目,将 ListBox的值设置app.getElementById('id').setValue('');
为空白值,但我一直无法找到清除 DateBox 的方法. 目标是创建一个简单的表单,允许用户填写并使用按钮提交表单,然后自动重置表单,以便用户可以再次填写以进行新的提交。listBox.addItem('')
app.getElementById('id').setItemSelected({item # for blank item}, true);
或者,如果我怀疑还不能清除 DateBox(功能限制),那么我可以简单地使用 TextBox。为了使事情看起来更好(并帮助用户以脚本期望的格式输入日期),如果我可以将 DatePicker 添加到 TextBox 但除了旧教程之外,我发现它会从头开始生成整个 DatePicker 函数我还没有找到将新的 DatePicker GAS 类附加到文本框的简单方法。
GAS 文档:https ://developers.google.com/apps-script/reference/ui/date-box
日期选择器 2 教程 - https://sites.google.com/site/appsscripttutorial/miscellaneous/date-picker-2
非常简化的示例代码有望提供足够的信息,而不会强迫任何人阅读 5 页代码来了解我想要强调的内容。submitHandler(e)
请特别注意函数中的注释。可以将此代码复制到空白(已保存)电子表格中进行测试,或者如果您更愿意这样做,您可以替换电子表格特定代码以仅返回应用程序。
//Opens the active spreadsheet and selects the first sheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = sheets[0];
function doGet() {
var app = UiApp.createApplication().setTitle('Test Form');
//Main panel with a 3x2 grid hosting labels and boxes.
var mainVP = app.createVerticalPanel();
var mainCP = app.createCaptionPanel('Purchase Details');
var mainG = app.createGrid(3, 2);
var nameL = app.createLabel('Name');
var nameT = app.createTextBox().setName('name').setId('name');
var dateL = app.createLabel('Date');
var dateD = app.createDateBox().setName('date').setId('date');
var ownL = app.createLabel('Own/Lease');
var ownC = app.createListBox().setName('ownLease').setId('ownLease');
//Submit button with auto-reset functionality.
var subHP = app.createHorizontalPanel().setSize('100%', '40px');
var subL = app.createLabel('Submitted').setId('subL').setVisible(false);
var subB = app.createButton('Submit');
//Handler for button.
var subHandler = app.createServerHandler('submitHandler');
subHandler.addCallbackElement(mainVP);
subB.addClickHandler(subHandler);
//Add options to ListBox.
ownC.addItem('').addItem('Own').addItem('Lease');
//Place widgets on Grid.
mainG.setWidget(0, 0, nameL).setWidget(0, 1, nameT)
.setWidget(1, 0, dateL).setWidget(1, 1, dateD)
.setWidget(2, 0, ownL).setWidget(2, 1, ownC);
//Aligns the submit Button and Label to the right and adds them to their HPanel.
subHP
.add(subL).setCellHorizontalAlignment(subL, UiApp.HorizontalAlignment.RIGHT)
.add(subB).setCellHorizontalAlignment(subB, UiApp.HorizontalAlignment.RIGHT);
//Adds stuff to panels so it shows up.
mainCP.add(mainG);
mainVP.add(mainCP);
mainVP.add(subHP);
app.add(mainVP);
var doc = SpreadsheetApp.getActive();
doc.show(app);
}
function submitHandler(e) {
var app = UiApp.getActiveApplication();
//Set variables for each field in the Purchase Details CaptionPanel.
var name = e.parameter.name;
var date = e.parameter.date;
var ownLease = e.parameter.ownLease;
//I use a sheet.appendRow() method here but for the purposes of this question-
//we don't actually need the data, just need to clear it.
//Clears data from form to prepare for another submission.
app.getElementById('name').setValue('');
//app.getElementById('date') /* This is the field I need to clear somehow. */
app.getElementById('ownLease').setItemSelected(0, true);
//Displays the word "Submitted" when the Submit Button is clicked.
var label = app.getElementById('subL');
label.setVisible(true).setStyleAttribute('color', 'blue');
return app;
}
总之:
- 有没有办法使用处理函数清除 DateBox 中的值,如果是这样,如何?
- 如果清除 DateBox 是不可能的,我如何将 DatePicker 添加到 TextBox 并将数据输入限制为特定格式(因此 TextBox 不会将“blahblahblah”作为有效日期)?