ハンドラー関数内で DateBox の値をクリアすることは可能ですか? シンプルなだけでTextBoxの値をクリアできます.ListBoxを含めてそのアイテムに設定することapp.getElementById('id').setValue('');
で、ListBoxの値を空白の値に設定できますが、DateBoxをクリアする方法を見つけることができませんでした. 目標は、ユーザーがフォームに入力し、ボタンを使用してフォームを送信できるシンプルなフォームを作成し、フォームを自動リセットして、ユーザーが新しい送信のために再度入力できるようにすることです。listBox.addItem('')
app.getElementById('id').setItemSelected({item # for blank item}, true);
または、私が推測するように、DateBox (機能の制限) をまだクリアできない場合は、単純に TextBox を使用できます。見栄えを良くする (そして、ユーザーがスクリプトが期待する形式で日付を入力できるようにする) ために、TextBox に DatePicker を追加できればいいのですが、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 ページのコードを読まなくても十分な情報が提供されることを願っています。特に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" を取得しません)。