清除DateBox或将DatePicker附加到TextBox - Google Script

时间:2013-09-27 14:34:36

标签: google-apps-script reset clear datebox

是否可以在处理函数中清除DateBox的值?您可以使用简单的app.getElementById('id').setValue('');清除TextBox的值,并且可以通过包含listBox.addItem('')然后将ListBox设置为{{1}的该项来将ListBox的值设置为空值但我一直无法找到清除DateBox的方法。目标是有一个简单的表单,允许用户填写它并使用按钮提交表单,然后让表单自动重置,以便用户可以再次填写新的提交。

或者,如果我怀疑还不能清除DateBox(功能限制),那么我可以简单地使用TextBox。为了让事情看起来更好(并帮助用户以脚本期望的格式输入日期),如果我可以将DatePicker添加到TextBox但是除了我发现的从头开始生成整个DatePicker函数的旧教程之外,这将是很好的。我还没有找到一种将新的DatePicker GAS类附加到TextBox的简单方法。

GAS文档:https://developers.google.com/apps-script/reference/ui/date-box

日期选择器2教程 - https://sites.google.com/site/appsscripttutorial/miscellaneous/date-picker-2

许多简化的示例代码,希望能够提供足够的信息,而不必强迫任何人阅读5页代码以达到我想要强调的内容。请特别注意app.getElementById('id').setItemSelected({item # for blank item}, true);函数中的注释。此代码可以复制到空白(已保存)的电子表格中进行测试,或者您可以替换电子表格特定的代码,以便在您更喜欢这样做时返回应用程序。

submitHandler(e)

总结:

  1. 有没有办法使用处理函数清除DateBox中的值?若然,怎么做?
  2. 如果无法清除DateBox,我怎样才能将DatePicker添加到TextBox并将数据输入限制为特定格式(因此TextBox不会将“blahblahblah”作为有效日期)?

1 个答案:

答案 0 :(得分:1)

您是否尝试过第三种解决方案,即创建一个新的datepicker小部件并将其添加到您的网格中以替换“已填充的”小部件?

这是一个测试,我在提交处理程序中执行“替换”,但我想它会放在一个名为“再次提交”的单独处理程序中,例如......(但这取决于你):

我更改了您的提交确认,以显示新datePicker返回的日期(仅用于测试)

//Opens the active spreadsheet and selects the first sheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = sheets[0];
var popAttributes = {'padding':'10px','font-family':"Arial, sans-serif",'fontSize':'10pt','color':'#000099','background':'#ffffee','border-radius':'10px'}
var btnAttributes = {'padding':'3px','font-family':"Arial, sans-serif",'fontSize':'10pt','border-radius':'6px'}

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').setStyleAttributes(popAttributes);
    var mainG = app.createGrid(3, 2).setId('grid');
      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').setStyleAttributes(btnAttributes);
//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('grid').setWidget(1, 1, app.createDateBox().setId('date').setName('date'))
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').setText(e.parameter.date);// to test the returned date value
return app;
}