繁体   English   中英

使用Google Apps脚本在Google Spreadsheets中创建对话框表单吗?

[英]Creating a dialog form in Google Spreadsheets with Google Apps Script?

我要的功能是这样的:

Click on a menu option
Interactive box comes up with 6 textboxes that correspond with a label each
In two of those boxes, at most three, a number is inputted
Corresponding to an algorithm those numbers will change the values in the current active cell range if the value in the cell (a letter) is equal to the label of any of the earlier textbox inputted values, the empty cells get left empty, others that have no correspondence will be changed to 0.
Click OK, the active selected cell range changes accordingly.

我希望将此功能嵌入到电子表格中。 我已经开始,但是由于我没有对GAS做任何事情,因此我偶然发现了一些问题,并且需要知道如何使用GAS的人的解决方案。 我不知道此功能是否可以按照我的设想实现。

function calculateScores() {
 var sheet = SpreadsheetApp.getActiveSheet();
 var range = sheet.getActiveRange();
 var values = range.getValues();
 for (var i = 0; i <= values.length - 1; i++) {
  var row = values[i];
  Logger.log(row);
 }
};

function onOpen() {
 var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
 var entries = [{
 name : "Calculate Scores",
 functionName : "calculateScores"
 }];
 spreadsheet.addMenu("Calculate Scores", entries);
};

这将创建菜单项,并仅记录所选范围。 展望未来,我该如何解决其余界面和功能的问题?

您可以使用UiApp进行此操作。 我在下面放了一些示例代码,可能会对您有所帮助。

您可以在此处的文档中找到有关UiApp的有用信息。

瓦卡尔·艾哈迈德(Waqar Ahmed)的网站上也有很多示例,我发现它们对我们很有帮助。

function calculateScores() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  //Create the user interface
  var app = UiApp.createApplication();

  //Create two textboxes
  var tBox1 = app.createTextBox().setName('tBox1').setId('tBox1');
  var tBox2 = app.createTextBox().setName('tBox2').setId('tBox2');

  //Create the OK button and a handler to respond to when OK is clicked.
  var okHandler = app.createServerHandler('respondToOK');
  var btnOK = app.createButton('OK', okHandler);

  //I find grids very handy for arranging labels and widgets like textboxes neatly
  var myGrid = app.createGrid(3, 2).setId('myGrid').setStyleAttribute('background', 'lightCyan')
    .setText(0, 0, 'Label for text box 1')
    .setWidget(0, 1, tBox1)
    .setText(1, 0, 'Label for text box 2')
    .setWidget(1, 1, tBox2)
    .setWidget(2, 1, btnOK);

  /*The OK button's handler needs a callback element otherwise it can't read the user's input.
    You could add multiple callback elements to each of the textboxes but that's just extra code.
    As all the textboxes are added to myGrid then just one callback to that will work fine.
  */
  okHandler.addCallbackElement(myGrid);

  app.add(myGrid);
  ss.show(app);
}

function respondToOK(e) {
  var app = UiApp.getActiveApplication();

  var entry1 = e.parameter.tBox1;
  var entry2 = e.parameter.tBox2;

  //Substitute your own algorhythm here, I just multiplied the two entries
  var result = entry1 * entry2;

  //Enter the result in the active cell
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var cellAddress = ss.getActiveCell().getA1Notation();
  ss.getActiveSheet().getRange(cellAddress).setValue(result);

  //Close the user interface
  return app.close();
}

希望这可以帮助。 祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM