如何使用谷歌脚本在提示对话中进行下拉数据验证?

时间:2018-02-16 23:44:48

标签: google-apps-script prompt

我尝试使用谷歌脚本为用户创建一个下拉列表的提示对话框。

到目前为止我在这里:



function addProject() {
  var ui = SpreadsheetApp.getUi(); // Same variations.

  var result = ui.prompt(
      'Select project from drop-down',
      '(use "Project Index" sheet to add a project)',
      ui.ButtonSet.OK);

  // Process the user's response.
  var button = result.getSelectedButton();
  var text = result.getResponseText();
  if (button == ui.Button.OK) {
    // User clicked "OK".
    ui.alert('You selected ' + text + '.');
  } else if (button == ui.Button.CANCEL) {
    // User clicked "Cancel".
    ui.alert('I didn\'t get your project.');
  }
}




我的工作表中的按钮激活此功能。所有这一切都是创建一个提示,您可以输入一个字符串。但我需要使用数据验证来创建一个下拉列表,该列表引用工作表中其他位置的范围,以便用户可以输入他们想要的任何内容。

有人可以帮助我在快速对话中获得一个下拉列表吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

很遗憾,您无法以这种方式使用ui.prompt。您最好的选择是使用Custom Dialog,您可以在其中输入下拉列表。

修改:对于部分代码答案,请在此处查看答案How to create a drop down list in (App Script) Spreadsheet Input Box?

答案 1 :(得分:-1)

function NewItem() {
  var doc = SpreadsheetApp.getActiveSpreadsheet();
  var app = UiApp.createApplication().setTitle('My Application');
  var panel = app.createVerticalPanel();
  var lb = app.createListBox(true).setId('myId').setName('myLbName');
var sh = SpreadsheetApp.getUi();
var response = sh.alert("Confirm!", "This will delete the selected item in all sheets, do you want to continue?", sh.ButtonSet.OK_CANCEL)

if (response == sh.Button.OK)
    {

  // List of categories for user to select
  lb.setVisibleItemCount(9);
  lb.addItem('Category 1');
  lb.addItem('Category 2');
  lb.addItem('Category 3');
  lb.addItem('Category 4');


  panel.add(lb);
  var button = app.createButton('OK');
  var handler = app.createServerClickHandler('click').addCallbackElement(panel);
  button.addClickHandler(handler);
  panel.add(button);
  app.add(panel);
  doc.show(app);
  }
    if (response == sh.Button.CANCEL)
    {
      sh.alert("Canceled", "The action is canceled", sh.ButtonSet.OK)
    }

}


function click(eventInfo) {
  var app = UiApp.getActiveApplication();
  // get category that user chose
  var value = eventInfo.parameter.myLbName;
  var doc = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("items_with_category");
  var RevenueItem = Browser.inputBox("Copy paste the name of the revenue item from the QB report:");
  var COSItem = Browser.inputBox("Copy paste the name of the COS item from the QB report:");
  var Client = Browser.inputBox("Copy past the client from QB report:");
  var RevAcct = Browser.inputBox("Revenue Account Code (eg. 4001):");  
  var COSAcct = Browser.inputBox("Revenue Account Code (eg. 5001):");  
  var lastRow = doc.getLastRow()+1;
  //doc.getRange(lastRow,1).setValue(value);

  doc.getRange(lastRow,1).setValue(COSItem);
  doc.getRange(lastRow,2).setValue(RevenueItem);
  doc.getRange(lastRow,3).setValue(Client);
  doc.getRange(lastRow,4).setValue(value);
}